38

I'm using Eclipse to code in C/C++ and I'm struggling with what might be something pretty easy. In my code below I use printf() and after scanf(). Althougth printf is written before scanf() the output differs. I was able to find out something about similar issue here. But I wasn't able to solve it. Any ideas?

Code:

#include <stdio.h>

int main()
{
    int myvariable;

    printf("Enter a number:");
    scanf("%d", &myvariable);
    printf("%d", myvariable);

    return 0;
}

Expected output:

Enter a number:1
1

Instead I get:

1
Enter a number:1
5
  • Your question is confusing: "In my code below I use printf() and after scanf()". "printf is written before scanf()". it's not what you put in the code. Please rephrase your question.
    – Elazar
    Commented Jun 1, 2013 at 21:15
  • Your code works for me.
    – spartygw
    Commented Jun 1, 2013 at 21:19
  • You are right, sorry. I meant, that first I want to print something, in this case: printf("Enter a number:"); Then read number from keyboard. And then print the number into the console. But instead after running the programme nothing prints out and the programme waits for input. After receiving the input the programme prints out both "Enter a number:" and the number itself.
    – quapka
    Commented Jun 1, 2013 at 21:24
  • I don't know. Maybe I have wrong plugins or something. As I wrote before, I'am new to this, and I just followed few tutorials on how to set C/C++ in Eclipse. Maybe it'll be better to remove everything and try to start from fresh beginning.
    – quapka
    Commented Jun 1, 2013 at 21:27
  • This question is probably a duplicate of: printf not printing to screen (which was posted 14 hours easlier) Commented Feb 23, 2022 at 0:05

2 Answers 2

50

Your output is being buffered. You have 4 options:

  1. explicit flush

    fflush after each write to profit from the buffer and still enforce the desiredbehavior/display explicitly.

     fflush( stdout );
    
  2. have the buffer only buffer lines-wise

    useful for when you know that it is enough to print only complete lines

     setlinebuf(stdout);
    
  3. disable the buffer

     setbuf(stdout, NULL);
    
  4. disable buffering in your console through what ever options menu it provides


Examples:

Here is your code with option 1:

#include <stdio.h>
int main() {

    int myvariable;
    
    printf("Enter a number:");
    fflush( stdout );
    scanf("%d", &myvariable);
    printf("%d", myvariable);
    fflush( stdout );

    return 0;
}

Here is 2:

#include <stdio.h>
int main() {

    int myvariable;

    setlinebuf(stdout);    

    printf("Enter a number:");
    scanf("%d", &myvariable);
    printf("%d", myvariable);

    return 0;
}

and 3:

#include <stdio.h>
int main() {

    int myvariable;

    setbuf(stdout, NULL);     

    printf("Enter a number:");
    scanf("%d", &myvariable);
    printf("%d", myvariable);

    return 0;
}
4
  • Thanks. The first option works. But still, it's seems like pretty messy solution considering writing longer and more complex code. I mean, it's like doubleing the work. I'll try the fourth though.
    – quapka
    Commented Jun 1, 2013 at 21:32
  • If you don't care about buffering and its possible benefits you can go with option 3. It's a one-liner (set only once in your program and done). Option 4 is an alternative if you do not want to or cannot change the code at all. Option 2 probably won't reliably echo your input if there is no newline.
    – zsawyer
    Commented Jun 1, 2013 at 21:44
  • Each of the printf("%d", myvariable); lines would be better with a newline at the end. Only omit the newline if you are consciously building up a single line of output piecemeal. Commented Sep 15, 2016 at 5:05
  • This answer would be better if it used setvbuf(..., _IONBF, ...) instead of setbuf and used setvbuf(..., _IOLBF, ...) instead of setlinebuf. setvbuf is part of the C standard library; the other two are not.
    – jamesdlin
    Commented Nov 14, 2018 at 0:48
6

Ok, so finally I used something similar to what @zsawyer wrote as an option labelled 3. In my code I inserted this line:

setvbuf(stdout, NULL, _IONBF, 0);

As a first line in main():

#include <stdio.h>

int main()
{
    setvbuf(stdout, NULL, _IONBF, 0);

    int myvariable;

    printf("Enter a number:");
    scanf("%d", &myvariable);
    printf("%d", myvariable);

    return 0;
}

I got it from here.

Not the answer you're looking for? Browse other questions tagged or ask your own question.