puts() vs printf() for printing a string
Last Updated :
23 Jul, 2025
In C, both puts() and printf() functions are used for printing a string on the console and are defined in <stdio.h> header file. In this article, we will discuss the differences in the usage and behavior of both functions.
puts() Function
The puts() function is used to write a string to the console and it automatically adds a new line character '\n' at the end.
Syntax
puts("str");
Parameters
- str: It takes a null-terminated string as the argument.
Return Value
- It returns a non-negative value if the output is successful, or End-of-File if an error occurs.
printf() Function
The printf() function is also used to print data on the console, but it can also be used to print the formatted data to the console based on a specified format string.
Syntax
printf("format_string", Arguments);
Parameters
- format_string: It can be a simple string or a string containing format specifiers.
- Arguments: It is the variable names whose value is to be printed.
Return Value
- It returns the number of characters successfully written to the console and a negative value if an error occurs.
Which of the following two should be preferred?
puts() can be preferred for printing a string because it is generally less costly(implementation of puts() is generally simpler than printf()), and if the string has formatting characters like '%s', then printf() would give unexpected results. Also, if str is a user input string, then the use of printf() might cause security issues.
Also, note that puts() move the cursor to the next line.
If you do not want the cursor to be moved to the next line, then you can use the following variation of puts().
fputs(str, stdout)
Difference between the printf() and puts()
Following are some differences between printf() and puts() functions in C:
|
printf allows us to print formatted strings using format specifiers. | puts do not support formatting. |
printf does not add new line characters automatically. | puts automatically adds a new line character. |
It returns the number of characters successfully written to the console. | It returns a non-negative value on success and EOF (end-of-file) on failure. |
printf can handle multiple strings at one time which helps in concatenating the strings in the output. | puts can print a single string at one time. |
printf can print data of different data types. | puts can print only strings. |
You can try the following programs for testing the above-discussed differences between puts() and print().
Example 1
The below C program demonstrates the use of puts.
C
// C program to show the use of puts
#include <stdio.h>
int main()
{
puts("Geek%sforGeek%s");
getchar();
return 0;
}
Example 2
The below C program demonstrated the use of fputs.
C
// C program to show the use of fputs
#include <stdio.h>
int main()
{
fputs("Geeksfor", stdout);
fputs("Geeks", stdout);
return 0;
}
Example 3
The below C program demonstrates the unexpected behavior when %s is used in printf.
C
// C program to show the unexpected behaviour
// when %s is used in printf
#include <stdio.h>
int main()
{
// % is intentionally put here to show the unexpected
// behaviour when %s is used in printf
printf("Geek%sforGeek%s");
getchar();
return 0;
}
OutputGeek0>z??forGeek;>z??
Similar Reads
What is the difference between printf, sprintf and fprintf? The printf() function is used as a standard method for output operations and C also provides some different versions of this function such as sprintf() and fprintf(). These functions are also used for output operations but in different contexts.The below table lists the primary differences between t
3 min read
What is use of %n in printf() ? In C printf(), %n is a special format specifier which instead of printing something causes printf() to load the variable pointed by the corresponding argument with a value equal to the number of characters that have been printed by printf() before the occurrence of %n. C #include<stdio.h> int
1 min read
Format String Vulnerability and Prevention with Example A format string is an ASCII string that contains text and format parameters. Example: // A statement with format stringprintf("my name is : %s\n", "Akash");// Output// My name is : Akash There are several format strings that specify output in C and many other programming languages but our focus is o
3 min read
putchar() function in C The putchar(int ch) method in C is used to write a character, of unsigned char type, to stdout. This character is passed as the parameter to this method. Syntax: int putchar(int ch) Parameters: This method accepts a mandatory parameter ch which is the character to be written to stdout. Return Value:
1 min read
fprintf() in C fprintf is used to print content in file instead of stdout console. int fprintf(FILE *fptr, const char *str, ...); Example: C // C Program for the above approach #include<stdio.h> int main() { int i, n=2; char str[50]; //open file sample.txt in write mode FILE *fptr = fopen("sample.txt
1 min read
Difference between return and printf in C In C programming, return and print serve fundamentally different purposes and they are used in distinct contexts to achieve specific tasks. Let's see each of them and see what are their functions and differences. 1. Return Statement In C, return is a statement used within a function to terminate the
2 min read