Eclipse C printf () error

I just installed Eclipse for C ++ and I had a problem with the printf() function. My code should start by typing: β€œGive the number,” but that is not the case. If I type 4, this is what I get:

  4

 Give the number:

 I am number 1

 I am number 2

 I am number 3

 I am number 4
 #include <stdio.h> #include <stdlib.h> int main(void) { int n=0,i; printf("Give the number:\n"); scanf("%d", &n); for(i=1;i<=n;i++) printf("I am number %d\n",i); return EXIT_SUCCESS; } 
+4
source share
1 answer

There are three different types of buffering strategies:

  • Characters written or unreadable from an unbuffered stream are transferred individually to or from a file as soon as possible.

  • Characters written to or read from a line, a buffered stream, are transferred to or from a file in blocks when a newline character is encountered.

  • Characters written or read from a fully buffered stream are transferred to or from a file in the form of blocks of arbitrary size.

As Daniel Fisher and Jonathan Leffler noted, it seems that your terminal is initially fully buffered, which means you should use fflush(stdout); to transfer your stream to the terminal or change its configuration using setvbuf(stdout, 0, _IOLBF, 0); .

0
source

All Articles