0

It only prints the description when I print b again after the if statement, really weird behavior, when I remove the last line it doesn't print description is ...dos anyone know why this happens and how I can fix this? Thanks

char * b;
if (list!= NULL){
b = strdup ( (char *)g_object_get_data(G_OBJECT(list->data), "description") );
printf(" description is %s ", b);
}
printf("\nprinting b: %s\n", b);
mihajlv
  • 2,275
  • 4
  • 36
  • 59

2 Answers2

1

It's seems stdout is line-buffered, i.e. printf hoards output until it encounters a newline or its buffer fills up. Add a newline to the first printf:

printf(" description is %s\n", b);

To ensure the output buffer is flushed, you can say:

fflush(stdout);
cnicutar
  • 178,505
  • 25
  • 365
  • 392
1

You can also flush the buffer any time with fflush()

Michel
  • 2,523
  • 2
  • 17
  • 14