How do I properly 'printf' an integer and a string in C?

后端 未结 3 948
遥遥无期
遥遥无期 2021-01-03 19:30

I have the following code:

char *s1, *s2;
char str[10];

printf(\"Type a string: \");
scanf(\"%s\", str);

s1 = &str[0];
s2 = &str[2];

printf(\"%s\\         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-03 19:54

    scanf("%s",str) scans only until it finds a whitespace character. With the input "A 1", it will scan only the first character, hence s2 points at the garbage that happened to be in str, since that array wasn't initialised.

提交回复
热议问题