Extensions To The C Language Family; Statements And Declarations In Expressions - Red Hat ENTERPRISE LINUX 4 Manual

Hide thumbs Also See for ENTERPRISE LINUX 4:
Table of Contents

Advertisement

GNU C provides several language features not found in ISO standard C. (The
directs GCC to print a warning message if any of these features is used.) To test for the availability of
these features in conditional compilation, check for a predefined macro
defined under GCC.
These extensions are available in C and Objective-C. Most of them are also available in C++. Chapter
7 Extensions to the C++ Language, for extensions that apply only to C++.
Some features that are in ISO C99 but not C89 or C++ are also, as extensions, accepted by GCC in
C89 mode and in C++.

6.1. Statements and Declarations in Expressions

A compound statement enclosed in parentheses may appear as an expression in GNU C. This allows
you to use loops, switches, and local variables within an expression.
Recall that a compound statement is a sequence of statements surrounded by braces; in this construct,
parentheses go around the braces. 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
The last thing in the compound statement should be an expression followed by a semicolon; the
value of this subexpression serves as the value of the entire construct. (If you use some other kind of
statement last within the braces, the construct has type
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)
But this definition computes either
GNU C, if you know the type of the operands (here taken as
follows:
#define maxint(a,b) \
({int _a = (a), _b = (b); _a
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.
If you don't know the type of the operand, you can still do this, but you must use
6.6 Referring to a Type with

Extensions to the C Language Family

(b) ? (a) : (b))
or
a
b
).
typeof
, and thus effectively no value.)
void
twice, with bad results if the operand has side effects. In
int
_b ? _a : _b; })
Chapter 6.
-pedantic
, which is always
__GNUC__
), you can define the macro safely as
typeof
option
.
foo ()
(Section

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the ENTERPRISE LINUX 4 and is the answer not in the manual?

Table of Contents