Your array needs to be able to hold four char
s, since it must also contain the 0-terminator. With that fixed, specifying a maximal length in the format,
scanf("%3s", string);
ensures that scanf
reads no more than 3 characters.
More Related Contents:
- isnt everything where it should be, why the segmentation fault?
- ‘ ‘, ‘\n’ , scanf() and output screen
- How do you allow spaces to be entered using scanf?
- How to use sscanf in loops?
- When should I use ampersand with scanf()
- Reading string from input with space character? [duplicate]
- How to read string from keyboard using C?
- Why doesn’t scanf need an ampersand for strings and also works fine in printf (in C)?
- Reading a string with spaces with sscanf
- What does `scanf(“%*[^\n]%*c”)` mean?
- Whitespace before %c specification in the format specifier of scanf function in C
- Why scanf(“%d”, […]) does not consume ‘\n’? while scanf(“%c”) does?
- Why is adding a leading space in a scanf format string recommended?
- Format specifier in scanf for bool datatype in C
- Strange character after an array of characters
- pointer to string concept in c [closed]
- Why is this string reversal C code causing a segmentation fault? [duplicate]
- C: Multiple scanf’s, when I enter in a value for one scanf it skips the second scanf [duplicate]
- How do I create an array of strings in C?
- How to convert enum names to string in c
- printf not printing on console
- Get scanf to quit when it reads a newline?
- C – trying to read a single char from stdin (and failing) w/ scanf / getchar
- scanf(“%c”) call seems to be skipped
- Why does char* cause undefined behaviour while char[] doesn’t?
- Concatenating strings in C, which method is more efficient?
- Does C support raw string literals?
- equivalent of Console.ReadLine() in c++
- A possible algorithm for determining whether two strings are anagrams of one another? [closed]
- Scan multiple integers without knowing the actual number of integers