Text editor : VS code
Compiler : minGW
I was needed to store a number between 1-10, so I thought why don't store it in a char variable because it takes only 1 byte.
so, look at the following code
char a = 3;
printf("%d", a);
OUTPUT
3
it worked fine
Then I decided to take value as input using scanf
Here's the code for that
char a;
printf("Enter number : ");
scanf("%d", &a);
printf("You entered : %d", a);
OUTPUT
Enter number : 9
You entered : 9
It worked fine too, but problem lies when I tried with two variables
Look at the the code below
char a = 2, b = 3;
printf("before scanf a : %d\n", a);
printf("before scanf b : %d\n", b);
printf("Enter new value of a : ");
scanf("%d", &a);
printf("after scanf a : %d\n", a);
printf("after scanf b : %d\n", b);
OUTPUT
before scanf a : 2
before scanf b : 3
Enter new value of a : 9
after scanf a : 9
after scanf b : 0 //Notice value of b has changed to 0
Even though, I didn't even touched b's value. still it changed. . I tried a lot to figure it out but failed, I'm a beginner. I think it's something to do with stdin/out stream. please help me here..