M = You.age;
Structure members can be initialized during the declaration of the structure. In the following example, the radius and height of structure Cylinder are initialized to 1.2 and 2.5 respectively:
struct Cylinder {
float radius;
float height;
} MyCylinder = {1.2, 2.5};
Values can also be set to members of a structure using pointers by defining the variable types as pointers. For example, if TheCylinder is defined as a pointer to structure Cylinder, then we can write:
struct Cylinder {
float radius;
float height;
} *TheCylinder;
TheCylinder->radius = 1.2;
TheCylinder->height = 2.5;
The size of a structure is the number of bytes contained within the structure. We can use the sizeof operator to get the size of a structure. Considering the above example,
sizeof(MyCylinder)
returns 8, since each float variable occupies 4 bytes in memory.
Bit fields can be defined using structures. With bit fields we can assign identifiers to bits of a variable. For example, to identify bits 0, 1, 2, and 3 of a variable as LowNibble and to identify the remaining 4 bits as HighNibble we can write:
struct {
LowNibble : 4;
HighNibble : 4;
} MyVariable;
We can then access the nibbles of variable MyVariable as:
MyVariable.LowNibble = 12;
MyVariable.HighNibble = 8;
In C language we can use the typedef statements to create new types of variables. For example, a new structure data type named Reg can be created as follows:
typedef struct {
unsigned char name[20];
unsigned char surname[20];
unsigned age;
} Reg;
Variables of type Reg can then be created in the same way other types of variables are created. In the following example, variables MyReg, Reg1, and Reg2 are created from data type Reg:
Reg MyReg, Reg1, Reg2;
The contents of one structure can be copied to another structure, provided that both structures are derived from the same template. In the following example, structure variables of the same type, P1 and P2, are created, and P2 is copied to P1:
struct Person {
unsigned char name[20];
unsigned char surname[20];
unsigned int age;
unsigned int height;
unsigned weight;
}
struct Person P1, P2;
........................
........................
P2 = P1;
3.1.17 Unions
Unions are used to overlay variables. A union is similar to a structure and is even defined in a similar manner. Both are based on templates, and the members of both are accessed using the “.” or “->” operators. A union differs from a structure in that all variables in a union occupy the same memory area, that is, they share the same storage. An example of a union declaration is:
union flags {
unsigned char x;
unsigned int y;
} P;
In this example, variables x and y occupy the same memory area, and the size of this union is 2 bytes long, which is the size of the biggest member of the union. When variable y is loaded with a 2-byte value, variable x will have the same value as the low byte of y. In the following example, y is loaded with 16-bit hexadecimal value 0xAEFA, and x is loaded with 0xFA:
P.y = 0xAEFA;
The size of a union is the size (number of bytes) of its largest member. Thus, the statement:
sizeof(P)
returns 2.
This union can also be declared as:
union flags {
unsigned char x;
unsigned int y;
}
union flags P;
3.1.18 Operators in C
Operators are applied to variables and other objects in expressions to cause certain conditions or computations to occur.
mikroC language supports the following operators:
• Arithmetic operators
• Relational operators
• Logical operators
• Bitwise operators
• Assignment operators
• Conditional operators
• Preprocessor operators
• Arithmetic Operators
Arithmetic operators are used in arithmetic computations. Arithmetic operators associate from left to right, and they return numerical results. The mikroC arithmetic operators are listed in Table 3.4.
Table 3.4: mikroC arithmetic operators
Operator | Operation |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Remainder (integer division) |
++ | Auto increment |
- | Auto decrement |
The following example illustrates the use of arithmetic operators:
/* Adding two integers */
5 + 12 // equals 17
/* Subtracting two integers */
120 – 5 // equals 115
10 – 15 // equals -5
/* Dividing two integers */
5 / 3 // equals 1
12 / 3 // equals 4
/* Multiplying two integers */
3 * 12 // equals 36
/* Adding two floating point numbers */
3.1 + 2.4 // equals 5.5
/* Multiplying two floating point numbers */
2.5 * 5.0 // equals 12.5
/* Dividing two floating point numbers */
25.0 / 4.0 // equals 6.25
/* Remainder (not for float) */
7 % 3 // equals 1
/* Post-increment operator */
j = 4;
k = j++; // k = 4, j = 5