+ 1

Why print 8 value when input char for %d

#include <stdio.h> int main() { int a, b; printf("Enter two numbers:"); scanf("%d %d", &a, &b); printf("\nSum: %d", a+b); return 0; } Input - c h Output - 8

3rd Apr 2019, 11:16 AM
Manchanayakage Shalika Prasad
Manchanayakage Shalika Prasad - avatar
1 ответ
+ 4
That's a garbage value. a and b are integers and scanf("%d") will look for integers, not characters. If you enter characters when the program expects integers, it's like you enter nothing at all. So it will just print the sum of the two values that were in memory where a and b are stored now (they are garbage values since you didn't initialize them)
3rd Apr 2019, 11:27 AM
Anna
Anna - avatar