0

Explain the bug

Assuming user enters "hello world"output is hello 0

c

4/1/2020 9:23:08 PM

Ajith

4 Answers

New Answer

+3

In this case scanf reads 2 inputs, a string (%s) and an int (%d). "hello world" are 2 inputs because of the space, you basically provide it with 2 %s. When scanf reads the 2nd input, "world" it expects it to be an int, yet it encounters an invalid character, 'w', so it just aborts. The final value can be anything because it's undefined behavior.

0

char str[100]; int i; scanf("%s %d",str,&i); printf("%s %d",str,i);

0

(Silly me - I for some reason saw two printf statements.)

0

Thanks