+ 1
I think it should take only 5 character as input, but it's taking more than 5 character,, why it's happening ?
#include<stdio.h> int main() { char a[5]; scanf("%s",&a); printf("%s",a); return 0; }
5 ответов
+ 2
S.M. Rakibul Alam even if you should enter only 5 (non-whitespace) characters, scanf will write out of bounds of the 5-char array. It would write a terminating null in the 6th character position, past the end of the string. Always allow at least one more char for storing the null.
Please note the warning from the compiler. Passing &a is passing the pointer to a, which is already a pointer due to being declared an array. The & is unnecessary and may overwrite memory somewhere other than a.
0
Mirielle So you are saying that,, because of using %s the code will take input until the memory bites are full...
0
Mirielle OK,, I'll check that.