Friday, May 16, 2014

printf() function

printf() function

you will think that printf() is  simple function and WHAT TO LEARN ABOUT IT ?

Today we will going to learn some facts of printf() which you sometimes ignored.

printf() is standard inbuilt function of C programming language defined in <stdio.h> header file. It is used to perform output operation on output screen i.e. It is used to print data on output screen, data includes string, character, integer number, floating number etc.

Let us check your knowledge, please try to answer following program.
Find output of following program.

Example 1:
#include <stdio.h>
main()
{
printf("Value=%d",printf("Hello"));
}

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Output of this program is 

==> HelloValue=5

Let us learn the core concept of printf() function.

syntax :


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



printf() function prints the data on output screen as per the given format.
Upon successful operation of printf(), these functions returns the number of 
 characters  printed on output screen and therefore its return type is int.

In the above example, inner printf() function prints "Hello" on output screen 
and returns 5 (length of "Hello" string) to outer printf() function and therefore outer printf function gets value 5 in place of %d format specifier.

Try more examples :

Example 2:

#include <stdio.h>
main()
{
int n=100;
printf("Value=%d",printf("Value=%d",n));
}

output : Value=100Value=9

Example 3:

#include <stdio.h>
main()
{
int n=10;
printf("Value=%d",printf("Value=%d",n));
}

output : Value=10Value=8




Best of Luck - Try to learn the unknown things.