In C programming language, puts() is a function defined in header <stdio.h> that prints strings character by character until the NULL character is encountered. The puts() function prints the newline character at the end of the output string.
Syntax
int puts(char* str);
Parameters
- str: string to be printed.
Return Value
The return value of the puts function depends on the success/failure of its execution.
- On success, the puts() function returns a non-negative value.
- Otherwise, an End-Of-File (EOF) error is returned.
Example
C
// C program to illutrate the use of puts() function #include <stdio.h> int main()
{ // using puts to print hello world
char * str1 = "Hello Geeks" ;
puts (str1);
puts ( "Welcome Geeks" );
return 0;
} |
Output
Hello Geeks Welcome Geeks
Difference between puts() and fputs()
The puts() and fputs() function have similar working in C programming language with major differences being:
- Unlike the puts function which writes only in the stdout stream (console), the fputs function can write to any stream.
- fputs function does not append a newline character in the stream.
Example
C
// C program to illustrate the return value of puts function #include <stdio.h> int main()
{ int num = puts ( "Hello Geeks" ); // storing the returned value in num
printf ( "\n%d" , num);
return 0;
} |
Output
Hello Geeks 12
Notice the value of ‘num’ is 12 and not 11, since the puts function considers the newline character (“\n) as well.