0

I just started learning C, but I've already encountered a problem. I want to write a simple calculator, but -I don't know why- the second integer always counts as 0. I just can't find out what could be the problem.

Here's my code:

int main(){

    int a, b;
    char c;

    printf("Enter 2 numbers:\n");
    scanf("%d %d", &a, &b);
    printf("Enter an operator:\n");
    scanf("%s", &c);

    switch(c){
    case '+':
        printf("%d\n", a+b);
        break;
    case '-':
        printf("%d\n", a-b);
        break;
    case '*':
        printf("%d\n", a*b);
        break;
    case '/':
        printf("%d\n", a/b);
        break;
    }
}

Thanks for the help:)

Wander Nauta
  • 18,832
  • 1
  • 45
  • 62

3 Answers3

1

You can use scanf("%d%d", &a, &b) without a space between. Note that the input must contain whitespace between the numbers (otherwise 12 3 would be indistinguishable from 1 23). Also, you should note that %s is designed to work with strings, and is not safe. Try to use scanf(" %c", &c) to work with a single character, not a string (also note the space before the percent, that swallows all the whitespace before the operator character).

Arusekk
  • 827
  • 4
  • 22
  • Not a single whitespace. You missed the need of the correct format specifier. – Yunnosch Oct 05 '19 at 17:41
  • 1
    Most of this "answer" counts as _advice_, the actual problem is the erroneous use of `%s` to read a character - the resulting overrun corrupts the adjacent data. We now have an OP who thinks the solution is the space - which it is not (primarily). The _answer_ as such is not as prominent as it should be. You should make that the first point. The _advice_ might even be more appropriate for a comment on the question rather than part of this answer - it should at least be secondary. – Clifford Oct 05 '19 at 18:15
  • *You can use `scanf("%d%d", &a, &b)` without a space between.* What difference would that make? – Steve Summit Oct 05 '19 at 18:38
  • 1
    Yes, I said it wrong, the space AND the incorrect usage of %s/%c:) – CrocoGuy Oct 05 '19 at 23:34
1

You are reading a string into a single character: %s will read, f.ex. + and \n into c thus overwriting your stack.

You should also check the return value of scanf() for errors, i.e. if the user has entered incorrect data and the parser failed.

With these changes the code works as described:

#include <stdlib.h>
#include <stdio.h>

int main(void) {
    int a, b;
    char c[20];
    int result;

    printf("Enter 2 numbers:\n");
    result = scanf("%d %d", &a, &b);
    if (result != 2) {
        perror("integer input failed");
        return(1);
    }
    printf("Enter an operator:\n");
    result = scanf("%s", c);
    if (result != 1) {
        perror("character input failed");
        return(1);
    }

    /* just showing that everything was recognized correctly... */
    printf("'%d' '%d' '%s'\n", a, b, c);

    switch(c[0]){
    ...

Example run:

$ gcc -Wall -Werror -o dummy dummy.c
$ ./dummy
Enter 2 numbers:
1 2
Enter an operator:
+
'1' '2' '+'
3
$
Stefan Becker
  • 5,695
  • 9
  • 20
  • 30
0

You are using %s for reading into a char. That is undefined behaviour.
You need to use correct format specifiers to avoid that. In this case %c.
Additionally, to skip the newline/return which you find in the input after scanning a number, use a blank beforehand, i.e. " %c".

End of the answer, with undefined behaviour, anything can happen.

Just for fun however, try this code and use 2 and 257 as input:

#include <stdio.h>

int main(){

    int a=99, b=98;
    char c='x';

    printf("Enter 2 numbers:\n");
    scanf("%d %d", &a, &b);
    printf("You entered %d %d. Now enter an operator:\n", a, b);
    scanf(" %s", &c);
    printf("After scanning a null-terminated string into a char variable, a and b are now %d %d.\n", a,b);

}

If we are lucky and you get the same effect of undefined behaviour as I do, then you have now learned something about where the 0 at the end of the operator string ends up. Namely in one of the bytes of the variable b.

With the proposed input you probably get 2 and 256. The 2 has survived, the 257 got the lower byte zeroed and ends up being 256.

Also try entering 2 3 and "+A". You are aware that 'A' has an ascii value of 65, aren't you?

As a final hint, study this article. It is very helpful for anythig you want to read in.
http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html

Yunnosch
  • 26,130
  • 9
  • 42
  • 54