Gets and puts in C are standard input and output functions used for handling strings. The gets() function was used to read a line of text from standard input, and the puts() function is used to write a string to standard output. These two functions are often taught together in old C material, but they are not equally safe or equally recommended in modern programming.
The biggest issue is that gets() is unsafe because it does not check the input length. That can lead to buffer overflow. Because of this, modern C programming avoids gets() and prefers safer functions such as fgets(). In this article, we will understand gets and puts in C, their syntax, examples, differences, limitations, common mistakes, and best practices.
What are gets and puts in C?
gets() and puts() are string handling functions from the C standard library.
gets()reads a string from input.puts()prints a string to output.
puts()is still easy to understand, butgets()is unsafe and should not be used in modern C programs.
Header File for gets and puts in C
These functions are associated with the standard input and output header file:
#include <stdio.h>gets() Function in C
gets() was used to read a full line of text from standard input into a character array until a newline character is encountered.
Old syntax:
gets(str);Example:
#include <stdio.h>
int main(void)
{
char name[20];
gets(name);
puts(name);
return 0;
}This code may compile on some old systems, but it is not considered safe.
Why gets() is Unsafe in C
The problem with gets() is that it does not know the size of the destination array. If the user enters more characters than the array can hold, memory outside the array may be overwritten.
- it can cause buffer overflow
- it can corrupt memory
- it can create serious security problems
- it has been removed from modern C standards
Because of these reasons, you should not use gets() in real programs.
Safer Alternative to gets() in C
The safer alternative is fgets(), because it allows you to specify the maximum number of characters to read.
#include <stdio.h>
int main(void)
{
char name[20];
fgets(name, sizeof(name), stdin);
puts(name);
return 0;
}This is much safer because the function knows the buffer size.
puts() Function in C
puts() is used to display a string on the output screen. It automatically adds a newline at the end.
Syntax:
puts(str);Example:
#include <stdio.h>
int main(void)
{
char message[] = "Hello from C";
puts(message);
return 0;
}The output will be:
Hello from CSince puts() adds a newline automatically, you do not need to include \n inside the string just to move to the next line.
Difference Between puts() and printf() in C
| Point | puts() | printf() |
|---|---|---|
| Works with | String only | Formatted output |
| Adds newline automatically | Yes | No |
| Format specifiers | No | Yes |
| Best use | Simple string output | Detailed formatted output |
If you only need to print a plain string, puts() is simple and readable. If you need formatting, use printf().
Difference Between gets() and fgets() in C
| Point | gets() | fgets() |
|---|---|---|
| Checks buffer size | No | Yes |
| Safe for input | No | Much safer |
| Modern use | Avoid | Recommended |
| Standard status | Removed from modern C | Still valid |
Practical Use of puts() in C
- printing menu headings
- displaying plain messages
- showing string data without format specifiers
- making simple output code more readable
So while gets() is obsolete and unsafe, puts() is still a useful simple output function.
Common Mistakes with gets and puts in C
- using
gets()in modern code - expecting
puts()to support format specifiers - forgetting that
puts()adds a newline automatically - assuming old textbook code is still safe today
- confusing
gets()withfgets()
| Mistake | Problem | Better Practice |
|---|---|---|
Using gets() | Buffer overflow risk | Use fgets() |
Using puts("Value = %d") | No formatting support | Use printf() for formatting |
Adding extra newline after puts() | Unexpected blank lines | Remember puts() already prints newline |
Best Practices for gets and puts in C
- Do not use
gets()in modern C code. - Use
fgets()for string input instead ofgets(). - Use
puts()only when plain string output is enough. - Use
printf()when formatting is needed. - Prefer modern safe input functions in all real programs.
FAQs
What are gets and puts in C?
gets() and puts() are standard input/output functions used for string input and output, but gets() is unsafe and obsolete.
Why is gets() unsafe in C?
Because it does not limit how many characters are read, which can overflow the destination array.
What should be used instead of gets() in C?
You should use fgets() instead of gets().
What does puts() do in C?
puts() prints a string and automatically adds a newline at the end.
What is the difference between puts() and printf() in C?
puts() prints a plain string, while printf() supports formatted output.
Is gets() available in modern C?
It may appear in very old or nonstandard environments, but it has been removed from modern C standards and should not be used.