2

The following is a simple C program:

#include <stdio.h>

int main(void)
{
    //Question 2.16

    //Variables that will be used to store the two numbers
    int num1;
    int num2;

    //Message to prompt the user
    printf ("Enter two numbers\n");

    //Accepting the users input
    scanf("%d %d" , &num1, &num2);

    if (num1 > num2) {
        printf("%d is greater\n", num1); // Print num1 if num1 is greater
    }
    else { //Otherwise print that num1 is not greater
        printf("%d is not greater\n", num1);
    }

    return 0; // End of program
}

But when I build and run the program (the IDE that I am using is Eclipse Cpp Neon), I have to input the values for the variables num1 and num2 before the first printf statement is executed. See the following for the console output:

2 5

Enter two numbers

2 is not greater

My question is simply this: Why is this happening? Any explanation would be welcome.

Lundin
  • 195,001
  • 40
  • 254
  • 396
D Brown
  • 460
  • 3
  • 8
  • 22
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/198339/discussion-on-question-by-d-brown-why-is-the-scanf-statement-executing-before-th). – Samuel Liew Aug 22 '19 at 23:32

3 Answers3

3
printf(...)

function puts the output into buffer of output stream. So it may happen that sometimes the output is displayed after sometime.

scanf(...)

function uses input stream.

Both function involves independent streams and due to buffering the results may not seem sequential as per code. To forcefully flush any stream

int fflush(FILE *stream);

is used.

Please use

fflush(stdout);

after print statement to get desired output.

  • 3
    Why does the new-line character in the `printf` output not cause a flush of the stream, as is required for line-buffered streams per C 2018 7.21.3 3? We should not just add code to a program until it works; we should understand the root causes. Does this IDE not open a line-buffered stream? How do we know? – Eric Postpischil Aug 22 '19 at 14:29
  • @EricPostpischil I would really like to know the root cause for this though. Thanks for bringing that point up. – D Brown Aug 22 '19 at 15:31
  • @biplavkarna Thanks much Biplav, it worked! Great going! (I'm not sure what happened the first time I made this comment, to be honest.) – D Brown Aug 22 '19 at 16:53
1

My advice is the following:

(1) Use a stream explicitly rather than implicitly: For instance, prefer fprintf(stdout, "Enter two numbers\n"); fflush(stdout); instead of printf ("Enter two numbers\n");. Apply the same rule for the scanf function—that is, prefer fscanf and the stream (for instance, stdin) that you are using.

(2) Do not season a scan function anymore than one input at a time—this can cause your stream to malfunction in an unexpected way that you cannot calculate: Therefore, prefer fscanf(stdin, "%d", &num1); fscanf(stdin, "%d", &num2); instead of scanf("%d %d", &num1, &num2);.

C. R. Ward
  • 93
  • 5
  • Thanks! I was able to use the fflush() method successfully. When you say stream, you mean a byte stream that is specifically for the transmission of data to an output device? And can you tell me why it is important to use an explicit stream rather than an implicit one? – D Brown Aug 22 '19 at 22:04
  • In a function, there should be only one single interpretation of what effect the function can have, even if it uses a lot of condition tests to decide what to do. See tag [unit-testing]. – C. R. Ward Aug 26 '19 at 16:24
-1

Try using fflush between your printf and scanf statements:

// Message to prompt the user
printf("Enter two numbers\n");
fflush(stdout);
// Accepting the user's input
scanf("%d %d", &num1, &num2);
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76