+ 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; }

29th Oct 2022, 3:48 PM
S M Rakibul Alam
S M Rakibul Alam - avatar
5 Antworten
+ 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.
29th Oct 2022, 6:58 PM
Brian
Brian - avatar
0
Mirielle So you are saying that,, because of using %s the code will take input until the memory bites are full...
29th Oct 2022, 4:46 PM
S M Rakibul Alam
S M Rakibul Alam - avatar
0
Mirielle OK,, I'll check that.
30th Oct 2022, 12:03 AM
S M Rakibul Alam
S M Rakibul Alam - avatar