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?
Related
#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.
I am using do...while as a loop control in C language. I need to terminate the program when the user enters "99", else the user should guess the number to terminate the program, until then the program will run. Here is the code. I don't have any compiler error, but instead it prints the number 0 to the number the users entered.
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int num = 0;
do {
scanf("Enter the magic number to terminate this program: %d\n", &num);
printf("You entered : %d\n", num);
num++;
} while (num != 100);
printf("\nThe magic number is correct and the program is terminated:");
return 0;
}
Any help is appreciated.
scanf is not like Python's input, for example. It requires the exact input that you specify in the format string. In your particular case, you are expecting the users to enter "Enter the magic number to terminate this program: ", followed by a number.
One way to check this is to use the return value, which tells you the number of matched arguments:
k = scanf("...\n", &num);
printf("scanf managed to get %d inputs\n", k);
The solution is to split the input and output portions:
printf("Enter the magic number to terminate this program: ");
scanf("%d", &num);
scanf will automatically filter out whitespace between inputs, including the newlines that the user enters. You can also consume those explicitly with either "%d\n" or equivalently "%d ". However, you shouldn't do that. scanf treats any whitespace characters as "any number of whitespace", and will block until it receives the next non-whitespace character. It will also cause a problem if your stream ends without a trailing newline.
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().
I'm currently studying beginner C programming using Eclipse IDE.
Below is a very basic program that suppose to print out a line first, take in some input, and then print out a second line. But my Eclipse always ask me for the input first before printing out the two lines together afterwards.
This is extremely frustrating because it is something so simple and doesn't work. What am I doing wrong?
#include <stdio.h>
int main(void){
int aNumber;
printf("first line\n");
scanf("%d", &aNumber);
printf("second line with %d", aNumber);
return 0;
}
Had the same problem once.
Solved it by flushing stdout.
#include <stdio.h>
int main(void){
int aNumber;
printf("first line\n");
fflush(stdout); // Prints stdout content
scanf("%d", &aNumber);
printf("second line with %d", aNumber);
return 0;
}
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.