-2

Printf() on following code may behave incorrectly on some machine (including mine).

printf("\n number of factor %d\n", sum); 
fflush(stdout);
if(sum == 0) 
{   
   printf("\n The  number %d is prime", p);
} 

The code first prints out the variable ``sum'', and if, the sum is zero then it prints out the number.

While first printf prints correctly but second printf statement doesn't print. Am I missing something here?

piyush_sao
  • 548
  • 2
  • 8
  • 25

1 Answers1

1

You are either missing a newline at the end of the printf format string, or another call to fflush

example why fflush is important

// file exflu.c
#include <stdio.h>
#include <unistd.h>
int main(int argc, char**argv) {
   int i=0;
   printf ("without newline from %s", argv[0]);
   // add perhaps a fflush(stdout); here
   sleep(5);
   scanf ("%d", &i);
   printf(" i=%d\n", i);
   return 0;
}

Observe the above program with and without fflush; without it, the message appears after five seconds (because the scanf is probably doing a fflush(NULL) implicitly).

Basile Starynkevitch
  • 1
  • 16
  • 251
  • 479