HPlogo HP C/HP-UX Reference Manual: Version A.05.55.02 > Chapter 3 Data Types and Declarations

Declarations

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

A declaration specifies the attributes of an identifier or a set of identifiers.

Syntax

declaration ::=
declaration-specifiers [init-declarator-list] ;

declaration-specifiers ::=
storage-class-specifier [declaration-specifiers]
type-specifier [declaration-specifiers]
type-qualifier [declaration-specifiers]

init-declarator-list ::=
init-declarator
init-declarator-list , init-declarator

init-declarator ::=
declarator
declarator = initializer

Description

Making a declaration does not necessarily reserve storage for the identifiers declared. For example, the declaration of an external data object provides the compiler with the attributes of the object, but the actual storage is allocated in another translation unit.

A declaration consists of a sequence of specifiers that indicate the linkage, storage duration, and the type of the entities that the declarators denote.

You can declare and initialize objects at the same time using the init-declarator-list syntax. The init-declarator-list is a comma-separated sequence of declarators, each of which may have an initializer.

Function definitions have a slightly different syntax as discussed in “Function Declarators ”. Also, note that it is often valid to define a tag (struct, union, or enum) without actually declaring any objects.

New Declaration features

HP C has added the C9x feature which allows you to declare variables and types inside a block of statements. This also allows declaration of new variables or types, such as expr_1, as shown in the for statement below:

for(expr_1;expr_2;expr_3) statement_1

This new variable or type declared in expr_1 can be used in expr_2, expr_3 and statement_1.

Caveats

The HP C/ANSI C compiler implementation of declarations within code is similar to, but not identical to, the C++ implementation of declarations within code. When specifying declarations within code in the HP C/ANSI C compiler, do not expect the same behavior in HP aC++. For example:

for(int i = 0; i < j; i ++) int i;

Note the lack of a new block opening for the for statement. The C++ compiler accepts this form, with warnings, but the C compiler does not. The difference in the way the stack is handled causes the difference in behavior.

Previously, the C compiler did not emit the source file information for the global typedefs. To correct this, use -y option along with -g when debug info is generated. You can generate debug information by compiling with +Oobjdebug.

Example

int main()
{
int i=5,j;

j=i*i;
printf(*"%d\n",j);

int k=j;
/*This is accepted in the new release of HP C*/

for(struct aa {int a;int b} AA={10,50};AA.a<=AA.b;AA.a++){
/*This is accepted by the new feature */
printf("%d\n",AA.a);}
}

Examples

Valid Declarations:

extern int pressure [ ]; /* size will be declared elsewhere */
extern int lines = 66, pages; /* declares two variables,
initializes the first one */
static char private_func (float); /* a function taking a float,
returning a char, not known
outside this unit */
const float pi = 3.14; /* a constant float, initialized */

const float * const pi_ptr = &pi; /* a constant pointer to a constant
float, initialized with an
address constant */
static j1, j2, j3; /* initialized to zero by default */
typedef struct {double real, imaginary;} Complex;
/* declares a type name */
Complex impedance = {47000}; /* second member defaults to zero */
enum color {red=1, green, blue}; /* declares an enumeration tag and
three constants */
int const short static volatile signed
really_Strange = {sizeof '\?'}; /* pretty mixed up */

Invalid Declarations:

int ; /* no identifier */
; /* no identifier */
int i; j; /* no specifiers for j */

© Hewlett-Packard Development Company, L.P.