why does following printf print 0x32 - c

#include<stdio.h>
int main ()
{
printf("%#04x",50);
}
Some one showed me above code and I could not understand it since I do have used printf in C programs but above sort of use I saw in my life for the first time.
Why did above code prints output as
0x32
Can some one give me a link or reference to some thing so that I can understand it better.

From the printf(3) manual page:
The flag characters
The character % is followed by zero or more of the following flags:
# — The value should be converted to an "alternate form". For o conversions, the first character of the output string is made zero (by prefixing a 0 if it was not zero already). For x and X conversions, a nonzero result has the string "0x" (or "0X" for X conversions) prepended to it. For a, A, e, E, f, F, g, and G conversions, the result will always contain a decimal point, even if no digits follow it (normally, a decimal point appears in the results of those conversions only if a digit follows). For g and G conversions, trailing zeros are not removed from the result as they would otherwise be. For other conversions, the result is undefined.
Other flags omitted...
Thus, %# prepends "0x" to the output since the output format is x. The 0 is there to use 0 (zeros) for padding. Then the number 4 says the total number of characters to print is four. Had the number 4 been exchanged with 10, the output would have been 0x00000032 – a total of 10 characters.

50 decimal is 32 hexadecimal.
Apart from that, the documentation should tell you all you need to know.

Because %x formats the given value into hexadecimal.
In facts, 0x32 (hex) == 50 (decimal).
This is a good primer for reference.

Related

C printf difference between 0 flag & width attribute and precision flag

