C Programming/C Reference/stdio.h/puts

From Wikibooks, open books for an open world
Jump to: navigation, search

Puts() is a function used to display strings on screen. for example,

     #include<stdio.h>
     int main()
     {
             puts("welcome to WIKIPEDIA!!!");
     }

output:

welcome to WIKIPEDIA!!!

It simply prints the string as it is. It always ends with semicolon, as it is a complete c statement. It also places the cursor to the next line. It is same as printf() function or we can say printf()'s simplified function is puts(). It displays the whole string but it doesn't consider all formatting job as in printf(). We can pass only one argument at a time to puts(), and thus one puts() function displays only one string.

We can also pass a variable to put() for example,

     #include <stdio.h>
     int main()
     {
           char str[] = "welcome to WIKIPEDIA!!!";
           puts(str);
     }

output:

welcome to WIKIPEDIA!!!

The string gets stored at s[] and puts(s) is called it displays the string.

     int puts(const char *str)

In this puts() function writes string str and a newline to stdout. This function begins copying from address which is specified in the function (here it is str) till the null char occurs i.e. '\o' and this char doesn't get copy to stdout. while it returns EOF if an error occur,otherwise non-negative value.