b are allocated inside. There is not
enough space available to allocated bitfield c. A new char is then open
and bitfield c is allocated inside. This structure is thus occupying 2
bytes in memory.
A bitfield without name will be used to skip the corresponding amount
of bits in the allocation unit. A zero bitfield width is forcing the opening
of a new allocation unit.
The ANSI standard does not define in which order bitfields are filled.
The COSMIC compiler fills bitfields from the less significant bit to the
most significant bit by default. This ordering can be reversed by using
the +rev compiler option. The ANSI standard also limits the allocation
unit type to int or unsigned int. The COSMIC compiler allows all the
integer types as an extension.
Unions
3-10 Declarations © Copyright 2003 by COSMIC Software
3
By default, the compiler does not allocate the unused part of the last bit-
field if its size is larger or equal to a byte. This process can be disabled
by using the -pnb compiler option.
Unions
A union is declared like a structure, but the keyword union replaces the
keyword struct. A union may be initialized, but as all the fields are
located at the same address, it is seen as a single variable for the initial-
ization, which will be done using the first field of the union.
union {
char a;
int b;
long c;
} u = 1;
field a is initialized with the value 1 on a char. It is then more conven-
ient to define the largest field first to be sure to initialize all the union
byte.
A tag name may be specified on a union. Tag names are in the same
name space, so a union tag name cannot be the same as a structure tag
name.
Enumerations
An enumeration is declared with a syntax close to the structure/union
declaration. The list of fields is replaced by a list of identifiers. The key-
word enum replaces the keyword struct or union. A tag name may also
be specified, sharing the same name space than the structure and union
tag names. Each identifier will be assigned a constant value by the com-
piler. An enumeration variable will be allocated as a char, a short or a
long depending on the range of all the idenfiers. This means that the
compiler always needs to know all the enum members before it allo-
cates an enum variable. If the -pne option has been set, an enum varia-
ble is always allocated as an int and then there is no need to know the
enum members before to allocate the variable.
Note that the COSMIC compiler allows long enumerations as an exten-
sion to the ANSI standard.
Functions
© Copyright 2003 by COSMIC Software
Declarations 3-11
enum {blue, white, red} flag;
The names blue, white and red are three new constants. Values are
assigned to the names starting at zero, and incrementing by one for each
new name. So
blue is zero, white is one and red is two. The variable
flag will be declared as a plain char as a byte is large enough to hold
values from zero to two.
It is possible to define directly the value of a name by adding the char-
acter
= followed by a value to the name.
enum {blue, white = 10, red} flag;
blue
is still zero, white is now ten, and red is eleven, as the internal
counter is incremented from the previous value. These names become
new constants and may be used in any expression, even if they are not
assigned to an enumeration variable. In the same way, an enumeration
variable may be assigned with any kind of integer expression. An enu-
meration may be initialized as an integer variable.
Functions
A function is declared with three parameters. The first one indicates
that the object is a function, the second gives the argument list, and the
third is the type of the returned value. In order to match the global syn-
tax, the field is used to declare the type of the returned value.
The fact that the object is a function will be indicated by adding the
argument list written between parentheses
() after the name. The
field is used to declare the type of the returned value. If the
function has nothing to return, the field is replaced by the key-
word
void meaning that nothing is returned from the function. This
syntax will also allow the compiler to detect any invalid usage of the
function, if it is used in an expression or an assignment.
The argument list may be specified in two different ways. The first one
which is the oldest is known as the Kernigan and Ritchie (K&R) syn-
tax. The second has been introduced by the standardization and is
known as the prototyped syntax.
Functions
3-12 Declarations © Copyright 2003 by COSMIC Software
3
The K&R syntax specifies the argument list as a list of identifiers sepa-
rated by commas between the parentheses. This list is immediately fol-
lowed by the full declaration of the identifiers specified in the list. An
undefined identifier will be defaulted to an int argument.
int max(a, b)
int a;
int b;
The prototyped syntax specifies the argument
Continue reading on your phone by scaning this QR Code
Tip: The current page has been bookmarked automatically. If you wish to continue reading later, just open the
Dertz Homepage, and click on the 'continue reading' link at the bottom of the page.