100% found this document useful (1 vote)
45 views

Unit 5 C Language

Uploaded by

SRC Exam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
45 views

Unit 5 C Language

Uploaded by

SRC Exam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Unit – 5

Preprocessor and Bitwise Operators

Preprocessors are programs that process the source code before compilation. Several
steps are involved between writing a program and executing a program in C. Let us
have a look at these steps before we actually start learning about Preprocessors.

You can see the intermediate steps in the above diagram. The source code written by
programmers is first stored in a file, let the name be “program.c“. This file is then
processed by preprocessors and an expanded source code file is generated named
“program.i”. This expanded file is compiled by the compiler and an object code file
is generated named “program.obj”. Finally, the linker links this object code file to
the object code of the library functions to generate the executable file
“program.exe”.

Preprocessor Directives in C
Preprocessor programs provide preprocessor directives that tell the compiler to
preprocess the source code before compiling. All of these preprocessor directives
begin with a ‘#’ (hash) symbol. The ‘#’ symbol indicates that whatever statement
starts with a ‘#’ will go to the preprocessor program to get executed. We can place
these preprocessor directives anywhere in our program.
The following table lists all the preprocessor directives in C:
Preprocessor Directives Description

#define Used to define a macro

#undef Used to undefine a macro

Used to include a file in the source code


#include
program

Used to include a section of code if a


#ifdef
certain macro is defined by #define

Used to include a section of code if a


#ifndef
certain macro is not defined by #define

#if Check for the specified condition

#else Alternate code that executes when #if fails

Used to mark the end of #if, #ifdef, and


#endif
#ifndef

These preprocessors can be classified based on the type of function they perform.

Types of C Preprocessors
There are 4 Main Types of Preprocessor Directives:
1. Macros
2. File Inclusion
3. Conditional Compilation
4. Other directives

1. Macros
In C, Macros are pieces of code in a program that is given some name. Whenever
this name is encountered by the compiler, the compiler replaces the name with the
actual piece of code. The ‘#define’ directive is used to define a macro.
A macro is a segment of code which is replaced by the value of macro. Macro is defined by
#define directive. There are two types of macros:

1. Object-like Macros
2. Function-like Macros

Object-like Macros
The object-like macro is an identifier that is replaced by value. It is widely used to represent
numeric constants.

Syntax

#define token value


Example

#define PI 3.14

#define LIMIT 5

where after preprocessing, the token will be expanded to its value in the program.

Example 1

#include <stdio.h>

// macro definition
#define PI 3.14

void main()
{
float r=2.5, area;
clrscr();
area=PI*r*r;
printf(“\nThe area of circle is :%d”,area);
getch();
}

Example 2
#include <stdio.h>

// macro definition
#define LIMIT 5
void main()
{
for (int i = 1; i < = LIMIT; i++)
{
printf("%d \n", i);
}

getch();
}

Note There is no semi-colon (;) at the end of the macro definition. Macro definitions do not
need a semi-colon to end.

Function-like Macros
The function-like macro looks like function call.

We can also pass arguments to macros. Macros defined with arguments work
similarly to functions.

Example

#define fun1(a, b) a + b
#define fun2(r) r * r

#include <stdio.h>

// macro with parameter


#define AREA(l, b) (l * b)

void main()
{
int length = 10, breath = 5, area;

area = AREA(length, breath);

printf("Area of rectangle is: %d", area);

getch()
}
C Predefined Macros
ANSI C defines many predefined macros that can be used in c program.

No. Macro Description

1 _DATE_ represents current date in "MMM DD YYYY" format.

2 _TIME_ represents current time in "HH:MM:SS" format.

3 _FILE_ represents current file name.

4 _LINE_ represents current line number.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("File :%s\n", _ _FILE_ _ );
printf("Date :%s\n", _ _DATE_ _ );
printf("Time :%s\n", _ _TIME_ _ );
printf("Line :%d\n", _ _LINE_ _ );
getch();

Output:

File :simple.c
Date :May 27 2024
Time :12:28:46
Line :6

