Thursday, July 4, 2013

Printf in C

Printf() is a function in C which enables user to print some text on to the screen. This printf() function is provided in the library of the C programming, in the header file "STDIO.H". This header is responsible for the Input and Output operations on C.

All the functions responsible for the Input and Output operations are present in this header file. We can see the prototypes of these functions by visiting the Include folder of the "TC" which you have installed on your computer.

If you observe a C program carefully, we do have a doubt regarding the printf() function. C doesn't support function overloading, but printf() takes any number of parameters we give. Did you ever have a doubt of that???


Let us know about that clearly.

If you visit the Include folder in TC, you fill find a STDIO.H file. If we open that in text format. We are able to see the prototypes of the functions in STDIO.H header as we have already discussed. Among them, we find the printf() function prototype as
int     _Cdecl printf(const char *__format, ...);
The _Cdecl defines the calling convention which, along with other things describe how arguments are handled. In this case, it means that args are pushed onto the stack and cleaned by the function making the call.

The const char * __format specifies the string format, i.e., it is the pointer of the String which we are going to print on the screen. We pass the pointer of the String as the first argument. This is the essential argument to be passed to the printf() function.

The ... represents the arguments followed. This represents, there can be any number of the arguments. Hence when we write a C program, we write the initial first parameter with some String mixed with the format specifiers, followed by the variables of the format specifiers we have used.

What happens if we have more number of parameters than the specified format specifiers or less number of parameters??? Click here to know more.