LurchiDerLurch
LurchiDerLurch

Reputation: 139

How does the infinite loop further down stop earlier code from execution?

I've come across this weird situation i can't wrap my head around. In the following code, the Program is apparantly enetering the infinite loop, but doesn't execute the code that comes before it.

#include <stdio.h>
#include <stdlib.h>

int main() {
    char buf[100];

    if (scanf("%s", buf)==EOF)  return 0;

    printf("This does not get printed");

    while(1) {} //infinite loop
    return 1;
}

Somehow, the printf command does not get executed, even after pressing enter or using Ctrl-d. However, it seems as if the code would end up in the infinite loop.

Does anyone explain whats going on here? I'm using gcc.

Upvotes: 1

Views: 76

Answers (1)

David Schwartz
David Schwartz

Reputation: 182769

Standard output is line buffered by default. If you write less than a line to the output, some or all of it can be delayed until more output occurs.

Upvotes: 3

Related Questions