Êíèãà: Advanced PIC Microcontroller Projects in C

3.1.18 Operators in C

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
/* 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
      - - - - - - - -

      0xB5: 1011 0101

  iv. ~0xAA returns 0x55
      0xAA: 1010 1010
      ~ :   0101 0101
      - - - - - - - -

      0x55: 0101 0101

   v. 0x14 >> 1 returns 0x0A (shift 0x14 right by 1 digit)
      0x14: 0001 0100
      >> 1: 0000 1010
      - - - - - - - -

      0x0A: 0000 1010

  vi. 0x14 >> 2 returns 0x05 (shift 0x14 right by 2 digits)
      0x14: 0001 0100
      >> 2: 0000 0101
      - - - - - - - -

      0x05: 0000 0101

 vii. 0x235A << 1 returns 0x46B4 (shift left 0x235A left by 1 digit)
      0x235A: 0010 0011 0101 1010
      << 1 :  0100 0110 1011 0100
      - - - - - - - - - - - - - -
      0x46B4: 0100 0110 1011 0100
viii. 0x1A << 3 returns 0xD0 (shift left 0x1A by 3 digits)
      0x1A: 0001 1010
      << 3: 1101 0000
      - - - - - - - -

      0xD0: 1101 0000

Assignment Operators

In C language there are two types of assignments: simple and compound. In simple assignments an expression is simply assigned to another expression, or an operation is performed using an expression and the result is assigned to another expression:

Expression1 = Expression2

or

Result = Expression1 operation Expression2

Examples of simple assignments are:

Temp = 10;
Cnt = Cnt + Temp;

Compound assignments have the general format:

Result operation = Expression1

Here the specified operation is performed on Expression1 and the result is stored in Result. For example:

j += k;

is same as:

j = j + k;

also

p *= m;

is same as

p = p * m;

The following compound operators can be used in mikroC programs:

+= -= *= /=  %=
&= |= ^= >>= <<=

Conditional Operators

The syntax of a conditional operator is:

Result = Expression1 ? Expression2 : Expression3

Expression1 is evaluated first, and if its value is true, Expression2 is assigned to Result, otherwise Expression3 is assigned to Result. In the following example, the maximum of x and y is found where x is compared with y and if x y then max = x, otherwise max = y:

max = (x > y) ? x : y;

In the following example, lowercase characters are converted to uppercase. If the character is lowercase (between a and z), then by subtracting 32 from the character we obtain the equivalent uppercase character:

c = (c >= a && c <= z) ? (c - 32) : c;

Preprocessor Operators

The preprocessor allows a programmer to:

• Compile a program conditionally, such that parts of the code are not compiled

• Replace symbols with other symbols or values

• Insert text files into a program

The preprocessor operator is the (“#”) character, and any line of code leading with a (“#”) is assumed to be a preprocessor command. The semicolon character (“;”) is not needed to terminate a preprocessor command.

mikroC compiler supports the following preprocessor commands:

#define #undef
#if     #elif  #endif
#ifdef  #ifndef
#error
#line

#define, #undef, #ifdef, #ifndef  The #define preprocessor command provides macro expansion where every occurrence of an identifier in the program is replaced with the value of that identifier. For example, to replace every occurrence of MAX with value 100 we can write:

#define MAX 100

An identifier that has already been defined cannot be defined again unless both definitions have the same value. One way to get around this problem is to remove the macro definition:

#undef MAX

Alternatively, the existence of a macro definition can be checked. In the following example, if MAX has not already been defined, it is given value 100, otherwise the #define line is skipped:

#ifndef MAX
 #define MAX 100
#endif

Note that the #define preprocessor command does not occupy any space in memory. We can pass parameters to a macro definition by specifying the parameters in a parenthesis after the macro name. For example, consider the macro definition:

#define ADD(a, b) (a + b)

When this macro is used in a program, ADD(a, b) will be replaced with (a + b) as shown:

p = ADD(x, y)

will be transformed into

p = (x + y)

Similarly, we can define a macro to calculate the square of two numbers:

#define SQUARE(a) (a * a)

We can now use this macro in a program:

p = SQUARE(x)

will be transformed into

p = (x * x)

#include  The preprocessor directive #include is used to include a source file in our program. Usually header files with extension “.h” are used with #include. There are two formats for using #include:

#include <file>

and

#include "file"

In first option the file is searched in the mikroC installation directory first and then in user search paths. In second option the specified file is searched in the mikroC project folder, then in the mikroC installation folder, and then in user search paths. It is also possible to specify a complete directory path as:

#include "C:templast.h"

The file is then searched only in the specified directory path.

#if, #elif, #else, #endif  The preprocessor commands #if, #elif, #else, and #endif are used for conditional compilations, where parts of the source code can be compiled only if certain conditions are met. In the following example, the code section where variables A and B are cleared to zero is compiled if M has a nonzero value, otherwise the code section where A and B are both set to 1 is compiled. Notice that the #if must be terminated with #endif:

#if M
 A = 0;
 B = 0;
#else
 A = 1;
 B = 1;
#endif

We can also use the #elif condition, which tests for a new condition if the previous condition was false:

#if M
 A = 0;
 B = 0;
#elif N
 A = 1;
 B = 1;
#else
 A = 2;
 B = 2;
#endif

In the above example, if M has a nonzero value code section, A = 0; B = 0; are compiled. Otherwise, if N has a nonzero value, then code section A = 1; B = 1; is compiled. Finally, if both M and N are zero, then code section A = 2; B = 2; is compiled. Notice that only one code section is compiled between #if and #endif and that a code section can contain any number of statements.

Îãëàâëåíèå êíèãè


Ãåíåðàöèÿ: 2.252. Çàïðîñîâ Ê ÁÄ/Cache: 2 / 0
ïîäåëèòüñÿ
Ââåðõ Âíèç