mihajlv
mihajlv

Reputation: 2315

Weird Behavior using gcc and glib?

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);

Upvotes: 0

Views: 100

Answers (2)

Michel
Michel

Reputation: 2623

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

Upvotes: 1

cnicutar
cnicutar

Reputation: 182629

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);

Upvotes: 1

Related Questions