2. File Inclusion
This type of preprocessor directive tells the compiler to include a file in the source
code program. The #include preprocessor directive is used to include the header
files in the C program.
There are two types of files that can be included by the user in the program:
Standard Header Files
The standard header files contain definitions of pre-defined functions like printf(),
scanf(), etc. These files must be included to work with these functions. Different
functions are declared in different header files.

Syntax
#include <file_name>

where file_name is the name of the header file to be included. The ‘<‘ and ‘>’
brackets tell the compiler to look for the file in the standard directory.
User-defined Header Files
When a program becomes very large, it is a good practice to divide it into smaller
files and include them whenever needed. These types of files are user-defined header
files.
Syntax
#include "filename"
The double quotes ( ” ” ) tell the compiler to search for the header file in the source
file’s directory.

3. Conditional Compilation
Conditional Compilation in C directives is a type of directive that helps to
compile a specific portion of the program or to skip the compilation of some
specific part of the program based on some conditions. There are the
following preprocessor directives that are used to insert conditional code:
1. #if Directive
2. #ifdef Directive
3. #ifndef Directive
4. #else Directive
5. #elif Directive
6. #endif Directive

#endif directive is used to close off the #if, #ifdef, and #ifndef opening
directives which means the preprocessing of these directives is completed.

Syntax
#ifdef macro_name
// Code to be executed if macro_name is defined
#ifndef macro_name
// Code to be executed if macro_name is not defined
#if constant_expr
// Code to be executed if constant_expression is true
#elif another_constant_expr
// Code to be excuted if another_constant_expression is true
#else
// Code to be excuted if none of the above conditions are true
#endif

#include <stdio.h>

// defining PI
#define PI 3.14159

void main()
{
clrscr();
#ifdef PI
printf("PI is defined\n");

#elif defined(SQUARE)
printf("Square is defined\n");
#else
#error "Neither PI nor SQUARE is defined"
#endif

#ifndef SQUARE
printf("Square is not defined");
#else
Printf("Square is defined");
#endif

getch();
}

Output
PI is defined
Square is not defined

4. Other Directives
Apart from the above directives, there are two more directives that are not
commonly used. These are:
1. #undef Directive
2. #pragma Directive
1. #undef Directive
The #undef directive is used to undefine an existing macro. This directive
works as:
#undef LIMIT
Using this statement will undefine the existing macro LIMIT. After this
statement, every “#ifdef LIMIT” statement will evaluate as false.
Example
The below example demonstrates the working of #undef Directive.

#include <stdio.h>

// defining MIN_VALUE

#define MIN_VALUE 10

void main()
{

// Undefining and redefining MIN_VALUE


printf("Min value is: %d\n",MIN_VALUE);

//undefining max value


#undef MIN_VALUE

// again redefining MIN_VALUE


#define MIN_VALUE 20

printf("Min value after undef and again redefining it: %d\n",


MIN_VALUE);

getch();
}

Output
Min value is: 10
Min value after undef and again redefining it: 20

2. #pragma Directive
This directive is a special purpose directive and is used to turn on or off
some features. These types of directives are compiler-specific, i.e., they vary
from compiler to compiler.

Syntax
#pragma directive

Some of the #pragma directives are discussed below:


1. #pragma startup: These directives help us to specify the functions that
are needed to run before program startup (before the control passes to
main()).
2. #pragma exit: These directives help us to specify the functions that are
needed to run just before the program exit (just before the control returns
from main()).
Bitwise Operator in C
The bitwise operators are the operators used to perform the operations on the data at
the bit-level. When we perform the bitwise operations, then it is also known as bit-level
programming. It consists of two digits, either 0 or 1. It is mainly used in numerical
computations to make the calculations faster.

We have different types of bitwise operators in the C programming language. The


following is the list of the bitwise operators:

Operator Meaning of operator

& Bitwise AND operator

| Bitwise OR operator

^ Bitwise exclusive OR operator

~ One's complement operator (unary operator)

<< Left shift operator

>> Right shift operator


Let's look at the truth table of the bitwise operators.

X Y X&Y X|Y X^Y

