2

I have a simple c program

#include <stdio.h>

int add(int, int);
int add (int a, int b) {
   return a+b;
}

int main(void) {
  int a, b, c;

  printf("Enter the 1st number ");
  scanf("%d",&a);
  printf("Enter the 2nd number ");
  scanf("%d",&b);
  c = add(a, b);
  printf("The value is %d ", c);
  return (0);
}

I am compiling the program with cc main.c and when I am running the program with ./a.out
I am not getting any output in the console.

Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989
souparno majumder
  • 1,521
  • 1
  • 21
  • 39

1 Answers1

5

The output is buffered for performance reasons. Replace

printf("The value is %d ", c); 

with

printf("The value is %d\n", c);

or use fflush(stdout);.

See Why does printf not flush after the call unless a newline is in the format string?

Community
  • 1
  • 1
user2644013
  • 172
  • 1
  • 13
  • 1
    It would still flush on program end. – Lightness Races in Orbit Aug 09 '15 at 13:48
  • 1
    I'm not sure why, but sometimes the newline character is not enough. I'm running mingw gcc (can't remember the exact version, but it might 4.9.2) and even with a newline character at the end of a string doesn't output it unless fflush(stdout) is called. P.S. who downvoted? It's a good answer, no? – kbau Aug 09 '15 at 13:48