4

So I'm a total newb to C. I'm using eclipse with MinGW compiler. I'm on the second chapter using the scanf and printf functions and my program is working, but only printing the statements to the console once I've entered the three ints into the scanf functions.

#include <stdio.h>
int main(void){
    int length, height, width, volume, dweight;

    printf("Enter the box length: ");
    scanf("%d", &length);
    printf("\nEnter the box width: ");
    scanf("%d", &width);
    printf("\nEnter the box height");
    scanf("%d", &height);

    volume = length * width * height;
    dweight = (volume + 165) / 166;

    printf("Dimensions: l = %d, w = %d, h = %d\n", length, width, height);
    printf("Volume: %d\n", volume);
    printf("Dimensional Width: %d\n", dweight);

    return 0;
}

console output:

8 (user input + "Enter" + key)
10 (user input + "Enter" key)
12 (user input + "Enter" key)
Enter the box length: 
Enter the box width: 
Enter the box heightDimensions: l = 8, w = 10, h = 12
Volume: 960
Dimensional Width: 6

any insights? I'm expecting it to printf, then scanf for user input like so:

Enter the box length: (waits for user int input; ex. 8 + "Enter")
Enter the box width: ...
ChrisMcJava
  • 2,145
  • 5
  • 25
  • 33

2 Answers2

7

Just add fflush(stdout); after each printf() before you call scanf():

#include <stdio.h>
int main(void){
    int length, height, width, volume, dweight;

    printf("Enter the box length: "); fflush(stdout);
    scanf("%d", &length);
    printf("\nEnter the box width: "); fflush(stdout);
    scanf("%d", &width);
    printf("\nEnter the box height"); fflush(stdout);
    scanf("%d", &height);

    volume = length * width * height;
    dweight = (volume + 165) / 166;

    printf("Dimensions: l = %d, w = %d, h = %d\n", length, width, height);
    printf("Volume: %d\n", volume);
    printf("Dimensional Width: %d\n", dweight);

    return 0;
}
phonetagger
  • 7,701
  • 3
  • 31
  • 55
  • that did it. why is that? – ChrisMcJava Sep 17 '13 at 19:01
  • 2
    On your system, the text in stdout remained buffered in memory and was waiting until either you printed enough more stuff that it filled the internal buffer, or else you explicitly flushed the content of the buffer. This is for efficiency reasons. Buffered output is generally much more efficient I/O... the system sends the buffer as a single large chunk of output, rather than a little bit at a time. In your case, that's not the behavior you wanted. A different system might behave entirely different in this respect; it's implementation-dependent behavior. – phonetagger Sep 17 '13 at 19:09
  • 2
    Incidentally, another event that could have triggered a flush is for your program to exit. So if you've been programming for a while and never had to do that, it's because you've probably never used scanf(). Another possibility is that `'\n'` in the output stream might trigger a flush. A newline isn't required to trigger a flush, but it might do so on some systems. – phonetagger Sep 17 '13 at 19:14
1

Dealing with Dirty Buffers in C !!

You can simply include a newline character (escape sequence) '\n' at the end of each printf(), this serves for flushing the buffers, that eventually enable the display on the output terminal.( same functionality is achieved by fflush(stdout) but there is no need to write it every time you call the printf(), just include a character '\n')

Note: Its always recommended to use a '\n' character as the last element inside the quotes "" for printf(), as the data will stay within the buffers unless a flushing mechanism is used, however the buffers are flushed automatically when the main() function ends, Moreover, data reaches its destination only when the interim buffers are flushed.

Our new code should look like this:

#include <stdio.h>
int main(void){
    int length, height, width, volume, dweight;
    printf("Enter the box length: \n");
    scanf("%d", &length);
    printf("\nEnter the box width: \n");
    scanf("%d", &width);
    printf("\nEnter the box height \n");
    scanf("%d", &height);
    volume = length * width * height;
    dweight = (volume + 165) / 166;
    printf("Dimensions: l = %d, w = %d, h = %d\n", length, width, height);
    printf("Volume: %d\n", volume);
    printf("Dimensional Width: %d\n", dweight);
    return 0;
}

Console output :

Enter the box length: 
8
Enter the box width:  
10
Enter the box height 
12
Dimensions: l = 8, w = 10, h = 12
Volume: 960
Dimensional Width: 6
  • 1
    Putting a newline character at the end of `printf()` before a `scanf()` does not seem to make any difference for me. –  Aug 06 '20 at 02:40