MrHappyAsthma
MrHappyAsthma

Reputation: 6522

Read no more than size of string with scanf()

Edit: for my class I have to use scanf. So recommending other ways of input is not the solution I am looking for (if there is one that involves scanf).


If I am reading in user input for a small project (for example, a game). Lets say I ask would you like to play? This would accept a yes or no answer. So i write up some simple code like this:

#include <stdio.h>

int main(void)
{
     char string[3]; //The max number of letters for "yes".

     printf("Would you like to play?");
     scanf("%s", string);
}

So this code should simply ask them to input yes or no. I am setting the length of my char array to size 3. This way it is large enough to hold yes and also no. But if someone were to enter invalid input such as yesss, I know how to compare the string afterwards to handle such an event, but wouldn't this technically/possibly overwrite other local variables I have declared because it would extend outside the length of my array? If so, is there a way to handle this to restrict 3 input characters or something? And if not, why/how does it know to only input for the size of 3?

Upvotes: 62

Views: 108666

Answers (5)

Mac
Mac

Reputation: 357

scanf's %s does not take a variable width specifier, as printf does. No matter, you get to do it inline. Something like:

#include <stdio.h>

int main(void)
{
   char format[20];
   char string[5];
   printf("Input a string, we capture %ld of them: ", sizeof(string));
   snprintf(format,sizeof(format),"%%%lds",sizeof(string));
   printf("\nAbout run : \'scanf(%s,&string)\'\n", format);
   int code = scanf(format,&string);
   printf("code is %d Got -> \'%s\'\n", code, string);
}

Running this:

% gcc foo.c -o foo
% ./foo
Input a string, we will capture 5 of them: 
About run : 'scanf(%5s,&string)'
abcdefghijk
code is 1 Got -> 'abcde'

Upvotes: 0

Timothy Malche
Timothy Malche

Reputation: 231

the safest way is to use

fgets(string, 4, stdin);

here you can store maximum 3 characters including one space reserved for NULL ('\0') character.

Upvotes: 23

gsamaras
gsamaras

Reputation: 73366

You should use the width modifier of scanf() and set it to be one less than the size of your string, so that you ensure that space exists for the NULL terminator.

So, if you want to store "yes", you will firstly need a bigger array than the one you have; one with size 4, 3 characters plus 1 for the null terminator. Moreover, you should instruct scanf() to read no more than size - 1 characters, where size is the length of your array, thus 3 in this case, like this:

#include <stdio.h>

int main(void)
{
     char string[4];
     scanf("%3s", string);
}

Upvotes: 10

anonymous
anonymous

Reputation: 51

http://www.cplusplus.com/reference/clibrary/cstdio/scanf/

use the "width" modifier;

%[*][width][modifiers]type

Upvotes: 5

Daniel Fischer
Daniel Fischer

Reputation: 183888

Your array needs to be able to hold four chars, 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.

Upvotes: 79

Related Questions