0

The whole function the question is about is about giving a two dimensional array initialized with {0} as output and making a user able to move a 1 over the field with

char wasd;
scanf("%c", &wasd);

(the function to move by changing the value of the variable wasd is not important i think) now my question is why using

scanf("%s", &wasd);

does only work partly(sometimes the 1 keeps being at a field and appears a 2nd time at the new place though it actually should be deleted) and

scanf("%.1s", &wasd);

leads to the field being printed out without stop until closing the execution program. I came up with using %.1s after researching the difference between %c and %s here Why does C's printf format string have both %c and %s?? If one can figure out the answer by reading through that, i am not clever or far enough with c learning to get it. I also found this fscanf() in C - difference between %s and %c but i do not know anything about EOF which one answer says is the cause of the problem so i would prefer getting an answer without it. Thank you for an answer

Gottaquest
  • 111
  • 6

2 Answers2

2

Simple as that, %s is the conversion for a (non-empty) string. A string in C always ends with a 0 byte, so any non-empty string needs at least two bytes. If you pass a pointer to a single char variable, scanf() will just overwrite whatever is in memory after that variable -- you cause undefined behavior and anything can happen.


Side note, scanf("%s", ..), even if you give it an array of char, will always overflow the buffer if something longer is entered, therefore causing undefined behavior. You have to include a field width like

char str[10];
scanf("%9s", str);

Best is not to use scanf() at all. For your single character input, you can just use getchar() (be aware it returns an int). You might also want to read my beginners' guide away from scanf.

  • thanks again for an answer. i read of getchar but tried to get used to scanf first. But i will check out the link when learning about getchar. If you pass a pointer to a single char variable, - but when using %s i pass it to a char array..dont i? – Gottaquest May 02 '18 at 17:17
  • See the simple example code with `%9s` above. Field width must be one less than array size because the final `0` byte of the string isn't counted in the field width... –  May 02 '18 at 17:23
  • I used `char wasd[3]; scanf("%c", &wasd[2]);`now. Why is the field width still to short when using `char wasd[2]; scanf("%c", &wasd[1]);` ? The 0 needs only one byte..+ the byte of the value of wasd – Gottaquest May 02 '18 at 17:43
  • Huh? Now I don't understand anything. Why are you now combining `%c` with an array of `char`? With `%c` there's no field width -- the field width in my example is the `9` in `%9s`. –  May 02 '18 at 18:12
2

A char variable can hold only one byte of memory to hold a single character. But a string (array of characters) is different from a char variable as it is always ended with a null character \0 or numeric 0. So in scanf you specifically mentioned whether you are reading a character or a string so that scanf can add a null character at the end of a string. So you are not suppose to use a %s to read a value for a char variable

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17