C scanf String Input: %s and fgets
You read a string into a char array. scanf with %s reads only one word, up to the next space. To read a whole line that contains spaces, use fgets. Either way, you have to give a length so the input can't go past the end of the array.
scanf("%99s", s); /* one word */
fgets(s, sizeof s, stdin); /* one line */
Reading one word with %s
%s skips any leading spaces and newlines, then reads up to the next space or newline and puts a \0 at the end. An array name is already an address, so you don't write &. The 19 in %19s is the maximum number of characters to read. It is one less than the array size, which leaves one element for the \0.
#include <stdio.h> int main(void) { char first[20], last[20]; scanf("%19s", first); scanf("%19s", last); printf("first: %s\n", first); printf("last: %s\n", last); return 0;}Mina Kim
first: Mina last: Kim
The input is the single line Mina Kim, but the first scanf takes only Mina. The next scanf reads the Kim that is left.
Reading a line with fgets
fgets(array, size, stdin) reads one line, up to the newline. Because you tell it the size, it doesn't go past the end of the array. But the newline \n at the end of the line is stored in the array too. To remove it, find its position with strcspn and put a \0 there.
#include <stdio.h>#include <string.h> int main(void) { char line[100]; if (fgets(line, sizeof line, stdin) != NULL) { line[strcspn(line, "\n")] = '\0'; printf("[%s]\n", line); printf("%d\n", (int)strlen(line)); } return 0;}Hello C world
[Hello C world] 13
fgets returns NULL when there is nothing to read (end of input). If the line is longer than the array, it reads as much as fits and leaves the rest for the next read.
Reading a line after a number
Input often has a count on the first line and lines of text below it. If you mix scanf("%d") and fgets, the newline left after the number gets in the way (see the common mistakes below). The safest way is to read every line with fgets and pull the number out of its line with sscanf.
#include <stdio.h>#include <string.h> int main(void) { char buf[100], line[100]; int n = 0; if (fgets(buf, sizeof buf, stdin) == NULL) return 0; sscanf(buf, "%d", &n); for (int i = 1; i <= n; i++) { if (fgets(line, sizeof line, stdin) == NULL) break; line[strcspn(line, "\n")] = '\0'; printf("%d: %s\n", i, line); } return 0;}2 red apple green tea
1: red apple 2: green tea
Reading a line with scanf: %[^\n]
%[^\n] means keep reading characters that are not a newline, so scanf alone can read a line with spaces in it. Here too, give a maximum number of characters, as in %99[^\n], and put a space in front to skip the newline left over from the previous line.
#include <stdio.h> int main(void) { int n; char title[100]; scanf("%d", &n); scanf(" %99[^\n]", title); printf("%d x [%s]\n", n, title); return 0;}3 The C Book
3 x [The C Book]
It doesn't read the newline; it leaves it in the input. Because of the space in front, spaces at the start of the line are thrown away too, and an empty line is skipped instead of read. If you need empty lines or leading spaces exactly as they are, use fgets.
Common mistakes
fgets after scanf reads an empty line
#include <stdio.h>
#include <string.h>
int main(void) {
int n;
char line[100] = "";
scanf("%d", &n);
fgets(line, sizeof line, stdin);
line[strcspn(line, "\n")] = '\0';
printf("%d [%s]\n", n, line);
return 0;
}
→ 3 [] instead of 3 [hello world]
%d reads only the number and leaves the newline. fgets treats everything up to that newline as one line and stops right away. Read the number with fgets and sscanf too, or use a format that skips the leading newline, such as scanf(" %99[^\n]", line).
Using %s without a length
char name[8];
scanf("%s", name); /* what if 8 or more characters come in? */
→ If a word longer than the array comes in, scanf writes past the end of the array. This is undefined behavior: another variable may get corrupted, or the program may crash
Write the array size - 1, as in scanf("%7s", name);. It reads only that many characters and leaves the rest in the input.
Not removing the newline from a line read with fgets
#include <stdio.h>
#include <string.h>
int main(void) {
char word[100] = "";
fgets(word, sizeof word, stdin);
printf("%d\n", (int)strlen(word));
printf("%d\n", strcmp(word, "hello") == 0);
return 0;
}
→ You entered hello, but the length is 6 and the comparison gives 0 (not equal)
That's because the array holds hello\n. Remove the newline right after reading, with word[strcspn(word, "\n")] = '\0';.