0 0 0 0 0

0 1 0 1 1

1 0 0 1 1

1 1 1 1 1

Bitwise AND operator


Bitwise AND operator is denoted by the single ampersand sign (&). Two integer
operands are written on both sides of the (&) operator. If the corresponding bits of both
the operands are 1, then the output of the bitwise AND operation is 1; otherwise, the
output would be 0.

For example,

We have two variables a and b.


a =6;
b=4;
The binary representation of the above two variables are given below:
a = 0110
b = 0100
When we apply the bitwise AND operation in the above two variables, i.e., a&b, the outp
ut would be:
Result = 0100
Example:
#include <stdio.h>
void main()
{
int a=6, b=14; // variable declarations
printf("The output of the Bitwise AND operator a&b is %d",a&b);
}

Bitwise OR operator
The bitwise OR operator is represented by a single vertical sign (|). Two integer operands
are written on both sides of the (|) symbol. If the bit value of any of the operand is 1,
then the output would be 1, otherwise 0.

For example,

We consider two variables,


a = 23;
b = 10;
The binary representation of the above two variables would be:
a = 0001 0111
b = 0000 1010
When we apply the bitwise OR operator in the above two variables, i.e., a|b , then the output wo
uld be:
Result = 0001 1111

Example
#include <stdio.h>
void main()
{
int a=23,b=10; // variable declarations
printf("The output of the Bitwise OR operator a|b is %d",a|b);
}
Bitwise exclusive OR operator
Bitwise exclusive OR operator is denoted by (^) symbol. Two operands are written on
both sides of the exclusive OR operator. If the corresponding bit of any of the operand
is 1 then the output would be 1, otherwise 0.

For example,

We consider two variables a and b,


a = 12;
b = 10;
The binary representation of the above two variables would be:
a = 0000 1100
b = 0000 1010
When we apply the bitwise exclusive OR operator in the above two variables (a^b), then
the result would be:
Result = 0000 1110

#include <stdio.h>
void main()
{
int a=12,b=10; // variable declarations
printf("The output of the Bitwise exclusive OR operator a^b is %d",a^b);
}

Bitwise complement operator


The bitwise complement operator is also known as one's complement operator. It is
represented by the symbol tilde (~). It takes only one operand or variable and performs
complement operation on an operand. When we apply the complement operation on
any bits, then 0 becomes 1 and 1 becomes 0.

For example,

If we have a variable named 'a',


a = 8;
The binary representation of the above variable is given below:
a = 1000
When we apply the bitwise complement operator to the operand, then the output woul
d be:
Result = 0111

#include <stdio.h>
void main()
{
int a=8; // variable declarations
printf("The output of the Bitwise complement operator ~a is %d",~a);
}

Bitwise shift operators


Two types of bitwise shift operators exist in C programming. The bitwise shift operators
will shift the bits either on the left-side or right-side. Therefore, we can say that the
bitwise shift operator is divided into two categories:

o Left-shift operator
o Right-shift operator

Left-shift operator

It is an operator that shifts the number of bits to the left-side.

Syntax of the left-shift operator is given below:

Operand << n

Where,

Operand is an integer expression on which we apply the left-shift operation.

n is the number of bits to be shifted.

Suppose we have a statement:


int a = 5;
The binary representation of 'a' is given below:
a = 0101
If we want to left-shift the above representation by 2, then the statement would be:
a << 2;
0101<<2 = 00010100

#include <stdio.h>
void main()
{
int a=5; // variable initialization
printf("The value of a<<2 is : %d ", a<<2);
}

Right-shift operator

It is an operator that shifts the number of bits to the right side.

Syntax of the right-shift operator is given below:

Operand >> n;

Suppose we have a statement,


int a = 7;
The binary representation of the above variable would be:
a = 0111
If we want to right-shift the above representation by 2, then the statement would be:
a>>2;
0000 0111 >> 2 = 0000 0001

#include <stdio.h>
void main()
{
int a=7; // variable initialization
printf("The value of a>>2 is : %d ", a>>2);
}

You might also like