0

Taking an Online C course, and when I did this for one of the homework questions, it does not work as I intend it to. It is supposed to prompt the user to enter the radius, and then do the calculations and print out the answer. What it DOES is print out nothing, but executes fine, and when I enter a number, THEN it prints out the prompt as well as the answer.

Basically, why wont it prompt to do the input first, as it is written in the code :

int main(void)
{
    float volume, radius;
    printf("Enter the radius of the sphere: \n");
    scanf("%f", &radius);
    volume = (4.0f / 3.0f) * 3.14f * radius * radius * radius;
    printf("%.2f", volume);
    return 0;
}
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
Dragon Wolf
  • 61
  • 1
  • 2
  • 11
  • 6
    Prompts for me. What's your environment? Also, you should `#include `. – Joachim Isaksson Jul 03 '13 at 09:27
  • possible duplicate of [Does reading from stdin flush stdout?](http://stackoverflow.com/questions/2123528/does-reading-from-stdin-flush-stdout) (that question involves output without a trailing `\n`, but the answers discuss the (in)significance of that...) – Tony Delroy Jul 03 '13 at 09:42

1 Answers1

8

Your code works as expected under my Visual Studio 2010 and with Linux/GCC, with #include <stdio.h>at the beginning. However, I do not know what your development environment is.

Try to insert an fflush(stdout); after your first printf.


This behavior as well-known with Eclipse (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=173732).

Eclipse CTD developper Anton Leherbauer gives some explanation here:

The problem is: The eclipse console is not a terminal. On unix systems, the method of choice is to create a pseudo terminal (pty) and connect the child process stdio to it. This way the child process behaves exactly in the same way as in an interactive terminal. CDT does exactly that for Linux, Mac OS X, Solaris and some other supported platforms. We don't have a Pty (or something of that kind) on Windows, therefore interactive console mode programs don't work as expected.

[...]

Eclipse has no way of flushing a buffer it does not control. Flushing on "\n" would indicate line buffering mode, which is currently not possible on Windows as I tried to explain.

Matthieu Rouget
  • 3,289
  • 18
  • 23
  • I am using the EclipseC/C++ IDE with MiniGW, since I use eclipse for java and android stuff as well. For the record I had the #include I just didn't put it in the post. Putting fflush(stdout) after the first printf did the trick, but why do I need that line? My book doesn't even mention this, can someone help explain why I needed the fflush for it to work properly in Eclipse? – Dragon Wolf Jul 03 '13 at 18:23
  • Normally, stdout is _line-buffered_, so anything you output using `printf()` can (but not necessarily _will_) sit in an output buffer until you print a newline (`'\n'`), at which point it gets flushed, and displayed to the user. However, when the output channel is part of a pipeline instead of being connected directly to your terminal, then stdout will typically be _fully buffered_, so even if you print a string that ends in a newline, it won't necessarily become visible to whatever's reading the output until your program has filled the output buffer, which could be any size but is typically – This isn't my real name Jul 04 '13 at 01:35
  • somewhere between 512 bytes and 2 kilobytes. The way around this buffering, of course, is the `fflush()` function, which forces the output buffer to be flushed even if you haven't printed that newline, or printed 2 kilobytes of data. Now, I don't *know* this, but given the behavior your describe, I speculate that when you're running your program from inside eclipse, it's running your program inside a pipeline so that it can catch the output for you, and this is resulting in your stdout being fully buffered instead of line buffered. – This isn't my real name Jul 04 '13 at 01:38