puts() in C
Last Updated :
06 Sep, 2023
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
#include <stdio.h>
int main()
{
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
#include <stdio.h>
int main()
{
int num = puts ( "Hello Geeks" );
printf ( "\n%d" , num);
return 0;
}
|
Notice the value of ‘num’ is 12 and not 11, since the puts function considers the newline character (“\n) as well.
Related Articles:
Share your thoughts in the comments
Please Login to comment...