EasyManua.ls Logo

Intel i960 - Statements and Declarations Inside of Expressions

Intel i960
347 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
i960 Processor Compiler User's Guide
7-40
7
Statements and Declarations Inside of Expressions
A compound statement in parentheses can appear inside an expression.
This allows you to declare variables within an expression. For example:
({ int y = foo (); int z;
if (y > 0) z = y;
else z = - y;
z; })
is a valid (though slightly more complex than necessary) expression for
the absolute value of
foo().
This feature is especially useful in making macro definitions "safe" (so
that they evaluate each operand exactly once). For example, the
"maximum" function is commonly defined as a macro in standard C as
follows:
#define max(a,b) ((a) > (b) ? (a) : (b))
But this definition computes either a or b twice, with bad results if the
operand has side effects. If you know the type of the operands (you can
assume
int), you can define the macro safely as follows:
#define maxint(a,b) \
({int _a = (a), _b = (b); _a > _b ? _a : _b; })
Embedded statements are not allowed in constant expressions, such as the
value of an enumeration constant, the width of a bit field, or the initial
value of a static variable.
Naming an Expression’s Type
You can give a name to the type of an expression using a typedef
declaration with an initializer. Here is how to define
name
as a type name
for the type of
exp
:
typedef
name
=
exp
;

Table of Contents

Related product manuals