2

consider the below programs. I would like to know why this codes behave in different way.Thankyou in advance.

this doesnot print any

#include <stdio.h>

int main() {
    int i = 0;

    while(i < 10) {
        if(i < 7)
            printf("value is%d", i++); 
    }
}

while this does

#include <stdio.h>
int main() {
    int i = 0;

    while(i < 10) {
        if(i < 7)
            printf("value is%d\n", i++);
    }
}
Akira
  • 4,385
  • 3
  • 24
  • 46
GANESH GANI
  • 101
  • 11
  • 3
    its called flushing – Vishwajeet Vishu Jul 05 '17 at 06:53
  • 1
    By default stdout is line buffered. This means that (unless you fill some internal buffer), it will not actually print anything until you print a newline, flush stdout or the program exits (which forces a flush of stdout). Your first program never prints a newline, flushes stdout or exits. – Art Jul 05 '17 at 06:54
  • 1
    Flushing would also happen when your program terminates. But as your program never stops, this won't happen. – Gerhardh Jul 05 '17 at 07:31

3 Answers3

5

First note both your programs never exit. When i hits 7 it's game over and your stuck forever doing nothing.

Second note the only difference is printing a new line. That should have been your clue. Since the loop is infinite you never print a new line or exit - both things flush STDOUT. Until you flush STDOUT you're just accumulating what you're trying to print in a buffer. Only the flushing will get it on screen, and clean the buffer.

@chux added a good point:

Buffering of STDOUT (or IN or ERR) is implementation defined, meaning different flavors of Linux, Windows, and so forth may display different behavior. Obviously in the OP STDOUT is buffered - as no output appears.

kabanus
  • 24,623
  • 6
  • 41
  • 74
  • I think, the C language does not demand `exit` to flush... Am I wrong ? – alinsoar Jul 05 '17 at 08:12
  • 1
    I checked now, yes, explicit `exit` is required to flush``Next, all open streams with unwritten buffered data are flushed, all open streams are closed, and all files created by the tmpfile function are removed.`` – alinsoar Jul 05 '17 at 08:16
  • "Until you flush STDOUT you're just accumulating what you're trying to print in a buffer." --> Maybe. It is implementation defined behavior. `stdout` might print out under various conditions and modes and not necessarily just accumulate. See [What are the rules of automatic flushing stdout buffer in C?](https://stackoverflow.com/q/39536212/2410359) – chux - Reinstate Monica Jul 05 '17 at 18:27
  • @chux correct, but it will only "print" when it's "flushed". I just gave the common occurrences. Obviously STDOUT is not flushed in OPs example. – kabanus Jul 05 '17 at 18:30
  • Disagree with only "print" when it's "flushed". It is implementation defined behavior when the printing occurs without a "flush". With a `"flush" the behavior is well defined. – chux - Reinstate Monica Jul 05 '17 at 18:35
  • @chux Agree then, I misunderstood you. I'm adding your point to the answer. – kabanus Jul 05 '17 at 18:38
2

When you use printf() it just puts your data into STDOUT buffer, its purpose is not to display onto screen. but when you use \n inside the printf() if flushes the STDOUT buffer on screen. you can also use fflush() if you are not using \n. From fflush() man page

For output streams, fflush() forces a write of all user-space buffered data for the given output or update stream via the stream's underlying write function. For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been con- sumed by the application. The open status of the stream is unaffected.

Vishwajeet Vishu
  • 492
  • 3
  • 16
0

Please read this.

For example simply including a "\n" in the printed stuff will typically flush it (because stdout is by default line-buffered when attached to a terminal).

#include<stdio.h>
int main() {
    int i=0;
    while(i<10) {
        if(i<7)
            printf("value is%d",i++);
        fflush(stdout);
    }
}
Seoul
  • 585
  • 1
  • 4
  • 14