This question already has answers here:
Set variable text column width in printf
(2 answers)
Closed 2 years ago.
// If I have an int, eg
int num=3;
//then how can I do this
printf(" %nums", some_string);
// to get it right aligned by 3 characters
context: I need to use a loop to print statements with variable alignments depending on the order they are printed, if I cant use a variable I cant do that.
Yes, it's in the printf manpage:
Instead of a decimal digit string one may write "*" or "*m$" (for some
decimal integer m) to specify that the field width is given in the
next argument, or in the m-th argument, respectively, which must be of
type int.
Related
This question already has an answer here:
Detecting integral overflow with scanf
(1 answer)
Closed 5 years ago.
I'm checking my code with different inputs. I have for example the next code:
if((scanf("%d",&n)!=1)) {
printf("Invalid number\n");
return 0;
}
if i'll try to scan a higher number of int than allowed (for example: 10000000000), it won't print "Invalid number". I want it to print and end the program. what to do?
Number of read items would be 1 but as int would not be able to hold the value it would hold something other than what is entered. Because 32 bit signed int(most likely the case) won't be able to hold that number. As a result it will contain some garbage value.
This question already has answers here:
Number of character cells used by string
(6 answers)
Closed 5 years ago.
i have a problem with printf width modifier in C
example:
char a[] = "o", b[] = "l";
printf("%-3s %s", a, b);
console output gives me 3 spaces between strings, but when I change "o" to "ó", console shows 2 spaces between. Every time i use character like "ł", "ó", "ś" in the string, width modifier is shortened by 1 sign, why this happens?
OS X 10.11 (El Captain)
"Special" characters as you are showing them need more bytes (char) to be represented in a string. Your arbitrary limit of 3 is not enough, raise it to some suitable value.
In addition, the way and length that such special characters are represented depends on the system. For portable code you should never make such arbitrary assumptions.
This question already has answers here:
Set variable text column width in printf
(2 answers)
Closed 6 years ago.
In C, as I understand so far, this would be correct:
printf("%10s\n", "This is C");
would return:
" This is C!"
(with the intentional space prior to the string; no quotations).
My question is can you replace the 10 specifying the length of the print using a variable? If so, how?
That's how:
printf("%*s\n", 10, "This is C");
The format changed from %10s to %*s. The printf() now would expect among the argument, before the string, an int with the width to pad the string to (the 10 in the example above; obviously could be a variable too).
To tell the printf() to pad the output to the left (instead of the default right) use the -: %-*s. (The output would change from " This is C" to "This is C ".)
To tell the printf() to take only few first bytes from the string, or if the string is not null-terminated, you can add .* to the format at the same place as the precision for floating point types. The printf() would print up to that number of characters, stopping at the first null character. Example:
int width = 10;
int chars = 4;
printf( "%-*.*s", width, chars, "This is C" );
would produce output "This ".
This question already has answers here:
What is the use of the %n format specifier in C?
(12 answers)
Closed 7 years ago.
I just encountered this by looking in the standard:
7.19.6.1 The fprintf function
in
8 The conversion specifiers and their meanings are:
regarding to:
n The argument shall be a pointer to signed integer into which is written the
number of characters written to the output stream so far by this call to
fprintf. No argument is converted, but one is consumed. If the conversion
specification includes any flags, a field width, or a precision, the behavior is
undefined.
What does this mean? What does %n do?
Did I get it correct, that acording to:
Returns
14 The fprintf function returns the number of characters transmitted
In this snippet:
int a, b;
b = printf ("Thi%n\s is just a test",&a);
a would equal to b?
the number of characters written to the output stream so far
"So far", means wherever you place your %n, the result will change. As of your example, it will be 3.
If your increase your %s position by one char, the resulting variable pointed will increase by one. Placing your %s at the very end of the string will make it equal to the value returned by printf
a = 3 and b = 19 for your case
a will be equal to number of character printed before %n.
Suppose you try to print printf ("This%sis%n just a test","coder", &a);
Then the value of a will be this + coder + is = 11.
And the value of b is always the total number of characters printed
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
what does “%.*s” mean in printf in c
I have found the following line :
asprintf(&c, "%s%.*s", *msg_in, size * rxed, buffer)
And I want to know the meaning of %.*s
The %.*s format means "print a string using a field width of n characters, where n is read from the next argument".
So here, it prints buffer with a width of size * rxed characters. (padding with spaces if necessary)
I would highly recommend reading the manual...
.* in a format string means:
the precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
Details can be seen here.
So you didn't give any details, but if the result of: size * rxed was 5, then you could do this:
asprintf(&c, "%s%.*s", *msg_in, size * rxed, buffer)
or
asprintf(&c, "%s%5s", *msg_in, buffer)
to the same effect.