C has two numeric types, int and float. A real can be double or long double, and an int may be short, long, or unsigned. There is a character type char, but no Boolean type. In C, any nonzero value is considered to be true and 0 false. Since C is close to the machine, certain nonprinting character constants are available, such as \b for backspace.
Types derived from the simple types above are :
Arrays :
Example : char name [25]
Pointers :
Example : int *pn
Structures : struct [<structure name>] {
Example : typedef struct {int day, month, year;} date;
Or : struct hire_date {int day, month, year;};
Using the typedef for date, we can then declare: date hire_date; and assign its fields as follows:
hire_date.day = 25;
hire_date.month = 9;
(&hire_date) -> year = 1990;
Note here the combination (&....)->.-> is a special symbol meaning "the field of the structure (union) pointed to by the variable on the left." Parentheses are needed around (&hire_date) because -> has precedence over &.
Unions: union [<union name>
unions are always discriminated, so no ambiguities can occur. For Example :
typedef union {int iarg; float farg;} numeric_const;
numeric_const pi, zero;
(&pi) -> farg = 3.141592; zero.iarg = 0;
Unions and strucures are declared similarly, but in a struct (C's record), storage is allocated for all fields, whereas in a union, storage is allocated for the largest variant, and only one is assigned to a union variable. Pascal's variant record can be created in C if desired, since a union can be a field of a record (and vice versa).
Function :
parameter definitions;
{
local declarations;
statements;
}
A functional value may be of any type except another function or an array. In functions returning integers, the value type may be omitted. A function returning no value is of void type.
For Example :
void swap (px,py)
float *px; float *py;
{.....}
One important difference between C and Pascal functions is that no type checking occurs on either the number or type of parameters when a function is called, if the function is defined as above in the so-called classic style. More modern versions of C include a modern style, where parametertype information is included in the parameter list, and type checking can occur.
For Example:
void swap (float *px, float *py)
{....}
Another difference is that parameters are always passed by value, except for arrays.
f(a) will pass a pointer to the first element of the array a,a[0]. Reference calls are quite easily achieved by passing addresses.
C is commonly organized into modules of three types: manifest constants (macros), external variables (array and string initializations), and function definitions. These can be organized for separate compilation but may also reside in the same file.
No comments:
Post a Comment