C puts() and gets()
In this page:
puts("text"); // adds a newline automatically
fgets(string_name, size, stdin); // safe line input
What is puts()?
puts() prints a string followed automatically by a newline, making it a slightly simpler alternative to printf when you just need to display fixed text without any variable substitution or formatting.
Example: What is puts()?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
puts("Hello, World!");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Using puts() vs. printf()
puts() is easier to use than printf for plain text output since it takes no format specifiers, but it also can't interpolate variable values into the middle of a string the way printf's %d, %s, and similar placeholders can.
Example: Using puts() vs. printf()
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
puts("Fixed text, no formatting");
// Print formatted text to the screen
printf("Age: %d\n", 25);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Reading Text safely with fgets()
gets() is deprecated and removed from modern C standards because it has no way to limit how much input it reads, letting a user's input overflow the buffer and corrupt adjacent memory -- fgets() replaces it safely by taking a maximum length.
Example: Reading Text safely with fgets()
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include string functions (strlen, strcpy, ...)
#include <string.h>
// Program execution starts in main()
int main() {
char buffer[20] = "Sample text\n";
// Print formatted text to the screen
printf("%s", buffer);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Understanding Gets and Buffer Sizes
Unlike fgets(), the old gets() function never checks whether the destination buffer is large enough for the input being typed, which is exactly the security flaw that makes it unsafe to use in any real program.
Example: Understanding Gets and Buffer Sizes
#include <stdio.h>
int main() {
char buffer[10];
// gets(buffer) would not check if input fits in buffer -- unsafe
printf("Use fgets(buffer, sizeof(buffer), stdin) instead");
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Cleaning Newlines from Input
fgets() includes the trailing newline character in the buffer it reads, unlike gets() which discards it -- if your code compares that string against something without a newline, you'll need to strip it first, often with strcspn or manual indexing.
Example: Cleaning Newlines from Input
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include string functions (strlen, strcpy, ...)
#include <string.h>
// Program execution starts in main()
int main() {
char buffer[20] = "Hello\n";
buffer[strcspn(buffer, "\n")] = '\0';
// Print formatted text to the screen
printf("[%s]", buffer);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using
gets(), which has no size limit and can overflow the buffer; it was removed in C11. - Assuming
putsbehaves exactly likeprintf, when it always adds a newline. - Using
fgetsand forgetting that it keeps the trailing newline in the buffer.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: