1. ## sum problem

Code:
```#include <stdio.h>
main()
{
int a,b,c;
c=a+b;
printf("give first number:");
scanf("%d",&a);
printf("give second number:");
scanf("%d",&b);
printf("the result is:");
printf("%d",c);
}```
but when i give for first number 4 and for second number 7 my result is not 11.
the problem is propably the last printf.what is the correct syntax for valid result?? 2. The order you do things in C is important. The value of c is calculated before the input of a and b, so what value do you think c will have?

--
Mats 3. Code:
```#include <stdio.h>
main()
{
char operator;
int a,b,c;
printf("give the operator:");
scanf("operator");
if (operator='+')
{
printf("give first number:");
scanf("%d",&a);
printf("give second number:");
scanf("%d",&b);
c=a+b;
printf("the result is:");
printf("%d",c);
printf("\n");
}
else if (operator='-')
{
printf("give first number:");
scanf("%d",&a);
printf("give second number:");
scanf("%d",&b);
c=a-b;
printf("the result is:");
printf("%d",c);
printf("\n");
}
else if (operator='*')
{
printf("give first number:");
scanf("%d",&a);
printf("give second number:");
scanf("%d",&b);
c=a*b;
printf("the result is:");
printf("%d",c);
printf("\n");
}
else if (operator='/')
{
printf("give first number:");
scanf("%d",&a);
printf("give second number:");
scanf("%d",&b);
c=a/b;
printf("the result is:");
printf("%d",c);
printf("\n");
}
}```
run it and plz tell me what's going wrong because is difficult for me to explain the problem in english. 4. Code:
`scanf("operator");`
I think you made some typos here. 5. we have writen scanf("operator"); too.what is wrong??? 6. Look at your other usages of scanf(). See how they are assigning a value to a variable? Now look at the usage I quoted. What is it doing? 7. Originally Posted by sk8harddiefast Code:
```#include <stdio.h>
main()
{
char operator;
int a,b,c;
printf("give the operator:");
scanf("operator");
if (operator='+')
{
printf("give first number:");
scanf("%d",&a);
printf("give second number:");
scanf("%d",&b);
c=a+b;
printf("the result is:");
printf("%d",c);
printf("\n");
}
else if (operator='-')
{
printf("give first number:");
scanf("%d",&a);
printf("give second number:");
scanf("%d",&b);
c=a-b;
printf("the result is:");
printf("%d",c);
printf("\n");
}
else if (operator='*')
{
printf("give first number:");
scanf("%d",&a);
printf("give second number:");
scanf("%d",&b);
c=a*b;
printf("the result is:");
printf("%d",c);
printf("\n");
}
else if (operator='/')
{
printf("give first number:");
scanf("%d",&a);
printf("give second number:");
scanf("%d",&b);
c=a/b;
printf("the result is:");
printf("%d",c);
printf("\n");
}
}```
run it and plz tell me what's going wrong because is difficult for me to explain the problem in english.
I would llike to suggest a lot of things in your code:
1. scanf takes the address of the variable and syntax is similar to printf.
2. You are asking the user to input a and b in each case. It would have been better to ask for a and b before asking for the opeartor. This way you would have saved time in typing the code.
3. Instead of if and else if, I would suggest to use switch-case(if you have studied it) here.
4. return int explicitly from main. Popular pages Recent additions 