C printf()

The printf() function is an important function. This writes to stdout the arguments that make up the argument list as specified by the string pointed to by format.

The string pointed to by format consists of the two types of items. The first type is made up of the characters that will be printed on screen. And the second type contains format specifiers that defines the way the arguments can displayed. A format specifier begins with a percent sign (%) and is followed by the format code like:

printf("Hello %c %d %s", 'c', 100, "there!");

Following table lists the possible code with format :

C Programming Table:
Code Format
%a Hexadecimal output in the form of 0xh.hhhp+d
%A Hexadecimal output in the form of 0Xh.hhhP+d
%c Character
%d Signed decimal integers
%i Signed decimal integers
%e Scientific notation (lowercase e)
%E Scientific notation (uppercase E)
%f Decimal floating point
%F Decimal floating point (produces uppercase INF, INFINITY, or NAN only when applied to infinity or a value that is not a number. The %f specifier produces the lowercase equivalents)
%g Uses %e or %f, whichever is shorter
%G Uses %E or %F, whichever is shorter
%o Unsigned octal
%s Strings 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. This specifier causes the number of the characters written (up to the point at which %n is encountered) to be stored in that integer
%% Prints a percent sign

printf() Syntax

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

printf() Example

Following c code displays the number 10 after the line "this is a number" :

int i;
printf("this is a number\n");

Following c program displays the output shown in its comments :

void main()
	/* this will print "this is a test" left justified
	in 20 character field. */
	printf("%-20s", "this is a test");
	/* this will print a float with 3 decimal places
	in a 10 character field. The output will be 
	"     12.235". */
	printf("%10.3f", 12.234657);

C Online Test

« Previous Function Next Function »

Liked this article? Share it!

Like/follow us on social media for updates!