-2
#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);
}
Tomer W
  • 3,395
  • 2
  • 29
  • 44

3 Answers3

2

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);
}
ozerodb
  • 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
0

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);
}
user3386109
  • 34,287
  • 7
  • 49
  • 68
Ganesh Jadhav
  • 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
0

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:

  1. Always check the return value of the scanf
  2. Your main definition is invalid. If main does not take any parameters is has to be int main(void)
0___________
  • 60,014
  • 4
  • 34
  • 74