#include <stdio.h> // or cstdio
int printf(const char *format,...);
Print formatted output text to standard output (stdout).
#include <stdio.h>
main()
{
long x = 200000;
double y = 0.0088;
char str[10] = "John Smith";
int val=printf("x=%d, y=%f\nname: %s",x,y,str);
if( val == -1 )
return -1
retrun 0;
}
Will print:
x=200000, y=0.0088
name: John Smith
%[flags][width][.percision][length]type
flag | description |
---|---|
- | align left |
+ | print sign |
0 | extra zeros padding |
space | print space before number, when no sign |
# | other format |
Minimal output characters width.
Set the number of digits after the decimal point for number print.
Set the maximum number of characters to be printed for a string print.
length | description |
---|---|
h | short / unsigned short |
l | long / unsigned long |
L | long double |
type | description | variable type | print format |
---|---|---|---|
c | unsigned character | unsigned char | c |
d i | signed decimal integer | int | [-]dddddd |
u | unsigned decimal integer | unsigned int | dddddd |
x | unsigned hexadecimal integer - lower case a..f | unsigned int | hhhhhh |
X | unsigned hexadecimal integer - upper case A..F | unsigned int | HHHHHH |
o | unsigned octal integer | unsigned int | oooooo |
f | floating point number | double | [-]ddd.ddd |
e | floating point number | double | [-]ddd.dddedd |
E | floating point number | double | [-]ddd.dddEdd |
g | minimal length format selection: %f or %e | double | |
G | minimal length format selection: %f or %E | double | |
p | address of pointer | void * | hhhhhhhh |
s | print string | char * | |
n | argument get the number of characters written by printf | int * | |
% | print % | % |