Nearby lessons
44 of 72C - String Output
Learn how to display strings in C using printf(), puts(), and fputs(). Understand their basic differences through simple examples.
Displaying a String Using printf()
We can use printf() to display a string on the screen.
The %s format specifier is used to display a string.
Displaying Multiple Strings Using printf()
We can display multiple strings and messages using printf().
Use \n when you want to move the output to the next line.
Displaying a String Using puts()
The puts() function displays a string and automatically moves the cursor to the next line.
It is useful when we want to display a simple message.
Displaying a String Using fputs()
The fputs() function displays a string on the specified output stream.
For screen output, we can use stdout. Unlike puts(), fputs() does not automatically add a newline.
Difference Between printf(), puts(), and fputs()
These functions can display strings, but they have some differences.
printf()supports format specifiers such as%sand%d.puts()displays a string and automatically adds a newline.fputs()displays a string without automatically adding a newline.
For beginners, use printf() when you need to display variables with labels, and use puts() for simple messages.
Common Mistakes
- Using
%dinstead of%sto display a string. - Expecting
printf()to move to the next line without using\n. - Forgetting that
puts()adds a newline automatically. - Expecting
fputs()to add a newline automatically.
- printf("%s", name) displays a string.
- The %s format specifier is used to display a string.
- puts() automatically adds a newline after the string.
- fputs() does not automatically add a newline.
- Choose the output function according to your requirement.