CATEGORII DOCUMENTE |
Asp | Autocad | C | Dot net | Excel | Fox pro | Html | Java |
Linux | Mathcad | Photoshop | Php | Sql | Visual studio | Windows | Xml |
There 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.
Like playing the accordion, this is easier to demonstrate than to describe.
expression1?expression2:expression3If 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>bcontains a constant (1) of type int
and another (3.5) of type double
. Applying the arithmetic
conversions gives a result of type double
.
Other combinations are also permitted.
void
type, then that is the type of
the result. Various pointer types can be mixed.
void
. The type of the result when pointers are involved is derived in two separate steps.
void
and that is the
type of the result. If both operands are pointers to compatible types
(ignoring any qualifiers) the the result has the composite type.
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 printf
is selected using this magical operator.
Example 3.9
It'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
This 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-nand 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>Example 3.10
Unless 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
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 866
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved