samedi 30 juin 2018

Why do infinite loops delay printing? [duplicate]

This question already has an answer here:

This is a backstory, you can skip to the question in bold below if you don't wanna read it.

Recently while solving a problem I accidentally had an infinite loop in it. But I didn't know so I tried to debug the code, and obviously a good way to debug ( or at least a way that I'm used to ) is to place a few flags in the code, that would print to the console that the program has finished successfully until that part. But later I saw that even when I place the flag as the first thing in the program, it still wouldn't print anything. I managed to fin the bug by using return 0 instead of placing flags, but this got me wondering,

Why doesn't the following program print "A" right away?

#include<bits/stdc++.h>
using namespace std;
int main(){
    cout<<"A";
    int k=1;
    while(k)k++;
}

Aucun commentaire:

Enregistrer un commentaire