Home Segments Index Top Previous Next

172: Sidetrip

You can, if you want, use conditional-operator expressions in place of if statements, as in the following example, in which one of the two display expressions is evaluated:

#include  
main ( ) { 
  int change; 
  scanf ("%i", &change); 
  change == 1 
    ? 
    printf ("The price has changed by %i point.\n", change) 
    : 
    printf ("The price has changed by %i points.\n", change) 
    ; 
} 

This program not only reintroduces needless duplication, but also introduces a conditional-operator expression that is not inside any other expression that can make use of the value produced. Instead, the conditional-operator expression forms a complete statement. Such use of the conditional operator is considered poor programming practice, and your C compiler should issue a warning.