C/C++ printf() before scanf() issue
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
add a comment |
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
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
add a comment |
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
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
c printf output scanf
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
Your output is being buffered.
You have 4 options:
explicit flush
fflush
after each write to profit from the buffer and still enforce the desiredbehavior/display explicitly.fflush( stdout );
have the buffer only buffer lines-wise
useful for when you know that it is enough to print only complete lines
setlinebuf(stdout);
disable the buffer
setbuf(stdout, NULL);
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;
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 theprintf("%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 usedsetvbuf(..., _IONBF, ...)
instead ofsetbuf
and usedsetvbuf(..., _IOLBF, ...)
instead ofsetlinebuf
.setvbuf
is part of the C standard library; the other two are not.
– jamesdlin
Nov 14 '18 at 0:48
add a comment |
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.
add a comment |
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
Your output is being buffered.
You have 4 options:
explicit flush
fflush
after each write to profit from the buffer and still enforce the desiredbehavior/display explicitly.fflush( stdout );
have the buffer only buffer lines-wise
useful for when you know that it is enough to print only complete lines
setlinebuf(stdout);
disable the buffer
setbuf(stdout, NULL);
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;
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 theprintf("%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 usedsetvbuf(..., _IONBF, ...)
instead ofsetbuf
and usedsetvbuf(..., _IOLBF, ...)
instead ofsetlinebuf
.setvbuf
is part of the C standard library; the other two are not.
– jamesdlin
Nov 14 '18 at 0:48
add a comment |
Your output is being buffered.
You have 4 options:
explicit flush
fflush
after each write to profit from the buffer and still enforce the desiredbehavior/display explicitly.fflush( stdout );
have the buffer only buffer lines-wise
useful for when you know that it is enough to print only complete lines
setlinebuf(stdout);
disable the buffer
setbuf(stdout, NULL);
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;
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 theprintf("%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 usedsetvbuf(..., _IONBF, ...)
instead ofsetbuf
and usedsetvbuf(..., _IOLBF, ...)
instead ofsetlinebuf
.setvbuf
is part of the C standard library; the other two are not.
– jamesdlin
Nov 14 '18 at 0:48
add a comment |
Your output is being buffered.
You have 4 options:
explicit flush
fflush
after each write to profit from the buffer and still enforce the desiredbehavior/display explicitly.fflush( stdout );
have the buffer only buffer lines-wise
useful for when you know that it is enough to print only complete lines
setlinebuf(stdout);
disable the buffer
setbuf(stdout, NULL);
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;
Your output is being buffered.
You have 4 options:
explicit flush
fflush
after each write to profit from the buffer and still enforce the desiredbehavior/display explicitly.fflush( stdout );
have the buffer only buffer lines-wise
useful for when you know that it is enough to print only complete lines
setlinebuf(stdout);
disable the buffer
setbuf(stdout, NULL);
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;
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 theprintf("%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 usedsetvbuf(..., _IONBF, ...)
instead ofsetbuf
and usedsetvbuf(..., _IOLBF, ...)
instead ofsetlinebuf
.setvbuf
is part of the C standard library; the other two are not.
– jamesdlin
Nov 14 '18 at 0:48
add a comment |
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 theprintf("%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 usedsetvbuf(..., _IONBF, ...)
instead ofsetbuf
and usedsetvbuf(..., _IOLBF, ...)
instead ofsetlinebuf
.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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited May 29 '14 at 9:20
answered Jun 1 '13 at 21:54
quapka
1,14831131
1,14831131
add a comment |
add a comment |
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?
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