1
#include <stdio.h>

int main(void)
{
    printf("output test");
    int y = 8;
    printf("this is a test %d", y);
    return 0;
}
  1. Correctly output to Debug Console when running the program.
  2. NO output to Debug Console or Output window when step by step debugging.
  3. MAC intel chip computer
  4. This .c file is in a project with launch.json has externalConsole as false.
  5. gcc compiler or clang is used
2
  • 4
    Perhaps you should fflush(stdout) after each output. None of that will happen until the end of the program, because there are no newlines output. Commented Oct 19 at 9:26
  • 1
    haha, thanks. I added \n and it show up in the Debug Console, Thank you so much. Commented Oct 19 at 9:43

3 Answers 3

4

The content you have passed to printf will not make their way to the console until the end of the program unless there is a newline in the content. You can force the content to be shown in the console via fflush(stdout) though.

The behavior you have seen was that without the debug, the program finished and hence the output appeared, whilst while you have been debugging, unless you reached the end of the program, then the print was not showing up. Hence, adding newlines or calling fflush are the solution.

Sign up to request clarification or add additional context in comments.

Comments

3

A further option on top of newlines and explicitly flushing output with fflush is if your text is intended not to be normal output but rather a debugging or error message, you may wish to printf to stderr which is typically unbuffered and printed immediately.

#include <stdio.h>

int main(void)
{
    fprintf(stderr, "output test");
    int y = 8;
    fprintf(stderr, "this is a test %d", y);
    return 0;
}

Of course, even then, the messages you're printing are probably better with trailing newlines.

Comments

2

The console output is usually line buffered. A text is not displayed until a new line starts or program ends. This updated code should behave like you expect that. fflush(stdout) is unneeded usually.

printf("output test\n");
int y = 8;
printf("this is a test %d\n", y);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.