Understanding of gets() & puts() in C Language
Hello, C programmers! In this blog post, I’m going to explain two very useful
functions in C language: gets() and puts(). These functions are used to read and write strings from the standard input and output devices, such as the keyboard and the screen. Let’s see how they work and why they are important.What is a gets() & puts() in C Language?
gets() and puts() are C standard library functions used for input and output of strings. However, it’s important to note that gets() is considered unsafe and is not recommended for use in modern C programming due to its vulnerability to buffer overflows. Instead, you should use safer alternatives like fgets() for string input. Nonetheless, I will provide information about both functions for reference.
gets() Function in C Language:
- Purpose: The
gets()function is used for reading a line of text from the standard input (usually the keyboard) and storing it as a string. It reads characters until a newline character (‘\n’) or the end of the file is encountered and stores them in the provided character array. The newline character is replaced with a null terminator (‘\0’) to terminate the string. - Declaration:
char *gets(char *str); - Example:
#include <stdio.h> int main() { char buffer[100]; printf("Enter a string: "); gets(buffer); // Reads a line of text from the user printf("You entered: %s\n", buffer); return 0; }Note: The use ofgets()is discouraged because it does not perform any bounds checking, making it susceptible to buffer overflows if the input string is longer than the size of the buffer. It is strongly recommended to usefgets()instead, which allows you to specify the maximum number of characters to read and provides better safety.
puts() Function in C Language:
- Purpose: The
puts()function is used for writing a string to the standard output (usually the console). It takes a null-terminated string as an argument and appends a newline character (‘\n’) to the output. It then writes the string and the newline character to the standard output. - Declaration:
int puts(const char *str); - Example:
#include <stdio.h> int main() { char greeting[] = "Hello, World!"; puts(greeting); // Writes "Hello, World!" to the console followed by a newline return 0; } - Return Value: The
puts()function returns a non-negative value if successful, or it returnsEOF(end of file) if an error occurs. Note: Unlikeprintf(),puts()does not provide formatting options; it simply writes the entire string followed by a newline character to the output.
Examples of gets() & puts() in C Languages?
Certainly, here are examples of using the gets() and puts() functions in C. While I provide examples for gets(), please be aware that it is considered unsafe due to the absence of buffer size checking, and it is strongly recommended to use fgets() for safe string input in modern C programming.
- Example Using
gets()(Unsafe):
#include <stdio.h>
int main() {
char name[50];
printf("Enter your name: ");
gets(name); // Unsafe! No buffer size checking.
printf("Hello, %s!\n", name);
return 0;
}
Note: The gets() function reads input from the user without checking the size of the input, making it susceptible to buffer overflows. In practice, it should be avoided in favor of safer alternatives like fgets().
- Example Using
puts():
#include <stdio.h>
int main() {
char greeting[] = "Hello, World!";
puts(greeting); // Outputs "Hello, World!" followed by a newline.
return 0;
}
The puts() function is used here to output the “Hello, World!” string, and it automatically appends a newline character to the output.
Advantages of gets() & puts() in C Languages
The gets() and puts() functions in C have some advantages in specific situations, but they are limited in their use cases and are generally considered outdated due to significant security and safety concerns. Here are some potential advantages, although these should be weighed against their significant disadvantages:
Advantages of gets():
- Simplicity: The
gets()function is straightforward to use, making it suitable for simple programs where quick user input is needed.
Advantages of puts():
- Simplicity: Like
gets(),puts()is easy to use and is useful for simple programs where you want to output a string followed by a newline character. - Automatic Newline:
puts()automatically appends a newline character to the output, making it convenient for printing lines of text with line breaks.
It’s important to emphasize that while these functions have some simplicity advantages, their use is generally discouraged for several critical reasons:
- Unsafe Input with
gets():gets()does not perform bounds checking on input, making it highly susceptible to buffer overflows, which can lead to security vulnerabilities. For safe string input, it is strongly recommended to usefgets()instead. - Limited Formatting: Both
gets()andputs()lack formatting capabilities, which means you cannot easily format or manipulate the content you read or write. If you need to format or manipulate strings, functions likescanf(),printf(), or string manipulation functions from<string.h>are more appropriate. - Deprecated: In modern C programming,
gets()is deprecated, and its use should be avoided entirely due to its security risks. Instead, you should use safer alternatives likefgets()for string input. - Limited Functionality: These functions are limited in functionality compared to other input/output functions available in C. For more complex input and output operations, other functions offer greater flexibility and safety.
Disadvantages of gets() & puts() in C Languages
The gets() and puts() functions in C have significant disadvantages and limitations, which have led to their discouraged use in modern C programming. These functions pose serious security and safety risks, which should be carefully considered. Here are the primary disadvantages of gets() and puts():
Disadvantages of gets():
- Buffer Overflows: The most critical disadvantage of
gets()is its susceptibility to buffer overflows. It does not perform any bounds checking on the input, which means that if the input string is longer than the allocated buffer, it can overwrite memory beyond the buffer’s boundaries. This is a severe security vulnerability. - No Input Length Limit:
gets()will continue reading input until a newline character or the end of the file is encountered. There’s no way to limit the number of characters read, which can lead to unpredictable behavior and memory corruption. - No Safe Alternative: There is no safe way to use
gets()in C. It is recommended to avoid it entirely and use safer alternatives likefgets()for string input.
Disadvantages of puts():
- Limited Functionality:
puts()is very basic and has limited functionality. It can only be used to output strings with a newline character at the end. It cannot be used for more complex formatted output. - No Control Over Newline: While
puts()automatically appends a newline character to the output, it does not provide a way to control or customize the line ending. This can be a limitation in certain situations. - Lack of Formatting:
puts()does not support formatting options likeprintf(), which allows you to format output with placeholders and various formatting directives. If you need to format your output, you cannot do so withputs(). - No Error Reporting:
puts()does not provide a means of reporting errors. While it is rare forputs()to fail, other output functions likeprintf()orfwrite()provide error reporting facilities.
Discover more from PiEmbSysTech - Embedded Systems & VLSI Lab
Subscribe to get the latest posts sent to your email.



