Online Reference

 

 

C REFERENCE
RAPID TABLES

 

 

    Home > Programming > C Reference > C Standard Library > printf

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

flagdescription
-align left
+print sign
0extra zeros padding
spaceprint 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):

lengthdescription
hshort / unsigned short
llong / unsigned long
Llong double

 

Type:

typedescriptionvariable typeprint format
cunsigned character unsigned charc
d isigned decimal integerint[-]dddddd
uunsigned decimal integerunsigned intdddddd
xunsigned hexadecimal integer - lower case a..funsigned inthhhhhh
Xunsigned hexadecimal integer - upper case A..Funsigned intHHHHHH
ounsigned octal integerunsigned intoooooo
ffloating point numberdouble[-]ddd.ddd
efloating point numberdouble[-]ddd.dddedd
Efloating point numberdouble[-]ddd.dddEdd
gminimal length format selection: %f or %edouble 
Gminimal length format selection: %f or %Edouble 
paddress of pointervoid *hhhhhhhh
sprint stringchar * 
nargument get the number of characters written by printfint * 
%print % %

 


Escape sequences

C Standard library functions

printf on wikipedia

© 2006-2008 RapidTables.com