0

What is the problem with the scanf_s call in my code? (I can't get the value into num2 and the command is ending before it reaches the end.)

I don't have this problem when I try to do this:

scanf_s("%c", &op);
scanf_s("%d%d", &num1,&num2);

The original code:

#include<stdio.h>    
void main()
{
    int num1, num2, res, opok=1;
    char op;
    printf("please insert your experession:\n");
    scanf_s("%d%c%d", &num1, &op, &num2);
    
    switch (op)
    {
    case '+':
        res = num1 + num2;
        break;
    case '-':
        res = num1 - num2;
        break;
    case '*':
        res = num1 * num2;
        break;
    default:
        opok = 0;
        break;
    }
    if (opok == 1)
    {
        printf_s("%d\n", res);
    }
    else
    {
        printf_s("error\n");
    }

    

}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
roi sh
  • 9
  • 1
  • 2
  • 1
    While it is likely the issue is you need to include a space in the `scanf_s` format string or to use proper arguments with it, how can anybody be sure? You did not provide the input you are typing, the resulting observed behavior of the program, or a description of what behavior you desired or expected instead. E.g., you did not tell us what values appear in `num1`, `op`, or `num2` after scanning or what values you expected to appear. In the future, when asking questions like this, include a [mre], including information about input, observed output, and desired output. – Eric Postpischil Aug 08 '20 at 12:47
  • There is no such thing as "void main()". Please use one of the defined main function signatures. You probably want " int main()". – nvoigt Aug 08 '20 at 12:48
  • @EricPostpischil The documentation for `scanf_s`, whether in its [Microsoft](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/scanf-s-scanf-s-l-wscanf-s-wscanf-s-l?view=vs-2019) or [standard](https://en.cppreference.com/w/c/io/fscanf) form, makes it clear what the issue is here. – Adrian Mole Aug 08 '20 at 12:49
  • 1
    @AdrianMole: The documentation for `scanf_s` makes it clear what **one** issue is. It does not make it clear that is OP’s only problem or even that it is the one causing whatever problem they are complaining about, which they failed to state completely. An MRE should always be provided with questions like this. – Eric Postpischil Aug 08 '20 at 12:50
  • @nvoigt: C implementations are permitted to accept `void main()`, and, if you want to stick to one of the forms the standard requires that implementations accept, it would be `int main(void)`, not `int main()`. – Eric Postpischil Aug 08 '20 at 12:50
  • @EricPostpischil OK, I see your point. (And there is also the issue of why the first code seems to work, I guess.) – Adrian Mole Aug 08 '20 at 12:55
  • @Eric Also, the code supplied by the OP (the main code) ***is*** an MRE! I copy/pasted it into my IDE (Visual Studio) without change and it compiled, ran and reproduced the problem. – Adrian Mole Aug 08 '20 at 13:19
  • @AdrianMole: An MRE is not just code that runs. It includes sample input, sample output, and desired output. – Eric Postpischil Aug 08 '20 at 13:26
  • @EricPostpischil since you started being pedantic, the standard explicitly mentions `int main()` being acceptable, though *obsolescent*, allowed but discouraged. Most notably the C11,C17 drafts still use `int main()` in some of its examples. – Antti Haapala -- Слава Україні Aug 08 '20 at 19:04
  • @AnttiHaapala: In the C 2018 standard, I see no explicit statement that `int main()` is acceptable and no statement that it is obsolescent. Please give specific citations. – Eric Postpischil Aug 08 '20 at 19:25
  • there is no explicit citation, but there are still 2 examples that use `int main()`, and the standard says that the function should be defined to take no parameters, and `int main() {}` defines such a function too, though the example under uses `int main(void)`. – Antti Haapala -- Слава Україні Aug 08 '20 at 19:31

1 Answers1

2

When using the scanf_s function, the %c and %s format specifiers require an additional size argument for each of their corresponding data arguments, which is the size of the buffer pointed to by that argument. This size argument (of unsigned type) should immediately follow the corresponding data argument.

In your case (a single character to be read with the %c format), the size of that buffer will be 1, so use the following input line:

scanf_s("%d%c%d", &num1, &op, 1u, &num2);
//                            ^ size of the "op" variable (in bytes)
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83