Using the C/C++ puts() Function
What Will You Learn in This Guide?
In this guide, you will learn how the puts() function** works in **C/C++ programming languages.
We will explain why it is simpler and more efficient than alternatives such as printf() or cout.
We will also examine the details of the puts() function step by step, such as automatic new line insertion, return value (EOF) and fputs() difference.
Technical Summary
- Purpose: Fast printing of string data to standard output stream (
stdout) - Language: C and C++
- Advantage: Simpler than
printf(), adds automatic\n - Return Value:
≥ 0on success,EOF (-1)on error - Area of Use: Log messages, error reporting, fast text output
What is the puts() Function?
The puts() function is used to print a string of characters (const char*) to the standard output stream (console).
Automatically adds a newline (\n) after printing.
<stdio.h> is defined in the header file.
int puts(const char* str);
- Parameter: Fixed character string to be printed
Return Value:
-
If successful → 0 or a positive integer
-
In case of error → returns EOF (-1)
1. Basic Usage
The example below prints a text to stdout with puts().
#include<stdio.h>
int main() {
char GenixNodeMetni[] = "GenixNode platformu";
puts(GenixNodeMetni); // Metni ekrana yazar
return 0;
}
- Output:
GenixNode platformu
- Description: puts() successfully writes the string and automatically adds a new line at the end.
2. Auto New Line Feature
puts() automatically adds \n on every call. In this way printf("...\n"); there is no need to write.
#include<stdio.h>
int main() {
char Metin1[] = "İlk satır";
char Metin2[] = "İkinci satır";
puts(Metin1);
puts(Metin2);
return 0;
}
- Output:
İlk satır
İkinci satır
- This feature eliminates the need to write extra code when printing multi-line log messages to the console.
3. Checking the Return Value
If puts() runs successfully, it returns a non-negative value; in case of error, it returns EOF (-1).
#include<stdio.h>
int main() {
char TestMetni[] = "puts() dönüş değerini inceliyoruz.";
int val = puts(TestMetni); // stdout’a yazar ve sonucu döndürür
printf("Dönen Değer Val = %d\n", val);
return 0;
}
- Output:
puts() dönüş değerini inceliyoruz.
Dönen Değer Val = 0
Differences Between puts() and fputs()
| Feature | puts() | fputs() |
|---|---|---|
| Target Stream | stdout only (display) | Any stream (stdout, file, etc.) |
| Adding New Line | Automatically adds \n | Must be added manually |
| Flexibility | Simple and fast | More flexible for file operations |
| Error Status | EOF (-1) rotary | EOF (-1) rotary |
| Area of Use | Print to console | Printing to file or stream |
- Description:
puts() is ideal for log and simple text output. fputs() is preferred for file writing or special stream operations.
Frequently Asked Questions (FAQ)
- Why is it advantageous to use puts() instead of printf()?
In string printing operations, puts() is simpler and faster than printf().
- Why does puts() take char and not std::string?*
Since puts() belongs to the C library, it accepts null terminated (\0) character strings. If you are using std::string in C++, you can convert it with the .c_str() method.
- When does puts() stop output?
Printing ends when the null character (\0) at the end of the text is seen.
- Why can puts() be used when cout is available in C++?
puts() provides compatibility with legacy C-style code and is generally faster than cout for single lines of text.
- What is returned in case of error?
puts() returns EOF on error and does not write anything to the screen.
Result
The puts() function is one of the fastest and simplest ways to print text in C and C++. With its automatic new line insertion feature, it offers more practical use than printf() or cout. fputs() is preferred for file-based operations, and puts() is preferred for fast console logs.
Try it now on the GenixNode platform to test your code outputs and see the performance differences!

