Statements And Declarations Inside Of Expressions - Intel i960 User Manual

Processor compiler
Hide thumbs Also See for i960:
Table of Contents

Advertisement

7
7-40
i960 Processor Compiler User's Guide

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
operand has side effects. If you know the type of the operands (you can
assume
), you can define the macro safely as follows:
int
#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
declaration with an initializer. Here is how to define
for the type of
:
exp
typedef name = exp ;
.
or
twice, with bad results if the
a
b
typedef
as a type name
name

Advertisement

Table of Contents
loading

Table of Contents