c - What happens inside of this condition statement? while (a = foo(bar)) -


it may sounds silly, want know happening when execute while(a = function(b)){}.

suppose got null return value of read_command_stream.

can out of loop?

while ((command = read_command_stream (command_stream))) {     if (print_tree)     {         printf("# %d\n", command_number++);         print_command (command);     }     else     {         last_command = command;         execute_command(command, time_travel);     } } 

struct command

struct command {   enum command_type type;    // exit status, or -1 if not known (e.g., because has not exited yet).   int status;    // i/o redirections, or null if none.   char *input;   char *output;    union   {     // and_command, sequence_command, or_command, pipe_command:     struct command *command[2];      // simple_command:     char **word;      // subshell_command:     struct command *subshell_command;   } u; }; 

the syntax says:

while (expression) { ... } 

and expression can lot. can be:

  • a constant: while (1) { ... }
  • the result of comparison: while (a < b) { ... }
  • some boolean construct: : while (a < b && c < d ) { ... }
  • the resulting expression assignment: while (*dst++ = *src++) {;}
  • and assignment can involve function calls: while((ch = getc()) != eof) { ... }
  • a plain variable: while(i) ( ...)
  • an expression based on evaluation of plain variable : while (i--) { ... } (even side effects!)
  • a pointer expression: : while (*++argv) { ... }

now, in case of integer expression, expression checked not equal zero. pointer expressions, checked against not equal null. that's all.

the crux of in c, even assignment expression, can write:

a = b = c; 

or even: = b = c = d;

but, since assignment expression, write:

while ( = b = c = d) { ... } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -