0

I noticed a weird behaviour of some of my programs in c++ and when I was trying to figure out what coused it, I found out, that something wrong is going on with my console outputs. I used iostream and cstdio functions with the same behaviour. When I print something on console It doesn't display at all. Here are codes that I used for observing this strange behaviour. This piece of code outputs everything propertly (even if it shouldn't IMO):

#include <cstdio>
using namespace std;
int main(void) {
    int a = 0;
    scanf("%d", &a);
    a++;
    printf("result is %d", a);
}

This one however(correct I think) doesn't display anything, only the run finished message

#include <cstdio>
using namespace std;
int main(void) {
    int a = 0;
    scanf("%d", &a);
    a++;
    printf("result is %d \n", a);
}

I also tried it with removed space before "\n" with no difference. However, when I place more the same printf functions with "\n" at the end to the program, everything displays correctly (multiple times of course). Iostream behaves in similar way - using endl doesn' t cause anything to appear on console. What am I doing wrong? As to the original code that caused malfunctioning, I noticed that on my output nothing appeared but in my school, the same code output everything correctly. I am working under NetBeans 8.0.2. Thanks in advance for help

Cory Kramer
  • 98,167
  • 13
  • 130
  • 181
fifco
  • 11
  • 4
  • 2
    Not much C++ in those code snippets really. – Some programmer dude Dec 06 '14 at 17:11
  • 1
    As for your problem, what do you mean with "doesn't display anything"? Your call to `scanf` will *block* until you enter a newline. – Some programmer dude Dec 06 '14 at 17:14
  • Since you tagged this as C++, try using `cin` and `cout`. :-) – Thomas Matthews Dec 06 '14 at 17:14
  • 1
    Also, `stdout` (which is used by `printf`) is *line* buffered, you need to print a newline for the output buffer to be flushed. – Some programmer dude Dec 06 '14 at 17:14
  • Seems okay to me. http://i.imgur.com/q7SNOIw.png – angelsl Dec 06 '14 at 17:23
  • Yes that' s the thing I am asking about - output should be displayed after buffer flushing with newline character. Instead, when I put newline character there, no console output is generated. However if i don' t put it ther (the first code), output appears as if i flushed the buffer. Using cout doesn' display anything either. Since you can't reproduce it, there seems to be a problem on my console, is something like that possible?. – fifco Dec 06 '14 at 17:23
  • I recommend using the standard library for display (std::cout): https://stackoverflow.com/questions/2872543/printf-vs-cout-in-c/20238349. It is more modern than function printf from C. – User123 Feb 26 '20 at 15:09

1 Answers1

0

All output data is buffered, before it getting printed to console. You can use fflush or \n to flush the output stream and print all data.

TrezzJo
  • 120
  • 1
  • 8
  • yes but that' s the weird behaviour - when i use the newline character, nothing is printed - as if the buffer was flushed somewhere else not to my console – fifco Dec 06 '14 at 17:25