C examples for Language Basics:printf
Code | Format |
---|---|
%a | Hexadecimal output in the form 0xh.hhhhp +d (C99 only). |
%A | Hexadecimal output in the form 0Xh.hhhhP+d (C99 only). |
%c | Character. |
%d | Signed decimal integers. |
%i | Signed decimal integers. |
%e | Scientific notation (lowercase e). |
%E | Scientific notation (uppercase E). |
%f | Decimal floating point. |
%g | Uses %e or %f, whichever is shorter. |
%G | Uses %E or %F, whichever is shorter. |
%o | Unsigned octal. |
%s | String of characters. |
%u | Unsigned decimal integers. |
%x | Unsigned hexadecimal (lowercase letters). |
%X | Unsigned hexadecimal (uppercase letters). |
%p | Displays a pointer. |
%n | The associated argument must be a pointer to an integer. |
%% | Prints a % sign. |
You can use either %d or %i to display a signed integer in decimal format.
To output an unsigned integer, use %u.
The %f format specifier displays numbers in floating point. The matching argument must be of type double.
The %e and %E specifiers tell printf( ) to display a double argument in scientific notation.
Numbers represented in scientific notation take this general form:
x.dddddE+/-yy
To display the letter E in uppercase, use the %E format; otherwise, use %e.
You can tell printf( ) to use either %f or %e by using the %g or %G format specifiers. This causes printf( ) to select the format specifier that produces the shortest output. Where applicable, use %G if you want E shown in uppercase; otherwise, use %g.
The following program demonstrates the effect of the %g format specifier:
#include <stdio.h> int main(void) { double f;//from w w w. j a va 2 s . co m for (f = 1.0; f<1.0e+10; f = f * 10) printf("%g ", f); return 0; }