-3
char num1, num2;
scanf("%s %s", &num1, &num2);
printf("num1=%c, num2=%c", num1, num2);

I executed above code on Visual Studio in Mac with following cases:

Input: a b   => num1=, num2=b
Input: ab c  => num1=, num2=c
Input: a bc  => num1=c, num2=b
Input: ab cd => num1=d, num2=c

I absolutely know the %s should be %c, but I want to know why the first argument, char num1, cannot be displayed correctly in %s.

Moreover, the value of num2 precedes num1 if we input "a bc" or "ab cd". That looks confusing.

What's the reason or mechanism in C causes these strange output? Is the result the same on your computer? I think it is more important to know the reason rather than convention.

skyline
  • 443
  • 9
  • 31
  • 1
    Because your code is absolutely wrong. %s reads a string and stores input + the terminating null character. How on earth is a char supposed to store two different things? – Tanveer Badar Aug 02 '19 at 10:55
  • The variable `char num1` cannot be displayed with `printf` using the `%s` format because it requires a nul-terminated string, but you only have a single `char`. You say *I know the `%s` should be `%c`* so why aren't you using it correctly? It's thoroughly unclear what you are trying to do. – Weather Vane Aug 02 '19 at 10:55
  • Looks like strings are stored @ start addresses of chars and overwritten by each other ;-) Stack will be a bit messed up after... – Jan Aug 02 '19 at 11:03
  • In windows debug version Run-Time Check Failure #2 - Stack around the variable 'numX' was corrupted. A lot platform specific, but why RU trying to analyze code bug ?? – Jan Aug 02 '19 at 11:15
  • I can accept the former 2 cases, but the latter 2 cases is confusing to me. I think the value of num1 should precede num2, but it didn't. I am curious about that situation. – skyline Aug 02 '19 at 14:54

1 Answers1

0

The program has undefined behavior.

The format specifier %s appends a zero character to the memory occupied by the corresponding argument.

Nevertheless I can explain the program output.

It seems that in the stack the variable num1 follows the variable num2.

So when the input is

nput: a b

then to the address of the variable num2 there is written two characters 'b' and '\0'; The character '\0' occupies the memory of the variable num1.

So the output is

num1=, num2=b

When the input is

Input: ab c

when again the terminating zero is written in the memory occupied by the variable num1.

So you get the same output as above

num1=, num2=c

When the input is

Input: a bc

then in memory addressed by &num2 there are written three characters: 'b', 'c' and '\0'. So the character 'b' is placed in the variable num2, the character 'c' is placed in the variable num1 and the terminating zero overwrites memory beyond the memory allocated for the variables.

So the output is

num1=c, num2=b

In the last case when the input is

nput: ab cd

then the output will be

num1=d, num2=c

as it is described above.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335