1

I am doing a simple C program that accepts input and display some number.

#include<stdio.h>
#include<conio.h>

int main()
{
    // setvbuf(stdout, NULL, _IONBF, 0);
    int a, b, c;
    printf("Enter two values: \n");
    scanf("%d %d", &a, &b);
    c = a + b;
    printf("Total answer is %d", c);
    return 0;
}

I am using gcc as compiler.

So the process is there is a text that asks for input, then input two numbers and display the result.

Now, if I compile and run my code in windows cmd, It works perfectly. No problem at all.

But, if I compile and run using git bash, it asks first for numbers before displaying the prompt that asks for number. It is like it runs the scanf() first before the first printf() function.

Do you know why? I am just curious why it gives different result.

melpomene
  • 84,125
  • 8
  • 85
  • 148
Dan Angelo Alcanar
  • 369
  • 2
  • 3
  • 13
  • 3
    `` is not a standard header. – melpomene Mar 06 '18 at 14:15
  • 3
    Sounds like stdout is block buffered for some reason (apparently your program thinks output doesn't go to the console?). – melpomene Mar 06 '18 at 14:17
  • @melpomene Hi, I am not aware of that. Is it why I am having a different ouput? Thankyou. – Dan Angelo Alcanar Mar 06 '18 at 14:18
  • @melpomene Yes maybe. As you can see, there is a commented code, that I copy pasted from stackoverflow. That solves the problem. I am just curious why. – Dan Angelo Alcanar Mar 06 '18 at 14:20
  • 4
    Append `fflush(stdout);` after `printf("Enter two values: \n");` to insure prior printing. – chux - Reinstate Monica Mar 06 '18 at 14:28
  • 3
    [What are the rules of automatic flushing stdout buffer in C?](https://stackoverflow.com/q/39536212/2410359) may be useful. OP was curious there too. – chux - Reinstate Monica Mar 06 '18 at 14:31
  • 2
    It would be better if the result output included a newline at the end. Ideally, you should check that the `scanf()` call successfully read 2 values, too. And it is often good to echo the input values as well as showing the result. – Jonathan Leffler Mar 06 '18 at 14:42
  • @chux Shouldn't `printf("...\n")` flush stdout? Or is that implementation-defined? – Lundin Mar 06 '18 at 15:10
  • 1
    @Lundin If `printf("...\n")` flushes `stdout` is [implementation-defined](https://stackoverflow.com/a/39536803/2410359) per C11dr §7.21.3 3 "Support for these characteristics is implementation-defined". – chux - Reinstate Monica Mar 06 '18 at 15:13
  • it is a very poor programming practice to include header files those contents are not used. Suggest removing the statement: `#include ` – user3629249 Mar 07 '18 at 06:54
  • when calling any of the `scanf()` family of functions, always check the returned value (not the parameter values) to assure the operation was successful. in the posted code, any value other than 2 indicates an error occurred. – user3629249 Mar 07 '18 at 06:56
  • after removing the non-standard/unused header file: conio.h, I compiled and ran the code under linux. the result is: `Enter two values: Total answer is 3` Note however, that the last line is not actually displayed until the program exits (which causes `stdout` to be flushed. When writing items to `stdout` (or `stderr`), always either end the output with '\n' or follow the call with `fflush( stdout );` – user3629249 Mar 07 '18 at 07:00
  • Thank you all. I will do a google search about the things you commented here. Thankyou... – Dan Angelo Alcanar Mar 07 '18 at 12:27

0 Answers0