printf function to display output

printf is a pre defined or built in function in C to send formatted outputs. We will first try to send a simple message by using printf function like this.

printf("welcome to plus2net");

The above command will print the message welcome to plus2net

Adding a new line or a like break

To above message we will add one like break by adding \n at the end of it. For a new line we have to use \n ( no space between \ and n ). We will print two lines like this

printf("welcome to plus2net \n C tutorial section");

The above command will output will be

Welcome to plus2net
C tutorial section

Formatted output using printf

WE can display decimal integers by using %d
We will declare one integer variable first and then we will store a value in it. Then we will display the value using printf command.
Note: The statements written after // are comments for easy understanding , these words will not be executed by C compiler

int main(void){
int num; // num is declared as integer variable
num=15; // data is stored
printf("num = %d ", num); // final output
return 0;

Here we have formatted the display by saying %d , that is we are sending an integer output. The above statement will display

Num = 15

To display a decimal value we will declare a float variable first and then format it to display. Like this ( changes in above code are only shown )

float num; // num is declared as float variable
num=17.873; // data is stored
printf("num = %5.3f", num); // final output

The output is

num = 17.873

Output of a single char

char var = 'P';
printf(" Welcome to %c ",var);

Displaying string

char var[20] = "Plus2net";
printf(" Welcome to %s ",var);

Output is here

Welcome to Plus2net

C language is case sensitive, lower and upper case letters are to be treated differently. Printf is not same as printf.

1.Write a program to display sum of two integers

2.Write a program to display a patern like this.

Number of User Comments : 1

Pratibha Suralkar


what is the output of following :


Post Comment This is for short comments only. Use the forum for more discussions.

HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2017 All rights reserved worldwide Privacy Policy Disclaimer