3.4. Strange operatorsThere are two operators left to mention which look decidedly odd. They aren't ‘essential’, but from time to time do have their uses. Don't ignore them completely. This is the only place where we describe them, so our description includes what happens when they are mixed with pointer types, which makes them look more complicated than they really are. 3.4.1. The ?: operatorLike playing the accordion, this is easier to demonstrate than to describe. expression1?expression2:expression3 If expression1 is true, then the result of the whole expression is expression2, otherwise it is expression3; depending on the value of expression1, only one of them will be evaluated when the result is calculated. The various combinations of types that are permitted for expression2 and expression3 and, based on those, the resulting type of the whole expression, are complicated. A lot of the complexity is due to types and notions that we haven't seen so far. For completeness they are described in detail below, but you'll have to put up with a number of forward references. The easiest case is when both expressions have arithmetic type (i.e. integral or real). The usual arithmetic conversions are applied to find a common type for both expressions and then that is the type of the result. For example a>b?1:3.5 contains a constant (1) of type Other combinations are also permitted.
Various pointer types can be mixed.
The type of the result when pointers are involved is derived in two separate steps.
Qualifiers, composite types and compatible types are all subjects discussed later. The shortest useful example that we can think of is this one, where the
string to be printed by
#include <stdio.h>
#include <stdlib.h>
main(){
int i;
for(i=0; i <= 10; i++){
printf((i&1) ? "odd\n" : "even\n");
}
exit(EXIT_SUCCESS);
}Example 3.9It's cute when you need it, but the first time that they see it most people look very uncomfortable for a while, then recollect an urgent appointment somewhere else. After evaluating the first operand there is one of the sequence points described in Chapter 8. 3.4.2. The comma operatorThis wins the prize for ‘most obscure operator’. It allows a list of expressions to be separated by commas: expression-1,expression-2,expression-3,...,expression-n and it goes on as long as you like. The expressions are evaluated strictly left to right and their values discarded, except for the last one, whose type and value determine the result of the overall expression. Don't confuse this version of the comma with any of the other uses C finds for it, especially the one that separates function arguments. Here are a couple of examples of it in use.
#include <stdio.h>
#include <stdlib.h>
main(){
int i, j;
/* comma used - this loop has two counters */
for(i=0, j=0; i <= 10; i++, j = i*i){
printf("i %d j %d\n", i, j);
}
/*
* In this futile example, all but the last
* constant value is discarded.
* Note use of parentheses to force a comma
* expression in a function call.
*/
printf("Overall: %d\n", ("abc", 1.2e6, 4*3+2));
exit(EXIT_SUCCESS);
}Example 3.10Unless you are feeling very adventurous, the comma operator is just as well ignored. Be prepared to see it only on special occasions. After evaluating each operand there is one of the sequence points described in Chapter 8. |
The C BookThis book is published as a matter of historical interest. Please read the copyright and disclaimer information. GBdirect Ltd provides up-to-date training and consultancy in C, Embedded C, C++ and a wide range of other subjects based on open standards if you happen to be interested. |
|
West Yorkshire Office
GBdirect Ltd
Training: 0800 651 0338 Please call between 0900 and 1700 (UK time) on Monday to Friday South East Regional Office
GBdirect Ltd
Training: 0800 651 0338 Please call between 0900 and 1700 (UK time) on Monday to Friday Please note: |