#include <stdio.h>
#include <conio.h>
void Calculator();
void main()
{
Calculator();
getch();
}
void Calculator()
{
int n,j;
char f1;
double t;
printf("please enter two numbers");
scanf("%d%d",&n,&j);
printf("please enter the syboml you want ( * / + or -)");
scanf("%c",&f1);
if( f1 == '+')
t = n + j;
if (f1 == '-')
t = n-j;
if (f1 == '*')
t = n*j;
if (f1 == '/')
t = n/j;
printf("%f" ,t);
}

- 3,395
- 2
- 29
- 44

- 1
- 1
-
on what line did you get the error, used a debugger? – Tomer W May 29 '21 at 20:26
-
1You don't always initialize `t`. Also, `void main()` should be `int main(void)`, and `
` is not portable. – Keith Thompson May 29 '21 at 20:26 -
What will t be if the user enters a '$'? – BenM May 29 '21 at 20:26
-
1You should also use `scanf(" %c", &f1)`. Otherwise you'll set `f1` to the newline character after the two numbers. See https://stackoverflow.com/questions/20306659/the-program-doesnt-stop-on-scanfc-ch-line-why – Barmar May 29 '21 at 20:27
-
Use a debugger and learn how to test your code. – TomServo May 29 '21 at 20:42
-
@TomerW after I entered n and j I got the erorr without even getting the chance to enter f1 – user16071647 May 29 '21 at 20:43
-
@KeithThompson conio never caused me an eror and why must it be int main(void) – user16071647 May 29 '21 at 20:44
-
@BenM I get the error even before I enter the symbol. right after I enter n and j – user16071647 May 29 '21 at 20:45
-
I will trying adding else tho I get the eror before entering the symbol for some reason – user16071647 May 29 '21 at 20:46
-
@Barmar I added the space, but now it always prints t = 0 – user16071647 May 29 '21 at 20:50
-
@Barmar never mind, I fixed that, this for the help – user16071647 May 29 '21 at 20:51
-
When you do division, you should convert one of the input variable to `float`. – Barmar May 29 '21 at 20:51
-
@Barmar float n; t= n/f – user16071647 May 29 '21 at 20:54
-
`t = (float)n/j;` – Barmar May 29 '21 at 20:56
-
@Barmar in an 11/3 division o get 3.000000 instead of 3.666667 – user16071647 May 29 '21 at 21:00
-
You get that error/warning because you do not init any of your variables. If you disagree please explain for each of your variables where you do init it. – Yunnosch May 29 '21 at 21:03
-
@Yunnosch the main erorr was I needed a space before %c so that \n does not be taken as input. – user16071647 May 29 '21 at 21:11
-
@user16071647 It works for me. Did you do `(float)(n/j)` instead of `(float)n/j`? – Barmar May 29 '21 at 21:11
-
@Barmar what I did exactly is t = float(n/j) – user16071647 May 29 '21 at 21:13
-
That's not even valid C, it's C++. – Barmar May 29 '21 at 21:24
-
But the point is that you have to convert one of the variables to `float` *before* you do the division. Otherwise you do integer division, which returns an integer, and then convert that result to float. – Barmar May 29 '21 at 21:25
-
`
` is specific to MS-DOS compilers, if I recall correctly. `int main(void)` is the form specified by the C standards, Compilers are permitted to support `void main()`, but they don't have to, and there's no good reason to use it. – Keith Thompson May 29 '21 at 22:45 -
@Barmar Kinda late but can one input affect the other? What i mean suppose i am inputing a sentence with spaces and all. Then asking for 2nd input. can my first input like the new line get in the first scanf – user16071647 Jul 12 '21 at 00:35
-
@user16071647 It depends on the specific formats you're using. Formats that are delimited by whitespace such as `%d` and `%s` will not consume the newline. – Barmar Jul 12 '21 at 16:07
3 Answers
In your final printf
, you are using t
that has never been initialized, and might hold a garbage value if no-one of those if
conditions is met.
Consider initializing t
(a simple = 0 does the job) or add an else
clause somewhere
Edit:
While I was at it, I also made some changes to make sure the second scanf
ignores the trailing /n
without using fflush
.
Edit 2:
As suggested by HAL9000, assuming that an initialization to 0 would be enough is wrong. I modified the second part of the program to make use of a switch-case and eventually reject an invalid operator.
The final code looks like this
#include <conio.h>
#include <stdio.h>
void Calculator();
int main() {
Calculator();
getch();
return 0;
}
void Calculator() {
int n, j;
char f1;
double t;
printf("please enter two numbers: ");
scanf("%d%d", &n, &j);
printf("please enter the symbol you want ( * / + or -): ");
scanf(" %c", &f1);
switch (f1) {
case '+':
t = n + j;
break;
case '-':
t = n - j;
break;
case '*':
t = n * j;
break;
case '/':
t = (float)n / j;
break;
default:
printf("Invalid symbol, please use ( * / + or -)\n");
return;
}
printf("%f\n", t);
}

- 543
- 3
- 13
-
What makes you think that `t=0` is less of a garbage value than the the pseudo-random number that is stored in `t` as default? By initializing `t` with a meaningless value, just to get rid of a warning, you are only hiding a bug, not fixing it. – HAL9000 May 29 '21 at 21:05
-
@HAL9000 I completely agree with you, I just wanted to point out to the OP the reason of the warning. If this was my code, I would add a check on the char passed, and eventually handle an incorrect char – ozerodb May 29 '21 at 21:08
-
@HAL9000 I know I know my bad, I added a little switch-case to handle the operator, this way you either input a valid operator or `t` is never used and the initialization doesn't matter – ozerodb May 29 '21 at 21:21
In some compilers, you might be getting this t is not initialized error as your code will never ask please enter the symbol you want ( * / + or -) because scanf("%c",&f1);
will take input as trailing newline char, so t never gets initialized. I ran your code in GCC compiler on mac but got output as 0.0000 as t will never be initialized in your case.
You can just eat up the trailing char for that you can use getchar();
or you can also put a space in the format string, e.g. scanf(" %c",&f1);
to consume the newline character.
void Calculator()
{
int n,j;
char f1;
double t;
printf("please enter two numbers");
scanf("%d%d",&n,&j);
printf("please enter the syboml you want ( * / + or -)");
scanf(" %c",&f1);
if( f1 == '+')
t = n + j;
if (f1 == '-')
t = n-j;
if (f1 == '*')
t = n*j;
if (f1 == '/')
t = n/j;
printf("%f" ,t);
}

- 34,287
- 7
- 49
- 68

- 76
- 1
- 6
-
1`fflush(stdin)` is undefined behavior for standard C, see https://stackoverflow.com/questions/2979209/using-fflushstdin. The portable way to achieve this is to actually read the newline character first. – Nate Eldredge May 29 '21 at 20:57
-
Thanks for pointing it out.@NateEldredge @user3386109 this way is mentioned in the question comment itself. But yeah this the better way for sure. – Ganesh Jadhav May 29 '21 at 21:02
Not every program path leads to the assignment of the t
variable. So it can be used in the printf
not initialized.
switch(f1)
{
case '+':
t = n + j;
break;
case '-':
break;
t = n-j;
case '*':
t = n*j;
break;
case '/':
t = n/j;
break;
default:
t = 0;
break;
}
Now t
will always will be assigned with the value.
Some additional remarks:
- Always check the return value of the
scanf
- Your
main
definition is invalid. If main does not take any parameters is has to beint main(void)

- 60,014
- 4
- 34
- 74