'unknown type name, expected identifier in c
Here is my program:
#include <stdio.h>
int main() {
int menunum;
int operand1, operand2;
printf("Select number 1: ");
scanf("%d", operand1);
printf("Select number 2: ");
scanf("%d", operand2);
printf("Select operation: \n");
printf("(1) add(+)\t(2) sub(-)\n");
scanf("%d", &menunum);
switch (menunum) {
case 1:
printf("Result: %d+%d=%d", operand1, operand2, operand1 + operand2);
break;
case 2:
printf("Result: %d-%d=%d", operand1, operand2, operand1 - operand2);
break;
}
When I run the compiler, I get this error:
test.c:1:1: error: unknown type name 's'
s
^
test.c:1:2: error: expected identifier or '('
s
^
2 errors generated.
please help me
Solution 1:[1]
Check first and second scanf of your code you have missed Ampersand (&) before operand1 and operand2 and also add a close curly bracket for your main function.
scanf("%d", &operand1);
scanf("%d", &operand2);
Note The ampersand (&) allows us to pass the address of variable number which is the place in memory where we store the information that scanf read.
Updated Code
#include <stdio.h>
int main(){
int menunum;
int operand1, operand2;
printf("Select number 1: ");
scanf("%d", &operand1);
printf("Select number 2: ");
scanf("%d", &operand2);
printf("Select operation: \n");
printf("(1) add(+)\t(2) sub(-)\n");
scanf("%d", &menunum);
switch(menunum) {
case 1:
printf("Result: %d+%d=%d", operand1, operand2, operand1+operand2);
break;
case 2:
printf("Result: %d-%d=%d", operand1, operand2, operand1 - operand2);
break;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
