Greetings, today, when I try something new (a concept and style that suddenly appear in my mind), I came across several problems that I don’t understand why this happened.
The code
#include <stdio.h>
int main(void)
{
int fNum;
int sNum;
int status = 1;
int operator;
int ans;
printf("Enter the first operand :");
while(scanf("%d" , &fNum) == 0)
;
printf("Please enter the second operand :");
while(scanf("%d" , &sNum) == 0)
;
printf("Which operator you wanted to use\n");
printf("1 for +\n2 for -\n");
while(status)
{
scanf("%d" , &operator);
status = 0;
switch(operator)
{
case 1:
ans = fNum + sNum;
break;
case 2:
ans = fNum - sNum;
break;
default:
status = 1;
}
}
printf("The answer is %d\n" , ans);
return 0;
}
My analysis and question
First part:
1.) There is one thing that I don’t understand when I try to run this code, I get a warning from the compiler, "C: \ Users \ Znet \ Documents \ Pelles C Projects \ Test1 \ Test.c (10): warning # 2229: local "ans" are potentially used without initialization. , But of course I can still run the program since this is not an error message.
2.) , , , ans, (0 ), ? , ans switch, ? ( ) , , .
:
1.) switch, 1, 2 .
2.) 1 2, , , , .
3.) , , , , , , , . , , .
4.) , , , ?
, :)