Выбрать главу

      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:\temp\last.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.

3.1.19 Modifying the Flow of Control

Statements are normally executed sequentially from the beginning to the end of a program. We can use control statements to modify this normal sequential flow in a C program. The following control statements are available in mikroC programs:

• Selection statements

• Unconditional modifications of flow

• Iteration statements

Selection Statements

There are two selection statements: if and switch.

if Statement The general format of the if statement is:

if (expression)

 Statement1;

else

 Statement2;

or

if (expression) Statement1; else Statement2;