simple c program not printing output

22,638

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?

Share:
22,638
isnvi23h4
Author by

isnvi23h4

Hi, I am a souparno majumder,   I like coding in c#, java, php and ofcoarse javascript.   If my solution has helped you please endorse me at in.linkedin.com/in/souparno here is a link to my github profile github.com/souparno thanks :)

Updated on July 09, 2022

Comments

  • isnvi23h4
    isnvi23h4 almost 2 years

    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
    Lightness Races in Orbit over 8 years
    It would still flush on program end.
  • kbau
    kbau over 8 years
    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?