I am using gcc 4.9.2-10 deb8u1 compiler to compile Here is my code
#include <stdio.h>
int main(){
char *s;
char sa[10] , sb[10];
scanf("%s", sa);
printf("line\n");
scanf("%s", sb);
printf("%s %s", sa, sb);
}
Above code is no any problem if char is under the space provided
However
scanf("%s", s);
printf("line\n");
scanf("%s", sa);
printf("%s %s", s, sa);
Input: $: Hu
Result: line (null) Hu
Someone could told me what happen about second code wrong .? I cannot figure out why i cannt input second one .. Thx a lot .!
main()function.scanf()ing to an uninitialised pointer).scanf()family of functions: 1) always check the returned value (not the parameter values) to assure the operation was successful. 2) when using the input format specifier '%s' and/or '%[...]' always include a MAX CHARACTERS modifier that is one less than the length of the input buffer. This is for two reasons; a) those specifiers always append a NUL byte to the input b) to avoid any chance of a buffer overflow