I'm using win 8.1 64bit Eclipse Luna 4.4.0 and compiling with gcc 4.6.4 and the problem is e.g.
in this simple program, my printf and scanf statements are appearing on the console in the wrong order.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int i;
printf("Enter int: ");
scanf("%d",&i);
printf("Hello %d\n",i);
return EXIT_SUCCESS;
}
it does this:
4
Enter int: Hello 4
instead of this:
Enter int: 4
Hello 4
printf is buffered1. That means when you call it, it will not immediately print. Instead, it will store what you told it to print, and automatically print it when enough text has been stored in the buffer.
If you use a \n after in your print statement, it will automatically print the whole buffer (that's why the last call to printf prints everything). For your case, you may want to use a manual flush with fflush
printf("Enter int: "); fflush(stdout);
scanf("%d",&i);
printf("Hello %d\n",i);
1 Technically, it's stdout which is buffered, but it's easier to think of it as printf being buffered at this point.
Related
This question already has answers here:
C/C++ printf() before scanf() issue
(2 answers)
Closed 7 months ago.
I was doing c program in eclipse, first printf function is not showing in console, after I entered 2 values it is showing 2 printf out in same line, how to clear this problem
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int a,b,c;
printf("enter two numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf("total is %d",c);
return EXIT_SUCCESS;
}
This is my code
the problem seems to be that the print isn't flushed to the terminal.
when you use printf you actually are writing to a buffer that will be flushed to the terminal after one of the following:
the buffer is full
enough time has passed
you ended your print with '\n'
you give a flush command
I recommend just finishing your print with the '\n'
printf("enter two numbers\n");
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int a,b,c;
printf("enter the 2 numbers: ");
scanf("%d %d",&a,&b);
c=a+b;
printf("the sum is : %d ",c);
return(0);
}
this is a simple program to add 2 numbers.
my program would let me input the value..but it would not print the sum ,nor would it print the next line.
it would run till the scanf() and as i press enter, it would jst exit the program.
can you please tell me whats wrong. I am a beginner programmer...
There are two things you should think of here.
End printouts with a newline character, because stdout is often line buffered. Do printf("the sum is : %d \n",c); instead. Or call fflush(stdout); expliticly after the printout. This will ensure everything gets printed.
Add some input code in the end. Like an extra scanf("%d", &a); This is basically a small hack to prevent the window from closing before you can see the final output. Another alternative is to add sleep(3); to sleep for 3 seconds. A third alternative here is to see if there are some settings that controls the closing of the window in your IDE.
Your program works correctly, but it exits right after printing the output, giving you no time to look at it.
Consider adding some input before return(0);, such as 2 getchar(); calls. You need 2, because the first character read will be the \n that you typed after the numbers.
Putting the toes into the C programming and tried this test after another small program wasn't working:
#include <stdio.h>
int main()
{
int i;
printf("Test1\n");
printf("Test2\n");
printf("InputTest1: ");
scanf("%d\n", i);
printf("OutputTest1: %d\n", i);
printf("InputTest2: ");
scanf("%d\n", i);
return 0;
}
Output:
Test1
Test2
InputTest1: 10
For some reason that I cannot surmise when going from scanf to printf it hangs and then doesn't print anymore.
Thank you for your assistance,
First of all you need to pass a pointer to i to scanf(), not i itself (C doesn't have arguments by reference). But scanf() is fraught with problems for user input and it would be better to use fgets() and convert the number using strtol().
So, I have a printf, that asks for the users middle initial, then I have a scanf under that, then I output the users middle initial. My problem is that my printf is displaying after my scanf
C Code
#include <stdio.h>
#include <string.h>
int main(void) {
char middleInitial;
printf("What is your middle initial? ");
scanf(" %c", &middleInitial);
printf("Middle initial %c", middleInitial);
}
So as you can see, there are two printf's. My scanf is running before my first printf displays the question.
Example (This is what I'm getting in my terminal)
$ ./a.exe
c
What is your middle initial? Middle initial c
What I want
$ ./a.exe
What is your middle initial? c
Middle initial c
By the way, the c is what the user inputs
Call fflush(stdout) before your call to scanf().
type fflush(stdout); just before your first scanf() should be after first printf()
In my windows pc I face the same issue. If you use different operating system you might not need it.
I have a simple program from a C programming book, and it's supposed to ask for two integers and then add them together and show the sum. I'm able to enter the two numbers, but the output doesn't show up until the very end of the program.
#include <stdlib.h>
#include <stdio.h>
/* Addition Program*/
main()
{
int integer1, integer2, sum;
printf("Enter first integer\n");
scanf("%d", &integer1);
printf("Enter second integer\n");
scanf("%d", &integer2);
sum = integer1 + integer2;
printf("Sum is %d\n", sum);
return 0;
}
The output looks like this:
2
6
Enter first integer
Enter second integer
Sum is 8
Any help would be greatly appreciated, thanks!
It is possible that the output is not being flushed automatically. You can add fflush(stdout) after each printf() and see if that helps.
Which environment are you using to build and run this program?
Further to the above, printf will only automatically flush it's buffer if it reaches a newline.
If you are running on windows, a newline is \r\n instead of \n.
Alternatively you can do:
fflush(stdout);
Another alternative is to turn off buffering by calling:
setbuf(stdout, NULL);
EDIT:
Just found this similar(but not the same) question:
Why does printf not flush after the call unless a newline is in the format string?