Primary Expressions in C Programming

0
Primary expressions in C programming are the fundamental building blocks of C programs. They include identifiers, constants, string literals, parenthesized expressions, and generic selections. Let's delve into each of these:

Identifiers

An identifier is a primary expression, provided it has been declared as designating an object (in which case it is an lvalue) or a function (in which case it is a function designator). Thus, an undeclared identifier is a violation of the syntax.

Constants

A constant is a primary expression. Its type depends on its form and value.

String Literals

A string literal is a primary expression. It is an lvalue with type as detailed in 6.4.5.

Parenthesized Expressions

A parenthesized expression is a primary expression. Its type and value are identical to those of the unparenthesized expression. It is an lvalue, a function designator, or a void expression if the unparenthesized expression is, respectively, an lvalue, a function designator, or a void expression.

Generic Selections

A generic selection is a primary expression. Its type and value depend on the selected generic association. The controlling expression of a generic selection is not evaluated. If a generic selection has a generic association with a type name that is compatible with the type of the controlling expression, then the result expression of the generic selection is the expression in that generic association. Otherwise, the result expression of the generic selection is the expression in the default generic association. None of the expressions from any other generic association of the generic selection is evaluated.

The type and value of a generic selection are identical to those of its result expression. It is an lvalue, a function designator, or a void expression if its result expression is, respectively, an lvalue, a function designator, or a void expression.

For example, the `cbrt` type-generic macro could be implemented as follows:

#define cbrt(X) _Generic((X), \
long double: cbrtl, \
default: cbrt, \
float: cbrtf \
)(X)


Tags

Post a Comment

0Comments
Post a Comment (0)