开发者

C refusing to print debug info to console. Why?

My C code only prints the printf statement if I get rid of my loop. I have tried using regular while loops instead of a dowhile loop but it doesn't work开发者_开发知识库. Anyone know?

/**
 * Src for Planet Miner: Endless Space
*/

const char version[] = "a0.1_0";

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

int main()
{
    printf("Starting game... version %s", version);

    int gameRunning = 1;

    do {
        //printf("O"); debugging is fun!
    }
    while (gameRunning == 1); // Main game loop

    return 0; // End program after main loop
}

Would anyone kindly help?

I have tried using regular while loops instead of a do-while loop. However, it didn't want to print it out to the console.


You need a \n at the end of the string. There's no println unfortunately


The standard output stream stdout, which is used by printf, is buffered by default. If that stream is being directed to a terminal/console, it is usually line-buffered, otherwise it is fully buffered. Whether stdout is line-buffered or fully buffered may also depend on which platform you are using.

When stdout is buffered and you execute a printf statement that does not contain a newline character, you will usually not see the output immediately. You will only see it when the output buffer gets flushed.

Therefore, if you want to see the output immediately, you can either

  1. flush the output buffer explicitly, using fflush( stdout );, or
  2. if stdout is line-buffered, you can add a newline character to the output, which will cause the output buffer to be implicitly flushed.

The first option is probably better, because it is guaranteed to work also in situations in which stdout is not line-buffered, but fully buffered.


printf("0"); fflush(stdout); will solve your problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