struct Person {
unsigned char name[20];
unsigned char surname[20];
unsigned int age;
unsigned int height;
unsigned weight;
}
struct Person P1, P2;
........................
........................
P2 = P1;
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;
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
/* Pre-increment operator */
j = 4;
k = ++j; // k = 5, j = 5
/* Post-decrement operator */
j = 12;
k = j--; // k = 12, j = 11
/* Pre-decrement operator */
j = 12;
k = --j; // k = 11, j = 11
Relational Operators
Relational operators are used in comparisons. If the expression evaluates to TRUE, a 1 is returned; otherwise a 0 is returned.
All relational operators associate from left to right. A list of mikroC relational operators is given in Table 3.5.
Table 3.5: mikroC relational operators
Operator |
Operation |
== |
Equal to |
!= |
Not equal to |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal to |
<= |
Less than or equal to |
The following example illustrates the use of relational operators:
x = 10
x > 8 // returns 1
x == 10 // returns 1
x < 100 // returns 1
x > 20 // returns 0
x != 10 // returns 0
x >= 10 // returns 1
x <= 10 // returns 1
Logical Operators
Logical operators are used in logical and arithmetic comparisons, and they return TRUE (i.e., logical 1) if the expression evaluates to nonzero, and FALSE (i.e., logical 0) if the expression evaluates to zero. If more than one logical operator is used in a statement, and if the first condition evaluates to FALSE, the second expression is not evaluated. The mikroC logical operators are listed in Table 3.6.
Table 3.6: mikroC logical operators
Operator |
Operation |
&& |
AND |
|| |
OR |
! |
NOT |
The following example illustrates the use of logical operators:
/* Logical AND */
x = 7;
x > 0 && x < 10 // returns 1
x > 0 || x < 10 // returns 1
x >= 0 && x <= 10 // returns 1
x >= 0 && x < 5 // returns 0
a = 10; b = 20; c = 30; d = 40;
a > b && c > d // returns 0
b > a && d > c // returns 1
a > b || d > c // returns 1
Bitwise Operators
Bitwise operators are used to modify the bits of a variable. The mikroC bitwise operators are listed in Table 3.7.
Table 3.7: mikroC bitwise operators
Operator |
Operation |
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise EXOR |
$ |
Bitwise complement |
<< |
Shift left |
>> |
Shift right |
Bitwise AND returns 1 if both bits are 1, otherwise it returns 0.
Bitwise OR returns 0 if both bits are 0, otherwise it returns 1.
Bitwise XOR returns 1 if both bits are complementary, otherwise it returns 0.
Bitwise complement inverts each bit.
Bitwise shift left and shift right move the bits to the left or right respectively.
The following example illustrates the use of bitwise operators:
i. 0xFA 0xEE returns 0xEA
0xFA: 1111 1010
0xEE: 1110 1110
- - - - - - - -
0xEA: 1110 1010
ii. 0x01 | 0xFE returns 0xFF
0x01: 0000 0001
0xFE: 1111 1110
- - - - - - - -
0xFE: 1111 1111
iii. 0xAA ^ 0x1F returns
0xAA: 1010 1010
0x1F: 0001 1111
Читать дальше