1
#include<stdio.h>
int main()
{
    int a, b, c;
    printf("Enter the two numbers:");
    scanf("%d%d \n", &a, &b);
    c=a+b;
    printf("a+b= %d \n", c);
    c=a-b;
    printf("a-b= %d \n", c);
    c=a*b;
    printf("a*b= %d \n", c);
    c=a/b;
    printf("a/b= %d \n", c);
    c=a%b;
    printf("Remainder when a divided by b= %d \n", c);
    return 0;
}

When running this code, it doesn't ask for any input, but rather just gives a garbage value for all the print functions.

Why?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
LordRishav
  • 33
  • 6
  • what isn't working exactly? be more descriptive. does your code crash? or it doesn't provide you the correct output? – llamaro25 Oct 05 '20 at 04:05
  • It doesn't ask for the input part, and gives some garbage value. – LordRishav Oct 05 '20 at 04:06
  • Okay, thanks, it is working now. But why did it happen like such? I remember I used scanf without fflush(stdout); – LordRishav Oct 05 '20 at 04:13
  • Output to `stdout` (where `printf` is writing) is by default *line buffered*. That means all output is put into a buffer, and this buffer is *flushed* (actually written) when either the buffer is full, when you explicitly flush it (with `fflush(stdout)`), or when you print a newline. If none of that happens, then the output simply won't be written. – Some programmer dude Oct 05 '20 at 04:16
  • 2
    Also, one should almost never use trailing space in a `scanf` format (and newline is considered a space character). It will cause `scanf` to continue reading until it finds some non-space input, so you either need to provide three inputs, use the end-of-file keyboard-sequence, or otherwise do something which result in `scanf` erroring out. – Some programmer dude Oct 05 '20 at 04:19
  • 2
    It is unsafe to use `scanf` without checking the return value. See this page for further information: [A beginners' guide away from scanf()](http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html) – Andreas Wenzel Oct 05 '20 at 04:20
  • 1
    @Someprogrammerdude: According to [section 7.21.3 §3 of the ISO C standard](http://port70.net/~nsz/c/c11/n1570.html#7.21.3p3), output buffers are also intended to be flushed when reading input. However, this is implementation-defined. – Andreas Wenzel Oct 05 '20 at 04:28

2 Answers2

1

Your scanf line should be scanf("%d %d", &a, &b); instead. Always give spacing when you are inputting two variables as inputs, and do not insert a \n in the scanf function - it considers that as a place for a third input.

Please read about escape sequences in detail to see why this is an issue.

Saif Ul Islam
  • 345
  • 3
  • 14
  • 1
    There is no need to add the space between the two `%d` conversion specifications, though there is no harm done if it is added. Removing the trailing blank and newline from the `scanf()` format string is crucial. However, describing it as "[`scanf()`] considers that as a place for a third input" is not entirely accurate — the 'skip white space' process only ends when the next character is not white space, or when EOF or an error is encountered. – Jonathan Leffler Oct 05 '20 at 04:38
1

Add flush(stdout) to display input before scanf. In scanf, remove '\n' and add space between %d.

#include<stdio.h>
int main()
{
    int a, b, c;
    printf("Enter the two numbers:");
    fflush(stdout);
    scanf("%d %d", &a, &b);
    c=a+b;
    printf("a+b= %d \n", c);
    c=a-b;
    printf("a-b= %d \n", c);
    c=a*b;
    printf("a*b= %d \n", c);
    c=a/b;
    printf("a/b= %d \n", c);
    c=a%b;
    printf("Remainder when a divided by b= %d \n", c);
    return 0;
}

Example output:

Enter the two numbers:1 2
a+b= 3 
a-b= -1 
a*b= 2 
a/b= 0 
Remainder when a divided by b= 1 
llamaro25
  • 642
  • 1
  • 7
  • 22
  • There is no need to add the space between the two `%d` conversion specifications, though there is no harm done if it is added. Removing the trailing blank and newline from the `scanf()` format string is crucial. – Jonathan Leffler Oct 05 '20 at 04:35
  • According to [section 7.21.3 §3 of the ISO C standard](http://port70.net/~nsz/c/c11/n1570.html#7.21.3p3), `stdout` should be automatically flushed when reading new input from a console using `stdin`. Therefore, the line `fflush(stdout)` should not be necessary before a call to `scanf`. However, as far as I can tell, the wording of the standard does not absolutely require this behavior, so I guess an explicit flush could be theoretically necessary on some platforms. But I believe it should not be necessary on common platforms such as Windows, Linux and MacOS. – Andreas Wenzel Oct 05 '20 at 04:42