Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте String Input and Output | String Initialization and Input/Output
Practice
Projects
Quizzes & Challenges
Вікторини
Challenges
/
C Strings

bookString Input and Output

Свайпніть щоб показати меню

When you work with strings in C, you will often need to read input from users and print output to the screen. Several standard C functions are available for these tasks. For output, printf and puts are commonly used. printf is versatile, allowing you to print formatted strings and variables, while puts simply prints a string followed by a newline. For input, scanf and gets are traditional choices, but each comes with important considerations. scanf can read formatted input, but when used with the %s specifier, it stops reading at the first whitespace and does not prevent buffer overflows unless you specify a maximum width. gets reads an entire line but is unsafe because it does not check the size of the buffer, which can lead to security vulnerabilities. Always be cautious and consider safer alternatives.

main.c

main.c

copy
123456789101112
#include <stdio.h> int main() { char name[50]; printf("Enter your name: "); scanf("%49s", name); // Limit input to 49 characters to avoid overflow printf("Hello, %s!\n", name); return 0; }

In the code above, you prompt the user to enter their name, read it into a character array using scanf, and then print a greeting using printf. Notice the use of %49s in the scanf format string. This restricts the input to 49 characters, leaving room for the null terminator, and helps prevent buffer overflows. However, there are still pitfalls:

  • scanf with %s stops reading at the first whitespace, so if the user enters a name with spaces, only the first word is captured;
  • Without specifying a width, you risk overwriting memory if the user enters a longer string than your buffer can hold;
  • Using gets would be even riskier, as it does not check buffer length at all.

Always use width specifiers with scanf when reading strings, and avoid gets to keep your programs safe from memory errors.

main.c

main.c

copy
123456789101112
#include <stdio.h> int main() { char name[50]; printf("Enter your full name: "); if (fgets(name, sizeof(name), stdin) != NULL) { printf("Hello, %s", name); } return 0; }

fgets is considered a safer option for reading strings in C. It allows you to specify the maximum number of characters to read, which helps prevent buffer overflows. In the example above, fgets reads up to 49 characters (leaving space for the null terminator) and can capture spaces in the input, making it suitable for full names or sentences. Unlike gets or scanf with %s, fgets helps ensure that your program does not write past the end of the buffer, making it the preferred function for safe string input.

question mark

Which function is considered safest for reading strings from input in C?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Секція 2. Розділ 2
some-alt