printf

Syntax:

#include <stdio.h> // or cstdio

int printf(const char *format,...);

Description:

Print formatted output text to standard output (stdout).

Example:

#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

Printf format:

Syntax:

%[flags][width][.percision][length]type

 

Flags (optional):

flag description
- align left
+ print sign
0 extra zeros padding
space print space before number, when no sign
# other format

 

Width (optional):

Minimal output characters width.

 

Percision (optional):

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 (optional):

length description
h short / unsigned short
l long / unsigned long
L long double

 

Type:

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 %   %

 


Escape sequences

C Standard library functions

printf on wikipedia

Write how to improve this page

C REFERENCE
RAPID TABLES