Tuesday, July 16, 2013

puts() as Output Function in C

In C, along with printf(), we have puts() to print some text on to the screen. We can print some literal text but not the variables. Using puts(), we can't print any variable values. But can print only some text on the output.

If we observe the prototype of the function puts() in STDIO.H, we will understand the function. It will be like this

int     _Cdecl puts(const char *__s);
So, as per the function, it can take only a character sequence but not the other arguments. Hence it can't print the values of the variables.

Example:

  1. #include<stdio.h>
  2. main()
  3. {
  4. puts("Hello world");
  5. return 0;
Also one more important note about the puts is that it automatically takes a newline after the execution. I mean, automatically cursor moves to the next line after printing the value.

In other words,
puts("Hello World");
is similar to
printf("Hello World\n"); 
We can use puts() in cases where we need not print the values of the variables.