-1

/I took input from user as 'a' and 'b' but in 'b' it takes 0 by default ..... whatever i give in 'b' as input it showing 0 as 'b' on applying Operation.... C code ....../

#include <stdio.h>

int main()
{
    int a,b;
    printf("Enter the I no. : ");
    scanf("%d",&a);
    printf("Enter the II no. : ");
    scanf("%d",&b);

    char c;
    printf("Enter the Operation : ");
    scanf("%s",&c);

    switch (c)
    {
    case '+':
        printf("Addtion of %d and %d = %d",a,b,a+b);
        break;
    case '-':
        printf("Subtraction of %d and %d = %d",a,b,a-b);
        break;
    case '*':
        printf("Multiplication of %d and %d = %d",a,b,a*b);
        break;
    case '/':
        printf("Division of %d and %d = %d",a,b,a/b);
        break;
    }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    `"%s"` is used to read a string of characters. To read a single character, use `" %c"`. Note the space before the `%`. You need that space. – user3386109 Feb 11 '23 at 17:36
  • Suggest that you learn some basic [debugging](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) steps. Print the values of a, b, and c after they've been input. Are they what you expected? Does the switch execute the correct case arm? – jarmod Feb 11 '23 at 17:53
  • Since the `scanf("%s", &c)` call treats `c` as an array of at least 2 characters, the null byte overwrote something — probably the least-significant byte of `b`. Enter a number bigger than 255 for `b` and see what you get. – Jonathan Leffler Feb 11 '23 at 18:30
  • When asking for debugging help, you should provide an **exact copy** of the input and an exact copy of the output and a sample of the output desired instead. This could help diagnose whether the stray null character from the errant use of `%c` is the cause or there is some other problem. – Eric Postpischil Feb 11 '23 at 18:50

2 Answers2

1

You declared an object of the type char

char c;

So to input a value for the object you need to use conversion specifier %c instead of %s

printf("Enter the Operation : ");
scanf(" %c",&c);

Pay attention to the leading space in the format string. It allows to skip white space characters.

Also before performing division you need to check whether b is not equal to 0.

And it is desirable to include the case label default in the switch statement for an invalid inputted operation.

Also some operations as for example the multiplication can result in overflow. To avoid overflow you should cast operands to the type long long int In this case the calls of printf will look the following way as for example

printf( "Addtion of %d and %d = %lld\n", a, b, ( long long int )a + b );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

scanf("%d",&b); appears to be correct code to read a decimal numeral into b. If a non-zero number is entered but b later appears to be zero, a likely explanation is that the mistaken use of scanf("%s",&c); writes a null byte into b.

%s is for reading multiple characters into an array of char. In addition to reading characters from input, it writes a null byte into the array to mark the end of the characters. However, c is a single character, not an array. If you entered a character such as “+” (followed by pressing Enter) then scanf("%s",&c); will write the character into c and then write a null character to the next byte in memory.

If the compiler happened to put b just after c in memory, that will alter the value stored in b.

Do not use scanf("%s",&c); to read a single character. Use " %c". The space character tells scanf to skip white-space (such as the new-line character that will be in the input stream after the user enters a value for b and presses Enter) %c tells scanf to read a single character: scanf(" %c", &c);.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312