Wednesday, 15 May 2013

c system calls being ran before the if statement -



c system calls being ran before the if statement -

so here problem, given code, added before please input name ignored, , automatically first instruction run(without first checking if x 5, input name, instructions prior run. if first line of programme printf, ignored , inquire input print printf statement though first. please help

int main(int argc, char** argv) { char val[70]; int x=3; if(x>5) { if(write(1, "please input name", 22)!=22) { homecoming -1; } if(read(0, val, 36) < 0) {} if(write(1, val, 36)!=36) {} } }

printf works on stdout file*. read() , write() calls work straight on file descriptors.

a file* buffered, meaning stuff printf resides in buffer in programme until flushed. while write() sends info straight operating system, without buffering in application.

so flush file* buffer create output appear:

int main(int argc, char** argv) { char val[70]; int x=3; printf("hello"); fflush(stdout);

and if stdout terminal, auto flushed when write newline, e.g. printf("hello\n");

c

No comments:

Post a Comment