C/C++ printf() before scanf() issue










22














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









share|improve this question























  • 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
    Jun 1 '13 at 21:15











  • Your code works for me.
    – spartygw
    Jun 1 '13 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
    Jun 1 '13 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
    Jun 1 '13 at 21:27















22














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









share|improve this question























  • 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
    Jun 1 '13 at 21:15











  • Your code works for me.
    – spartygw
    Jun 1 '13 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
    Jun 1 '13 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
    Jun 1 '13 at 21:27













22












22








22


20





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









share|improve this question















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






c printf output scanf






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 20 '14 at 19:16









Mark Rotteveel

59.3k1476119




59.3k1476119










asked Jun 1 '13 at 21:08









quapka

1,14831131




1,14831131











  • 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
    Jun 1 '13 at 21:15











  • Your code works for me.
    – spartygw
    Jun 1 '13 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
    Jun 1 '13 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
    Jun 1 '13 at 21:27
















  • 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
    Jun 1 '13 at 21:15











  • Your code works for me.
    – spartygw
    Jun 1 '13 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
    Jun 1 '13 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
    Jun 1 '13 at 21:27















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
Jun 1 '13 at 21:15





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
Jun 1 '13 at 21:15













Your code works for me.
– spartygw
Jun 1 '13 at 21:19




Your code works for me.
– spartygw
Jun 1 '13 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
Jun 1 '13 at 21:24





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
Jun 1 '13 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
Jun 1 '13 at 21:27




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
Jun 1 '13 at 21:27












2 Answers
2






active

oldest

votes


















26














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;






share|improve this answer






















  • 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
    Jun 1 '13 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
    Jun 1 '13 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.
    – Jonathan Leffler
    Sep 15 '16 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
    Nov 14 '18 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.






share|improve this answer





















    protected by Community Jun 12 '17 at 0:11



    Thank you for your interest in this question.
    Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



    Would you like to answer one of these unanswered questions instead?














    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    26














    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;






    share|improve this answer






















    • 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
      Jun 1 '13 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
      Jun 1 '13 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.
      – Jonathan Leffler
      Sep 15 '16 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
      Nov 14 '18 at 0:48
















    26














    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;






    share|improve this answer






















    • 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
      Jun 1 '13 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
      Jun 1 '13 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.
      – Jonathan Leffler
      Sep 15 '16 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
      Nov 14 '18 at 0:48














    26












    26








    26






    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;






    share|improve this answer














    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;







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jun 1 '13 at 22:00

























    answered Jun 1 '13 at 21:27









    zsawyer

    1,2591123




    1,2591123











    • 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
      Jun 1 '13 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
      Jun 1 '13 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.
      – Jonathan Leffler
      Sep 15 '16 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
      Nov 14 '18 at 0:48

















    • 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
      Jun 1 '13 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
      Jun 1 '13 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.
      – Jonathan Leffler
      Sep 15 '16 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
      Nov 14 '18 at 0:48
















    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
    Jun 1 '13 at 21:32





    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
    Jun 1 '13 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
    Jun 1 '13 at 21:44




    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
    Jun 1 '13 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.
    – Jonathan Leffler
    Sep 15 '16 at 5:05




    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.
    – Jonathan Leffler
    Sep 15 '16 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
    Nov 14 '18 at 0:48





    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
    Nov 14 '18 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.






    share|improve this answer



























      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.






      share|improve this answer

























        6












        6








        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.






        share|improve this answer














        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 29 '14 at 9:20

























        answered Jun 1 '13 at 21:54









        quapka

        1,14831131




        1,14831131















            protected by Community Jun 12 '17 at 0:11



            Thank you for your interest in this question.
            Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



            Would you like to answer one of these unanswered questions instead?



            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto