printf()

The print function from the stdio.h library enables data to be sent to the stdout (usually the screen).

 

printf() takes a “control string” (enclosed within double quotes) followed by optional data, often in the form of variables as its arguments, to be printed on the screen

 

takes the format of: printf(“control string”data) ;

 

e.g.

printf(“Hello, World!n”) ;

printf(“The data is the integer: %d \n”16) ;

printf(“The data is the integer: %d \n”myVar) ;

 

The %d is a format specifier (aka conversion characters), that indicates the format of the data being passed in to the string.

 

Each format specifier will take the next value to the right of the control string.

 

e.g.  To insert three values into the control string, use their appropriate format specifier at the position required in the control string:

 

printf(“The first number is: %d, followed by a floating point number: %f, and the character %c.”1799.98, ‘D’) ;

 

Which would output the following on the screen:

The first number is: 17, followed by a floating point number: 99.98, and the character D.

Leave a Reply