This question already has answers here:
How # flag in printf works?
(3 answers)
Closed 7 years ago.
Can someone explain what the purpose of the # sign in this printf statement:
printf("%#d\n",15);
It seems to be ignored while printing. The output of the statement is:
15
You can look in the printf documentation. You can find the description for # under flags:
Used with o, x or X specifiers the value is preceded 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.
I didn't come up with this answer myself. I just did a quick Google search and found this:
"Adding a # will cause a '0' to be prepended to an octal number (when using the o conversion specifier), or a 0x to be prepended to a hexadecimal number (when using a x conversion specifier). For most other conversion specifiers, adding a # will simply force the inclusion of a decimal point, even if the number has no fractional part."
You can read more here:
http://www.cprogramming.com/tutorial/printf-format-strings.html
Related
This question already has answers here:
How to control the number of exponent digits after 'e' in C printf %e?
(3 answers)
Closed 3 years ago.
When I use this:
printf("%e", 2000.0);
I get output 2.000000e+003 which is correct but very ugly. I then tried like this:
printf("%.1e", 2000.0);
Which outputs 2.0e+003 which is far better. The question is how to remove the 2 padding zeroes in the exponent so that the output is 2.0e+3. Is there a specific printf specifier for that or do I have to write a function manually that analyzes the floating point number and prints it the way I want (I'd hate to have to do that)?
The standard C features for printf do not provide any means of controlling the number of digits in the exponent. If you want a different format, you will have to write your own code or obtain code from somewhere else.
(The %a conversion does use only as many digits in the exponent as needed, but it prints the value with hexadecimal.)
This question already has an answer here:
Strange behavior in C when calculating sum of digits with leading zeroes
(1 answer)
Closed 5 years ago.
The C function printf seems to print different values depending on whether or not leading zeros are present.
I was trying to determine the numerical values for the mode argument in the Linux 'open' system call.
printf("mode:%d\n",S_IRWXU);
printf("mode:%d\n",00700);
both gave me 448, while
printf("mode:%d\n",700); gives me 700, as I would expect from both.
What is going on here?
I am using gcc (Ubuntu 5.4.0-6ubuntu1~16.04.5) 5.4.0 20160609
A numerical constant with one or more leading zeros is an octal constant, while one without a leading zero is a decimal constant and one with a leading 0x is a hexadecimal constant. This holds in any context, whether the value is passed to printf or any other function.
In the case of printf, you're using the %d format specifier which prints the value in decimal. If you pass in an octal constant, you'll see the decimal value of that octal number. In this example, 0700b8 == 7 * 8^2 + 0 * 8^1 + 0 * 8^0 == 7 * 64 == 448b10
If you're dealing with file permissions, those values are typically denoted as octal, so you should always use a leading 0 with those.
This question already has answers here:
Why isn't "0f" treated as a floating point literal in C++?
(6 answers)
Closed 8 years ago.
The make difference between double and float data, we add f. But when i tried to write:
float Value = 255f;
The compiler diplays the following error.:
line 50: error (dcc:1633): parse error near 'f'
line 50: error (dcc:1206): syntax error
line 50: fatal error (dcc:1340): can't recover from earlier errors
Why ?
According to draft n1570, §6.4.4.2, paragraph ¶2
Description
A floating constant has a significand part that may be followed by an exponent part and a
suffix that specifies its type. The components of the significand part may include a digit
sequence representing the whole-number part, followed by a period (.), followed by a
digit sequence representing the fraction part. The components of the exponent part are an
e, E, p, or P followed by an exponent consisting of an optionally signed digit sequence.
Either the whole-number part or the fraction part has to be present; for decimal floating
constants, either the period or the exponent part has to be present.
I made the relevant part bold, so you can see why it doesn't work.
Note that this also implies that
float value = 255e0f;
works.
You need a period in addition, in order the compiler to accept it:
float Value = 255.f;
It's part of the standard specification but I guess it was chosen this way in order to simplify the implementation of the lexical analyzer and also improve the readability of the number, since it's easy to think it's a hex number otherwise.
My guess would be that you are missing a period in your code.
float Value = 255.f;
Add a period before f and you should be good to go. If this doesn't work, please tell us what Error you're receiving.
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)
#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.