int and char

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
#include<conio.h>


int main()
{
   char str[3];
   scanf("\n%s",str);
   int c;
   if(str[1]=='+')
   printf("%d",str[0]+str[2]);
   else  if(str[1]=='-')
   printf("%d",str[0]-str[2]);
   else  if(str[1]=='*')
   printf("%d",str[0]*str[2]);
   else
   printf("%d",str[0]/str[2]);
    
    getch();
}


input:
1+2

output:
99

i want output to be 3..hw to do that?
There are two technical errors in this program:
1. You have declared the string for three character. By default the last character should be '\0'. So, can only enter two characters. If you enter 3 or more characters, compiler may show unexpected result. Source: http://www.programiz.com/c-programming/c-strings

2. str[0]+str[2] gives you 99 when you entered 1+2. It is because ASCII value of 1 is 49 and ASCII value of 2 is 50. When, character is stored in variable(or string), ASCII value corresponding to that number is stored instead of that character.

In fact, you can perform this problem using switch case statement. Source:
http://www.programiz.com/c-programming/c-switch-case-statement
i hve still not got my answer..
how to get output = 3?
someone plz tell me what do i need to change in code!!
You need to convert character type to integer type and it can be done using atoi() function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
#include<conio.h>
int main()
{
   char str[4];
   scanf("%s",str);
   str[0]=atoi(&str[0]);   // Converting str[0] character to integer
   str[2]=atoi(&str[2]);   // Converting str[2] character to integer
   if(str[1]=='+')
   printf("%d",str[0]+str[2]);
   else  if(str[1]=='-')
   printf("%d",str[0]-str[2]);
   else  if(str[1]=='*')
   printf("%d",str[0]*str[2]);
   else
   printf("%d",str[0]/str[2]);

    getch();
}


Using swtich case statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# include <stdio.h>
int main(){
     char operator;
     float num1,num2;
     printf("Enter operator +, - , * or / :\n");
     operator=getche();
     printf("\nEnter two operands:\n");
     scanf("%f%f",&num1,&num2);
     switch(operator)                              
     {
     case '+':
              printf("num1+num2=%.2f",num1+num2);
              break;
     case '-':
              printf("num1-num2=%.2f",num1-num2);
              break;
     case '*':
              printf("num1*num2=%.2f",num1*num2);
              break;
     case '/':
              printf("num2/num1=%.2f",num1/num2);
              break;
     default:                                 
/* if operator is other than +, -, * or /, error message is shown */
              printf(Error! operator is not correct");
              break;
     }
     return 0;
} 
1
2
str[0]=atoi(&str[0]);
str[2]=atoi(&str[2]);   
I dare you to print the value of int(str[2]) after these two lines.

The permitted length of the user input in the scanf statement needs to be set too.

1
2
   char str[4];
   scanf("\n%3s",str);


Here, str[4] has space to hold 3 characters plus the null terminator.
Therefore, the input must be limited to a maximum of three characters, using %3s.
Topic archived. No new replies allowed.