When outputting a string to the console, the puts function conveniently adds the newline character "\n" at the end.
Syntax:
It has the following syntax:
puts("str");
Parameters
It receives a string as input that is terminated by a null character.
When an error occurs, the function will return ### Return Value; conversely, a successful output will result in a non-negative value being returned.
What is the printf Function?
In conjunction with displaying information on the console, the printf function can output formatted data according to a specified format string.
Syntax:
It has the following syntax:
printf("format_string", Arguments);
Parameters
- format_string: Either a plain string or a string with format specifiers can be used.
- Arguments: The values that must be shown are those of the variable names.
If not, it returns the count of characters that have been successfully input into the console.
Efficiency:
- Puts does not require formatting. Hence, it is typically faster and more effective when printing strings.
- Because of its formatting capabilities, the printf has a higher processing overhead than puts, which could lead to a marginally slower output of simple strings.
- Puts prints the string followed by a newline character; it provides no formatting
- Printf lets us use format specifiers to set various formatting parameters, including alignment, field width, precision, and more.
- Puts adds a newline character \n at the end of the string by default and is only intended to print strings. The string to be printed is the only argument required.
- A more flexible function, the printf, may print strings and other data types and manage different formatting styles. With the use of format specifiers, it permits more intricate output formatting. The Printf can print a string with the %s format specifier.
Formatting:
Usage:
Which of the following two should be preferred?
When it comes to displaying a string, using puts might be a better choice compared to printf due to its lower cost (puts implementation is usually simpler than printf). Additionally, printf could result in unforeseen outcomes if the string includes special formatting characters such as '%s'. Furthermore, employing printf with a string input by the user, like str, may pose security risks.
Bear in mind that the puts function moves the cursor to the next line.
Utilize the provided variation of puts when there is a preference to prevent the cursor from moving to the subsequent line.
fputs(str, stdout)
Main differences between the printf and puts
There exist multiple variances between the printf and puts functions in the C programming language. Certain key distinctions between these functions include:
| printf() | puts |
|---|---|
| Format specifiers can be used with printf to print formatted strings. | Puts do not support formatting. |
| Printf does not automatically append new line characters. | Puts automatically appends a new line character. |
| The amount of characters successfully written to the terminal is returned. | In the event that it is unsuccessful, it returns EOF (end-of-file); if successful, it returns a non-negative result. |
| Printf's ability to handle several strings at once makes concatenating the strings in the output easier. | One string can be printed at a time using Puts. |
| Printf is capable of printing a variety of data kinds. | Puts can only print characters. |
Example 1:
#include <stdio.h>
int main()
{
puts("Java%sLogic Practice%s");
getchar();
return 0;
}
Output:
[Program Output]
Example 2
The following C code showcases the utilization of the fputs function.
#include <stdio.h>
int main()
{
fputs("Java", stdout);
fputs("Logic Practice", stdout);
return 0;
}
Output:
[Program Output]
Example 3
The following C code showcases the unpredictable outcome that occurs when %s is utilized within the printf function.
#include <stdio.h>
int main()
{
printf("Java%sLogic Practice%s");
getchar();
return 0;
}
Output: