Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Input Functions for Strings | Creating and Storing Strings
Working with Strings in C

bookInput Functions for Strings

Note
Definition

Definition:
Input functions for strings in C are used to read sequences of characters from the user. Two common input functions are scanf and fgets. scanf reads formatted input from standard input, while fgets reads a line of text from a file or standard input into a character array, including whitespace characters and stopping at a newline or after a specified number of characters.

When you need to get a string from the user in C, you can use either scanf or fgets, but they behave differently and have different safety profiles. scanf reads input according to a format string, and when used with %s, it reads a sequence of non-whitespace characters and stops at the first whitespace. This means scanf cannot read strings containing spaces, such as full names or sentences, and it does not automatically prevent writing past the end of your character array. fgets, on the other hand, reads an entire line of input, including spaces, up to a specified maximum length. This makes fgets more flexible and helps prevent reading more characters than your array can hold.

main.c

main.c

copy
123456789
#include <stdio.h> int main() { char name[20]; printf("Enter your name: "); scanf("%s", name); printf("Hello, %s!\n", name); return 0; }

Using scanf with %s can be risky because it does not limit the number of characters read. If the user enters more characters than the array can hold, the extra input will overflow into adjacent memory, which can corrupt data or cause your program to crash. This is called a buffer overflow, and it is a common security vulnerability in C programs.

main.c

main.c

copy
123456789
#include <stdio.h> int main() { char name[20]; printf("Enter your name: "); fgets(name, sizeof(name), stdin); printf("Hello, %s", name); return 0; }

Because fgets allows you to specify the maximum number of characters to read, it helps prevent buffer overflows. fgets also reads spaces and stops only when it reaches the specified limit or a newline, making it much safer and more suitable for reading most user input in C. For these reasons, you should prefer fgets over scanf when reading strings from the user.

question mark

Which input function is generally safer for reading strings in C?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 4

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you show an example of using fgets to read a string in C?

What are some common mistakes to avoid when using fgets?

How can I safely remove the newline character that fgets includes?

bookInput Functions for Strings

Sveip for å vise menyen

Note
Definition

Definition:
Input functions for strings in C are used to read sequences of characters from the user. Two common input functions are scanf and fgets. scanf reads formatted input from standard input, while fgets reads a line of text from a file or standard input into a character array, including whitespace characters and stopping at a newline or after a specified number of characters.

When you need to get a string from the user in C, you can use either scanf or fgets, but they behave differently and have different safety profiles. scanf reads input according to a format string, and when used with %s, it reads a sequence of non-whitespace characters and stops at the first whitespace. This means scanf cannot read strings containing spaces, such as full names or sentences, and it does not automatically prevent writing past the end of your character array. fgets, on the other hand, reads an entire line of input, including spaces, up to a specified maximum length. This makes fgets more flexible and helps prevent reading more characters than your array can hold.

main.c

main.c

copy
123456789
#include <stdio.h> int main() { char name[20]; printf("Enter your name: "); scanf("%s", name); printf("Hello, %s!\n", name); return 0; }

Using scanf with %s can be risky because it does not limit the number of characters read. If the user enters more characters than the array can hold, the extra input will overflow into adjacent memory, which can corrupt data or cause your program to crash. This is called a buffer overflow, and it is a common security vulnerability in C programs.

main.c

main.c

copy
123456789
#include <stdio.h> int main() { char name[20]; printf("Enter your name: "); fgets(name, sizeof(name), stdin); printf("Hello, %s", name); return 0; }

Because fgets allows you to specify the maximum number of characters to read, it helps prevent buffer overflows. fgets also reads spaces and stops only when it reaches the specified limit or a newline, making it much safer and more suitable for reading most user input in C. For these reasons, you should prefer fgets over scanf when reading strings from the user.

question mark

Which input function is generally safer for reading strings in C?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 4
some-alt