I'm currently learning the printf function of libc and I don't understand, what is the difference between:
printf("Test : %010d", 10);
using the 0 flag and 10 as width specifier
and
printf("Test : %.10d", 10);
using 10 as precision specifier
That produce the same output: Test : 0000000010
We'll start with the docs for printf() and I'll highlight their relevant bits.
First 0 padding.
`0' (zero)
Zero padding. For all conversions except n, the converted value is padded on the left with zeros rather than blanks. If a precision is given with a numeric conversion (d, i, o, u, i, x, and X), the 0 flag is ignored.
And then precision.
An optional precision, in the form of a period . followed by an optional digit string. If the digit string is omitted, the precision is taken as zero. This gives the minimum number of digits to appear for d, i, o, u, x, and X conversions, the number of digits to appear after the decimal-point for a, A, e, E, f, and F conversions, the maximum number of significant digits for g and G conversions, or the maximum number of characters to be printed from a string for s conversions.
%010d says to zero-pad to a minimum width of 10 digits. No problem there.
%.10d", because you're using %d, says the minimum number of digits to appear is 10. So the same thing as zero padding. %.10f would behave more like you expected.
I would recommend you use %010d to zero pad. The %.10d form is a surprising feature that might confuse readers. I didn't know about it and I'm surprised it isn't simply ignored.
Both formats produce the same output for positive numbers, but the output differs for negative numbers greater than -1000000000:
printf("Test : %010d", -10); produces -000000010
whereas
printf("Test : %.10d", -10); produces -0000000010
Format %010d pads the output with leading zeroes upto a width of 10 characters.
Format %.10d pads the converted number with leading zeroes upto 10 digits.
The second form is useful if you want to produce no output for value 0 but otherwise produce the normal conversion like %d:
printf("%.0d", 0); // no output
printf("%.0d", 10); // outputs 10
Also note that the initial 0 in the first form is a flag: it can be combined with other flags in any order as in %0+10d which produces +000000010 and it can be used with an indirect width as in printf("%0*d", 10, 10); which produces 0000000010.
There's no difference besides maybe a purely conceptual one.
In the first case you are just filling the blank area with completely independent padding 0 characters. In the second case these zeros are leading zeros created when converting your argument value. (This is admittedly very contrived.)
In any case these zeros look, smell and quack the same.
However, in general case there's one obscure specific situation when precision behaves differently from padded field-width: when you are asking for zero field width and print zero value. When zero precision is used, zero values are simply not printed at all. When zero field-width is used, zero values will appear as usual
printf("%00d\n", 0); // prints '0'
printf("%.0d\n", 0); // prints nothing
Obviously this is also a very contrived situation, since no padding occurs in this case.
In your second case you probably expected 10.0000000000 - but %d is only for integers. The specification says:
For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written.
(Precision is the part started with . , so in your case 10 .)
So, with %.10d you specified at least 10 digits to express the two-digit number, so it is completed with the 8 leading zeroes.
It means that both %010d and %.10d will produce the same result.

%.#s format specifier in printf statement in c

Please explain the output. What does %.#s in printf() mean?
#include<stdio.h>
#include <stdlib.h>
int main(int argc,char*argv[]){
char *A="HELLO";
printf("%.#s %.2s\n",A,A);
return 0;
}
OUTPUT:
#s HE
It's undefined behavior. # in printf format specifier means alternative form, but according to the standard, # is only used together with o, a, A, x, X, e, E, f, F, g, G, not including s.
C11 §7.21.6.1 The fprintf function Section 6
# The result is converted to an ‘‘alternative form’’. For o conversion, it increases
the precision, if and only if necessary, to force the first digit of the result to be a
zero (if the value and precision are both 0, a single 0 is printed). For x (or X)
conversion, a nonzero result has 0x (or 0X) prefixed to it. For a, A, e, E, f, F, g, and G conversions, the result of converting a floating-point number always
contains a decimal-point character, even if no digits follow it. (Normally, a decimal-point character appears in the result of these conversions only if a digit follows it.) For g and G conversions, trailing zeros are not removed from the result. For other conversions, the behavior is undefined.
For example, on my machine, output is different: %.0#s HE
%.1s is used to print the first character of the string
%.2s is used to print the first two characters of the string
%.3s is used to print the first three characters of the string and so on
where # : alternative form of the conversion is performed is a flag which have an optional usage with the format parameter in printf() and fprintf() functions etc.
But as #Yu Hao said # is only used together with o, a, A, x, X, e, E, f, F, g, G, not including s.
in your case %.#s usage is Wrong.
Example usage from reference given by #WhozCraig :
printf("Hexadecimal:\t%x %x %X %#x\n", 5, 10, 10, 6);
printf("Octal:\t%o %#o %#o\n", 10, 10, 4);
I agree with Yu Hao's answer that it is undefined behavior, but I think the reason is different. Yes, the # character works as a flag to convert the result to an alternative format. Yes, the # flag is undefined for strings. But in this case, the # is not a flag, it's a precision. It's still undefined, but the reason is different
The C11 standard at §6.21.6.1 says that the % sign is followed in sequence by:
Zero or more flags (including #)
An optional minimum field width
An optional precision
An optional length modifier
A conversion specifier character
Except for the conversion specifier, these are all optional. But the order in which they appear is always as above. So flag, if present, has to be first, immediately after the % character. Here what follows the % is not something indicating a flag, it is a period: %., indicating precision.
When you have %.# in your format string for printf(), the period indicates that the following character is the precision for the conversion specification that follows. I.e., the # in your code specifies the precision for the string s, not a flag. To be a flag, it would have to directly follow the % character, without the intervening period.
With regard to precision, the C standard §7.21.6.1 says this:
The precision takes the form of a period (.) followed either by an asterisk *
(described later) or by an optional decimal integer; if only the period is specified,
the precision is taken as zero. If a precision appears with any other conversion
specifier, the behavior is undefined.
Since in your format string you have %.#s, and # is neither an asterisk * nor a decimal integer, the result is undefined.
So to be extremely exact about why your code is undefined, I think it's because the # character appears in place of a legal precision, not because it is an illegal flag for the %s conversion. It would be illegal as a flag, of course, but that's not what is precisely (har har) happening here.

What this "#" is doing here

I came up with a code
#include <stdio.h>
int main()
{
int i = 1427;
double d = 1427.0;
printf("%#o\n", i);
printf("%#X\n", i);
printf("\n%g\n", d);
printf("%#g\n", d);
return 0;
}
which is giving the output:
02623
0X593
1427
1427.00
First I thought # is used for prefixing 0 to the output but I was wrong because of its strange behavior in last output in which it is printing zeroes after decimal.
Could someone explain what this #is and what it is doing here?
The # flag has a different behavior, depending on context.
If it is used with the o, x, or X specifiers, the value is preceded with 0, 0x, or 0X respectively.
If it is used with a/A, e/E, f/F, or G, the value always ends with a decimal point.
This behavior is quite well documented multiple places on the web. Searching for "printf" and possibly "format specifiers" will generally turn up lots of good links. Here are a few to whet your appetite:
http://en.wikipedia.org/wiki/Printf_format_string#Format_placeholders
http://www.cplusplus.com/reference/cstdio/printf/
http://en.cppreference.com/w/cpp/io/c/fprintf
http://msdn.microsoft.com/en-us/library/56e442dc.aspx
From the POSIX man pages man 3p printf:
#: Specifies that the value is to be converted to an alternative form. For o conversion, it increases the precision (if necessary) to force
the first digit of the result to be zero. For x or X conversion specifiers, a non-zero result shall have 0x (or 0X) prefixed to it. For a,
A, e, E, f, F, g, and G conversion specifiers, the result shall always contain a radix character, even if no digits follow the radix charac‐
ter. Without this flag, a radix character appears in the result of these conversions only if a digit follows it. For g and G conversion
specifiers, trailing zeros shall not be removed from the result as they normally are. For other conversion specifiers, the behavior is
undefined.
Putting it simple: it prints the alternative format, whatever it is. For hex. values, it is prefixing 0x, for octal 0, and for floating points, it forces some decimal rules...
I think that if you read about C's printf function here, you'll be able to further understand things such as "#", among others.
Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X
respectively for values different than zero. Used with a, A, e, E, f,
F, g or G it forces the written output to contain a decimal point even
if no more digits follow. By default, if no digits follow, no decimal
point is written.
Exerpt from wikipedia:
# Alternate form. For 'g' and 'G', trailing zeros are not removed. For 'f', 'F', 'e', 'E', 'g', 'G', the output always contains a decimal point. For 'o', 'x', and 'X', a 0, 0x, and 0X, respectively, is prepended to non-zero numbers.
%#o (Octal) 0 prefix inserted.
%#x (Hex) 0x prefix added to non-zero values.
%#X (Hex) 0X prefix added to non-zero values.
%#g Always show the decimal point trailing
zeros not removed.
Zeroes are bound to come after decimal because you are using %#g.

What does this kind of formatter mean?

printf ("%#p [ buf ] (%.2d) : %s \n", buf, strlen (buf), buf);
I never see %#p (%.2d) before,how does it work?
From http://www.cplusplus.com/reference/clibrary/cstdio/printf/:
Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero.
Used with e, E and f, it forces the written output to contain a decimal point even if no digits would follow. By default, if no digits follow, no decimal point is written.
Used with g or G the result is the same as with e or E but trailing zeros are not removed.
So it seems to do nothing in your case, since p is used to print a pointer address. I guess some compilers might interpret this differently, but I can't find any mention of it.
p specifies to print an address (i.e. a pointer). The # flag specifies "alternate form", which in this case, probably prepends 0x to the output.
It's a flag for the format identifier.
It will more than likely print out 0x before the pointed value (but I have not checked TBH)
A good explanation is found here
Not sure if that is a valid use of the '#' flag:
Used with o, x or X specifiers the
value is preceeded with 0, 0x or 0X
respectively for values different
than zero.
Used with e, E and f, it
forces the written output to contain
a decimal point even if no digits
would follow. By default, if no
digits follow, no decimal point is
written.
Used with g or G the result
is the same as with e or E but
trailing zeros are not removed.
It will most likely print an alternately formatted form for the pointer, appending 0x to the address.
In your case (p conversion) the result is undefined according to the man page. Anyway, %p and %#p prints the same value on my machine (looks like 0x7FFFF000)

Why does "%.3i" print leading zeros?

There was a bit of a surprise with some code today. I was compiling it on AIX, with the warning level set to anal to see what rogue issues might be lurking. Something new crawled out of the code.
1540-2837 (W) '0' flag is disregarded when combined with
precision and 'i' printf format.
After looking at the offending line, I put together a little program to reproduce it. Testing it on several platforms showed that it is not AIX specific.
The first printf below mimics what was found in the program.
#include <stdio.h>
int main(void)
{
int x = 3;
printf("Format 0.3i <%0.3i>\n", x); // prints 003, and AIX does a warning
printf("Format .3i <%.3i>\n", x); // prints 003, with no warning
printf("Format 3i <%3i>\n", x); // prints 3, with no warning.
return 0;
}
Normally, if leading zeros were needed, a format of "03i" would do the job nicely.
What does "%.3i" really mean here?
Why does it have the behavior that it does?
.X, where x is a number means "print at least X" digits, so %.3i means print at least 3 digits. If the number is less than 100, it is left padded with zeros.
From a doc on printf
"For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0."
There is another concept, "width" (e.g. "%3i") which causes a certain number of characters to be output (not necessarily digits, and the 0 specified is used to say that those characters should be 0s, as in "003" and not spaces, as in " 3")
From man 3 printf:
If a precision is given with a
numeric conversion (d, i, o, u, x, and
X), the 0 flag is ignored.
The . is specifying the precision and therefore the 0 is ignored. As to the "Why?" of it, you'd have to ask the authors of the C standard :)

Resources