| United States-English |
|
|
|
![]() |
HP C/HP-UX Reference Manual: HP-UX Systems > Chapter 2 Program Organization Declarations |
|
In general, a variable declaration has the following format: [storage_class_specifier] [data_type] variable_name[LINEBREAK] [=initial_value];
Here are a few sample variable declarations without storage class specifiers or initial values: int age; /* integer variable "age" */[LINEBREAK]int length, width; /* abbreviated declaration of two[LINEBREAK] variables*/[LINEBREAK]float ph; /* floating-point variable "ph" */[LINEBREAK]char a_letter; /* character variable "a_letter" */[LINEBREAK]int values[10]; /* array of 10 integers named values */[LINEBREAK]enum days {mon, wed, fri}; /* enumerated variable "days" */ The C language allows you to create your own names for data types with the typedef keyword. Syntactically, a typedef is similar to a variable declaration except that the declaration is preceded by the typedef keyword. A typedef declaration may appear anywhere a variable declaration may appear and obeys the same scoping rules as a normal declaration. Once declared, a typedef name may be used anywhere that the type is allowed (such as in a declaration, cast operation, or sizeof operation). You can write typedef names in all uppercase so that they are not confused with variable names. You may not include an initializer with a typedef. typedef long int FOUR_BYTE_INT; makes the name FOUR_BYTE_INT synonymous with long int. The following two declarations are now identical: Typedefs are useful for abstracting global types that can be used throughout a program, as shown in the following structure and array declaration: typedef struct {[LINEBREAK] char month[4];[LINEBREAK] int day;[LINEBREAK] int year;[LINEBREAK]} BIRTHDAY;[LINEBREAK] [LINEBREAK]typedef char A_LINE[80]; /* A_LINE is an array of[LINEBREAK] * 80 characters */ Type definitions can be used to compensate for differences in C compilers. For example: #if SMALL_COMPUTER[LINEBREAK] typedef int SHORTINT;[LINEBREAK] typedef long LONGINT;[LINEBREAK]#elif[LINEBREAK] BIG_COMPUTER[LINEBREAK] typedef short SHORTINT;[LINEBREAK] typedef int LONGINT;[LINEBREAK]#endif This is useful when writing code to run on two computers, a small computer where an int is two bytes, and a large computer where an int is four bytes. Instead of using short, long, and int, you can use SHORTINT and LONGINT and be assured that SHORTINT is two bytes and LONGINT is four bytes regardless of the machine. You can use typedefs to simplify complex declarations. For example: typedef float *PTRF, ARRAYF[], FUNCF(); This declares three new types called PTRF (a pointer to a float), ARRAYF (an array of floats), and FUNCF (a function returning a float). These typedefs could then be used in declarations such as the following: PTRF x[5]; /* a 5-element array of pointers to floats */[LINEBREAK]FUNCF z; /* A function returning a float */ The following two examples illustrate what can happen when you mix pointers and typedefs that represent arrays. The problem with the program on the left is that ptr points to an array of 80 chars, rather than a single element of a char array. Because of scaling in pointer arithmetic, the increment operator adds 80 bytes, not one byte, to ptr. Table 2-1 Title not available (Using typedefs for Arrays )
All identifiers (names) in a program fall into one of four name spaces. Names in different name spaces never interfere with each other. That is, you can use the same name for an object in each of the four name spaces without these names affecting one another. The four name spaces are as follows:
The following example uses the same name, overuse, in four different ways: int main(void)[LINEBREAK]{[LINEBREAK] int overuse; /* normal identifier */[LINEBREAK] struct overuse { /* tag name */[LINEBREAK] float overuse; /* member name */[LINEBREAK] char *p;[LINEBREAK] } x;[LINEBREAK] goto overuse;[LINEBREAK]overuse: overuse = 3; /* label name */[LINEBREAK]} Each struct, union, or enum defines its own name space, so that different declarations can have the same member names without conflict. The following is legal: struct A {[LINEBREAK] int x;[LINEBREAK] float y;[LINEBREAK]};[LINEBREAK]struct B {[LINEBREAK] int x;[LINEBREAK] float y;[LINEBREAK]}; The members in struct A are distinct from the members in struct B. Macro names do interfere with the other four name spaces. Therefore, when you specify a macro name, do not use this name in one of the other four name spaces. For example, the following program fragment is incorrect because it contains a macro named square and a label named square: #define square(arg) arg * arg[LINEBREAK] [LINEBREAK]int main(void)[LINEBREAK]{[LINEBREAK] ...[LINEBREAK]square: [LINEBREAK] ...[LINEBREAK]} 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.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||