Why does my conclusion not appear before the program is released?

I have a simple program from a C programming book, and it should query for two integers and then add them together and show the sum. I can enter two numbers, but the output is not displayed 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 result is as follows:

 2 6 Enter first integer Enter second integer Sum is 8 

Any help would be greatly appreciated, thanks!

+7
c
source share
2 answers

It is possible that the output is not reset automatically. You can add fflush (stdout) after each printf () and see if that helps.

What environment do you use to create and run this program?

+8
source share

In addition to the above, printf will only automatically flush the buffer if it reaches a new line.

If you are running Windows, the new line is \r\n instead of \n .

Alternatively, you can:

 fflush(stdout); 

Another option is to disable buffering by calling:

 setbuf(stdout, NULL); 

EDIT:

Just found this similar (but not the same) question: Why printf is not reset after a call if a new line is not specified in the format line?

+2
source share

All Articles