0% found this document useful (0 votes)
170 views22 pages

Pointers in C

Pointer is a variable that stores the memory address of another variable. There are several types of pointers in C including pointers to primitive data types like integers and floats, pointers to user-defined types like structures, and pointers to functions. Pointers allow accessing and modifying the value of the variable being pointed to indirectly through dereferencing the pointer variable. Pointer variables must be declared with a data type and can be initialized by assigning the address of another variable. Pointers can form chains where a pointer variable points to another pointer variable, allowing indirect access through multiple levels of indirection. Pointer arithmetic allows incrementing and decrementing pointers to access successive elements in arrays and strings.

Uploaded by

Mano Ranjani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
170 views22 pages

Pointers in C

Pointer is a variable that stores the memory address of another variable. There are several types of pointers in C including pointers to primitive data types like integers and floats, pointers to user-defined types like structures, and pointers to functions. Pointers allow accessing and modifying the value of the variable being pointed to indirectly through dereferencing the pointer variable. Pointer variables must be declared with a data type and can be initialized by assigning the address of another variable. Pointers can form chains where a pointer variable points to another pointer variable, allowing indirect access through multiple levels of indirection. Pointer arithmetic allows incrementing and decrementing pointers to access successive elements in arrays and strings.

Uploaded by

Mano Ranjani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 22

Pointers in C

What is pointer in c programming?


Pointer is a user defined data type which creates special types of
variables which can hold the address of primitive data type like
char, int, float, double or user defined data type like function,
pointer etc. or derived data type like array, structure, union,
enum.
Examples:
int *ptr;
int (*ptr)();
int (*ptr)[2];
In c programming every variable keeps two type of value.
1. Contain of variable or value of variable.
2. Address of variable where it has stored in the memory.

Understanding Pointers in C
Pointer is a variable just like other variables of c but only
difference is unlike the other variable it stores the memory
address of any other variables of c. This variable may be type
of int, char, array, structure, function or any other pointers. For
examples:
(1) Pointer p which is storing memory address of an int type
variable:
int i=50;
This statement instructs the system to find a location for the integer variable i and puts the value
of 50 in that location
i Variable

50 Value

5001 Address

int *p=&i;
Since a pointer is a variable, its value is also stored in the memory in another location.
Variable Value Address
i 50 5001

p 5001 5048
Since the value of variable p is the address of the variable
i, we may access the value of i by using the value of p and
therefore, we say that the variable p points to the variable i.
(2) Pointer p which is storing memory address of an array:
int arr[20];
int (*p)[20]=&arr;
(3) Pointer p which is storing memory address of a function:
char display(void);
char(*p)(void)=&display;
(4) Pointer p which is storing memory address of struct type
variable:
struct abc{

1
int a;
float b;
}var;
struct abc *p=&var;
Note: A variable where it will be stored in memory is decided by
operating system. We cannot guess at which location a particular
variable will be stored in memory.

Meaning of following pointer declaration and definition:


int a=50;
int *ptr1;
int **ptr2;
ptr1=&a;
ptr2=&pt1;
Explanation:
About variable a:
1. Name of variable: a
2. Value of variable which it keeps: 50
3. Address where it has stored in memory: 5000 (assume)
About variable ptr1:
4. Name of variable: ptr1
5. Value of variable which it keeps: 5000
6. Address where it has stored in memory: 9000 (assume)
About variable ptr2:
7. Name of variable: ptr2
8. Value of variable which it keeps: 9000
9. Address where it has stored in memory: 9555 (assume)
Pictorial representation of above pointer declaration and
definition:

Note:
* is known as indirection operator which gives content of any
variable
& is known as reference operator which gives address where variable
has stored in memory.

Accessing the Address of a Variable:


The actual location of a variable in the memory is system dependent
and the address of the variable is not known. We can detect the
address of the variable with the help of the operator &. The
operator & immediately preceding a variable returns the address of
the variable associated with it. The & operator can be used only
with a simple variable or an array element.

Example
p = &qty;
It gives the address of the variable qty. so the output will be
5000.

2
If x is an array then,
&x[0] and &x[i+3] are valid.

Example program:
main()
{
Char a=’A’;
int x=125;
float p=10.25, q=18.76;
printf(“%c is stored at addr %u \n”,a, &a);
printf(“%d is stored at addr %u \n”,x, &x);
printf(“%f is stored at addr %u \n”,p, &p);
printf(“%f is stored at addr %u \n”,q, &q);
}
Output
A is stored at addr 4436.
125 is stored at addr 4434.
10.250000 is stored at addr 4442.
18.760000 is stored at addr 4438.

Note: %u format for printing the address values.

Declaring and Initializing Pointers


Declaration:
Every variable must be declared for its type. Declaring
pointers are different from other declaring variable. Since pointer
variables contain addresses that belong to a separate data type,
they must be declared as pointers. The declaration of a pointer
variable takes the following form:

data type *pt_name


This tells the compiler three things about the variable pt_name.
1. The asterisk (8) tells that the variable pt_name is a pointer
variable.
2. pt_name needs a memory location.
3. pt_name points to a variable of type data type.
Examples
int *p; /*integer pointer */
float *x; /*float pointer*/
The declarations cause the compiler to allocate memory
locations for the pointer variables p and x. since the memory
locations have not been assigned any values, these locations may
contain some unknown values in them and therefore they point to
unknown locations.

Initialization:
 The process of assigning the address of a variable to a pointer
variable is known as initialization.
 Once the pointer variable is declared we can use the assignment
operator to initialize the variable.

3
Example:
int quantity;
int *p; /*declaration*/
p=&quantity; /*initialization*/

 We can also combine the initialization with declaration.


int *p=&quantity;

 We must ensure that the pointer variables always point to the


corresponding type of data.
Example:
float a, b;
int x, *p;
p= &a; /*wrong*/
b= *p;
will result in erroneous output because we are trying to assign the
address of a float variable to an integer pointer.

 When we declare a pointer to be of int type, the system assumes


that any address that the pointer will hold will point to an
integer variable.
 It is possible to combine the declaration of data variable, the
declaration of pointer variable and the initialization of the
pointer variable in one step.
Example:
int x, *p= &x; /*three in one*/
is valid.

Accessing a Variable through its Pointer:


The pointer variable can be accessed through the asterisk (*)
operator. It is usually known as indirection operator. Another name
for indirection operator is the dereferencing operator.
Example:
int qty, *p, n;
qty = 90;
p = &qty; /*p has memory location of qty*/
n = *p; /*n has the value of qty */

The * can be remembered as “value at address”.


p = &qty;
n = *p;
are equivalent to
n=*&qty;
which in turn is equivalent to
n=qty;

4
Chain of pointers:
It is possible to make a pointer to point to another pointer, thus
creating a chain of pointers s shown:
p2 p1 variable

address 2 address 1 value

Here, the pointer variable p2 contains the address of the pointer


variable p1, which points to the location that contains the desired
value. This is known as multiple indirections.
A variable that is a pointer to a pointer must be declared using
additional indirection operator symbols in front of the name.
Example
int **p2;
This declaration tells the compiler that p2 is a pointer to a
pointer of int type. The pointer p2 is not a pointer to an integer,
but a pointer to an integer pointer.

We can access the target value indirectly pointed to by pointer to


a pointer by applying the indirection operator twice.
/* Program for Chain of Pointers */
#include<stdio.h>
void main( )
{
int x, *p1, **p2;
x = 100;
p1 = &x ; // address of x
p2 = &p1; // address of p1
printf(“%d”, **p2);
}
This code will display the value 100. here p1 is declared as a
pointer to an integer and p2 as a pointer to a pointer to an
integer.

Pointer Expressions
 Like other variables, pointer variables can be used in
expressions.
Examples
y = *p * *p1; same as (*p) * (*p1)
sum=sum+ *p1;
*p2 = *p2 + 10;
 We may also use short-hand operators with the pointers.
Examples
p1++;
Sum += p2;
 Pointers can also be used to compared using relational operators.
Examples
p1 > p2;
p1 = = p2;

5
We may not use pointers in multiplication or division. . Similarly,
two pointers cannot be added.
Examples
p1 / p2;
p1 + p2;
p1 * p2;
are not allowed.

Pointer Increment and Scale Factor:


Pointer variable can be incremented by + operator.
Examples
p1++; /*is same as p1=p1+1*/
p1= p2+1;
 It will cause p1 to point to the next value of its type.
 If p1 is an integer pointer and its location 2800, then the next
value p1+1 will be stored at 2802 not 2801.
 That is, when we increment a pointer , its value is increased by
the ‘length’ of its data type.
 For an IBM PC , the ‘length’ of its data types as follows :
characters - 1 byte
integer - 2 bytes
float - 4 bytes
long integer - 4 bytes
double - 8 bytes
 The number of bytes used to store various data types depends on
the system and can be found by sizeof operator.

Pointers and Arrays:


When an array is declared, the compiler allocates a base
address and sufficient amount of storage to contain all the
elements of the array in contiguous memory locations. The base
address is the location of the first element (index 0) of the
array. The compiler also defines the array name as a constant
pointer to the first element. Suppose we declare an array x as
follows:
static int x[5]={1,2,3,4,5};
Suppose the base address of x is 1000 and assuming that each
integer requires two bytes, the five elements will be stored as
follows:
Elements x[0] x[1] x[2] x[3] x[4]

Value 1 2 3 4 5

Address 1000 1001 1002


1003 1004

Base Address
 The name x is defined as a constant pointer to the first elememt,
x[0] and the value of x is 1000, the location where x[0] is
stored.
x=&x[0]=1000

6
 If we declare p as integer pointer, then we can make the pointer
p to the array x by the following assignment:
p=x;
This is equivalent to
p=&x[0];
 Now we can access every value of x using p++ to move from one
element to another. The relationship between p and x is shown as:
p=&x[0] (=1000)
p+1=&x[0] (=1002)
p+2=&x[0] (=1004)
p+3=&x[0] (=1006)
p+4=&x[0] (=1008)
 The address of an element is calculated using its index and the
scale factor of the data type, for instance
Address of x[3]= base address + (3 * scale factor of int)
= 1000 +(3 *2) = 1006
 The pointer accessing method is much faster than array indexing.

Pointers and Character Strings:


A string is an array of characters, terminated with a null
character. Like in one-dimensional arrays, we can use pointer to
access the individual characters in a string.
C supports alternate method to create strings using pointers
variables of type char.
EX:
char *str= “good”;
This creates a string for the literal and then stores its address
in the pointer variable str.
We can also use the run-time assignment for giving values to a
string pointer.
Ex:
char *string1;
string1= “good”; /* not a string copy, bec the variable string1
is a pointer not a string*/
Example:
#include<stdio.h>
char *cptr = name;
void main()
{
char *name;
int length;
char *cptr = name;
name = “DELHI”;
while (*cptr!=’\0’)
{
printf(“%c is stored at address %u\n”, *cptr, cptr)
cptr++;
}
length = cptr – name;
printf(“\n Length of the String = %d\n”, length);
}
Output

7
D – is stored at address 54
E – is stored at address 55
L – is stored at address 56
H – is stored at address 57
I – is stored at address 58

Array of Pointers:
One important use of pointers is in handling of a table of strings.
Consider the following array of strings:
char name[3][25];
This says that the name is a table of strings containing three
names, each with a maximum length of 25 characters. The total
storage requirements for the name are 75 bytes.
Therefore instead of making each row fixed number of characters, we
can make it a pointer to a string of varying length. For ex.
char *name [3] = {
“New Zealand”,
“Canada”,
“India”
};
Declares name to be an array of three pointers to characters, each
pointer pointing to a particular name as:
name[0]  New Zealand
name[1]  Canada
name[3]  India
This declaration allocates only 28 bytes, sufficient to hold the
characters as shown below:
for(i=0;i<=2;i++)
printf(“%s\n”,name[i]); /Statement would print all the three names
The character arrays with the varying length are called “ragged
arrays”.

Pointers as Function Arguments:


We can pass the array element to the function. Similarly we can
also pass the address of the variable as an argument to a function.
When we pass the address to a function, the parameters receiving
the addresses should be pointers. The process of calling a function
using pointers to pass the addresses of variable is known as call
by reference. The function that is called by “reference” can change
the value of the variable used in the call.
Example
#include<stdio.h>
main()
{
int x=20;
change(&x);
print(“%d\n”,x);
}
change(p)
int *p;
{
*p = *p +10;

8
}
Some important points are as follows:
1. The function parameters are declared as pointers.
2. The dereferenced pointers are used in the function body.
3. When the function is called, the addresses are passed as
actual arguments
Example: Function using pointers to exchange the values stored in
two locations in the memory.
Program:
#include<stdio.h>
void exchange(int *, int *)
main()
{
int x=100, y=200;
printf(“Before exchange: x=%d y=%d”, x, y);
exchange(&x, &y); /*function call*/
printf(“After exchange: x=%d y=%d”, x, y);
}
exchange(int *a, int *b)
{
int t;
t=*a ; /* assign the value at address a to t */
*a=*b; /* put b into a */
*b=t; /* put t into b */
}
Output:
Before exchange: x=100 y=200
After exchange: x=200 y=100

Functions Returning Pointers:


Since pointers are a data type in C, we can also force a function
to return a pointer to the calling function.
Example
#include<stdio.h>
int *larger(int *,int *) // Function Prototype
void main( )
{
int a = 10, b = 20 ;
int *p;
p = larger(&a, &b) ; // Function Call
printf(“%d”,*p);
}
int *larger(int *x, int*y)
{
if(*x > *y)
return(x) ;
else
return(y);
}
The function larger( ) receives the addresses of the variables a
and b, decides which one is larger using the pointers x and y and
then returns the address of its location. The returned value is

9
then assigned to the pointer variable p in the calling function. In
this case, the address of the b is returned and assigned to p and
therefore the output will be the value of b, namely 20.

Pointers to Functions:
 A function, like a variable, has an address location in the
memory.
 It is also possible to declare a pointer to a function, which can
then be used as an argument in another function.
 A pointer to a function is declared as follows:
type(*fptr)();
 This tells the compiler that fptr is a pointer to a function
which returns type value. The parantheses around *fptr are
necessary. A statement like
type *gptr();
would declare gptr as a function returning a pointer to type.
 We can make a function pointer to point to a specific function by
simply assigning the name of the function to the pointer.
Example
double mul(int , int) ;
double (*p1) ( ) ;
p1 = mul;
declare p1 as a pointer to a function and then make p1 to point to
the function mul. To call the function mul, we may now use the
pointer p1 with the list of parameters as shown below:
(*p1)(x,y) /* Function Call */
is equivalent to
mul(x,y) /* Both statements are same */

Pointers and Structures:


 The pointers and structures can be used as follows:
Example
Struct inventory
{
char name[25];
int number;
float price;
}
product[2],*ptr;

ptr is pointer variable. product is an array variable.


ptr =product;
 Now the structure variables can be accessed by using pointer
variable as follows:
ptr  name;
ptr  number;
ptr  price;

 The symbol  is called the arrow operator (also known as member


selection operator) and is made up of a minus sign and a greater
than sign.

10
 The following for statement will print the values of members of
all the elements of product array.
for(ptr = product ; ptr < product ; ptr++)
printf(“%s %d %f \n”, ptr  name, ptrnumber, ptrprice);
 We could also use the notation
(*ptr).number
to access the member number. The parenthesis around *ptr are
necessary because the member operator ‘.’ has a higher precedence
than the operator.

Example1:
#include <stdio.h>
#include <conio.h>
int main ()
{
struct st
{
int id;
char *name;
char *address;
};
struct st employee, *stptr;
stptr = &employee;
stptr->id = 1;
stptr->name = "Angelina";
stptr->address ="Rohini,Delhi";
printf("Employee Information:id=%d\n%s\n%s\n",stptr->id, stptr->
name,stptr->address);
getch();
return 0;
}
OUTPUT:
Employee Information:
id=1
name = "Angelina
address ="Rohini,Delhi

Example2:
#include <string.h>
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
};
void printBook( struct Books *book ); /* function declaration */
int main( )
{
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */

11
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* print Book1 info by passing address of Book1 */
printBook( &Book1 );
/* print Book2 info by passing address of Book2 */
printBook( &Book2 );
return 0;
}
void printBook( struct Books *book )
{
printf( "Book title : %s\n", book->title);
printf( "Book author : %s\n", book->author);
printf( "Book subject : %s\n", book->subject);
printf( "Book book_id : %d\n", book->book_id);
}
Output:
Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407
Book title : Telecom Billing
Book author : Zara Ali

12
Bit Fields
This structure requires 8 bytes of memory space but in actual we are
going to store either 0 or 1 in each of the variables. The C
programming language offers a better way to utilize the memory space
in such situation. If you are using such variables inside a structure
then you can define the width of a variable which tells the C
compiler that you are going to use only those numbers of bytes. For
example, above structure can be re-written as follows:
struct
{
unsigned int widthValidated : 1;
unsigned int heightValidated : 1;
} status;
Now, the above structure will require 4 bytes of memory space for
status variable but only 2 bits will be used to store the values. If
you will use up to 32 variables each one with a width of 1
bit , then also status structure will use 4 bytes, but as soon as you
will have 33 variables, then it will allocate next slot of the
memory and it will start using 64 bytes. Let us check the following
example to understand the concept:

#include <stdio.h>
#include <string.h>
/* define simple structure */
struct
{
unsigned int widthValidated;
unsigned int heightValidated;
} status1;
/* define a structure with bit fields */
struct
{
unsigned int widthValidated : 1;
unsigned int heightValidated : 1;
} status2;
int main( )
{
printf( "Memory size occupied by status1 : %d\n", sizeof(status1));
printf( "Memory size occupied by status2 : %d\n", sizeof(status2));
return 0;
}
OUTPUT:
Memory size occupied by status1: 8
Memory size occupied by status2: 4

Bit Field Declaration


The declaration of a bit-field has the form inside a structure:
struct
{
type [member_name] : width ;
};
Below the description of variable elements of a bit field:

13
Elements Description
type An integer type that determines how the bit-field's
value is interpreted. The type may be int, signed
int, unsigned int.
member_name The name of the bit-field.
width The number of bits in the bit-field. The width must
be less than or equal to the bit width of the
specified type.
The variables defined with a predefined width are called bit fields.
A bit field can hold more than a single bit for example if you need a
variable to store a value from 0 to 7 only then you can define a bit
field with a width of 3 bits as follows:
struct
{
unsigned int age : 3;
} Age;
The above structure definition instructs C compiler that age variable
is going to use only 3 bits to store the value; if you will try to
use more than 3 bits then it will not allow you to do so. Let us try
the following example:
#include <stdio.h>
#include <string.h>
struct
{
unsigned int age : 3;
} Age;
int main( )
{
Age.age = 4;
printf( "Sizeof( Age ) : %d\n", sizeof(Age) );
printf( "Age.age : %d\n", Age.age );
Age.age = 7;
printf( "Age.age : %d\n", Age.age );
Age.age = 8;
printf( "Age.age : %d\n", Age.age );
return 0;
}
When the above code is compiled it will compile with
warning and when executed, it
produces the following result:
Sizeof( Age ) : 4
Age.age : 4
Age.age : 7
Age.age : 0

14
THE PREPROCESSOR

INTRODUCTION:
The unique feature of the C language is the preprocessor. The C
preprocessor provides several tools that are unavailable in other high-
level languages. The programmer can use these tools to make program
easy to read, easy to modify, portable, and more efficient.
The preprocessor is a program that processes the source code
before it passes through the compiler. It operates under the control of
preprocessor command lines or directives. Preprocessor directives are
places in the source program before the main line.
Before the source code passes through the compiler, it is examined
by the preprocessor for any preprocessor directives. If there are any,
appropriate actions are taken and then the source program is handed
over to the compiler.
The preprocessor follows special syntax rules that are different
from the normal C syntax. They all begin with the symbol # in column
one and do not require a semicolon at the end.
A set of commonly used preprocessor directives and their functions
is given in below table:
Directive Function
#define Defines a macro substitution
#undef Undefines a macro
#include Specifies the files to be included
#ifdef Test for a macro definition
#endif Specifies the end of #if
#ifndef Test whether a macro is not
defined
#if Test a compile-time condition
#else Specifies alternatives when #if
test fails

These directives can be divided into three categories:


1. Macro substitution directives

15
2. File inclusion directives
3. Compiler control directives

16
MACRO SUBSTITUTION
Macros are operation defined as symbols. Macro substitution is a
process where an identifier in a program is replaced by a predefined
string composed of one or more tokens. The preprocessor accomplishes
this task under the direction of #define statement. This statement,
usually known as a macro definition takes the following general form:
#define identifier string
This statement is included at the beginning of the program, and
then the preprocessor replaces every occurrence of the identifier in the
source code by the string. The definition is not terminated by semicolon.
The string may be any text, while the identifier must be valid C name.
There are different forms of macro substitution. The most common
forms are:
1. Simple macro substitution
2. Argumented macro substitution
3. Nested macro substitution

Simple macro substitution:


Simple string replacement is commonly used to define constants.
Example:
#define COUNT 100
#define FALSE 0
#define SUBJECTS 6
#define PI 3.14
#define CAPITAL “DELHI”
The macros are written in capital letters. It is a convention to write all
macros in capital letters to identify them as symbolic constants.
Example:
#define M 5
will replace all the occurrence of M with 5, starting from the line of
definition to the end of the program. Macro inside a string does not get
replaced. Consider the following two lines,
total=M*value;

17
printf(“M=%d\n”, M);
these two line would be changed during preprocessing as follows:
total=5*value;
printf(“M=%d\n”, 5); /“M=%d\n” is left unchanged
A macro definition can include more than a simple constant value. It can
include expressions as well. Following are valid definitions:
#define AREA 5*12.46
#define SIZE sizeof(int)*4
#define TWO-PI 2.0*3.14
Whenever we use expression is used care should be taken to prevent an
unexpected order of evaluation. Consider the evaluation of the equation,
ratio=D/A;
where D and A are macros defined as follows:
#define D 45-22
#define A 78+32
The result of the preprocessor’s substitution for D and A is:
ratio = 45-22/78+32
this is certainly different from the expected expression
(45-22) / (78+32)
Correct results can be obtained from by using parenthesis around the
strings as:
#define D (45-22)
#define A (78+32)

Macros with arguments:


The preprocessor permits us to define more complex and more useful
form of replacements. It takes the form:
#define identifier(f1, f2, … fn) string
There is no space between the macro identifier and the left parenthesis.
The identifiers f1, f2, … fn are the formal macro arguments.
Example:
#define CUBE(x) (x*x*x)
If the following statement appears in the program:
volume = CUBE(side);
Then the preprocessor would expand this statement to:

18
volume = (side * side * side);
Consider the following statement:
volume = CUBE (a + b);
This would expand to:
volume = (a + b * a + b * a + b);
Obviously the above statement not produce correct results, corrected
using parenthesis for each occurrence of a formal argument in the
string.

Nesting of macros:
We can also use one macro in the definition of another macro. That is
the macro definition can be nested.
Ex:
#define M 5
#define N M+1
#define SQUARE(x) ((x) *(x))
#define CUBE(x) (SQUARE(x) *(x))
#define SIXTH(x) (CUBE(x) * CUBE(x))
Macros can also be used as parameters of other macros. Ex:
#define MAX(M,N) (( (M) >(N)) ? (M) : (N)

FILE INCLUSION
An external file containing functions or macro definitions can be included
as a part of a program so that we need not rewrite those functions or
macro definitions. This is achieved by the #include preprocessor
directive. The two forms of the #include directive are:
#include “filename”
#include <filename>
where filename is the name of the file containing the required definitions
or functions. The preprocessor inserts the entire contents of filename
into source code of the program. When the filename is included within

19
the double quotation marks, the search for the file is made first in the
current directory and then in standard directories.
If the included file is not found, an error is reported and compilation is
terminated.
Ex:
#include <stdio.h>
#include “stdlib.h”

20
COMPILER CONTROL DIRECTIVES:
In C, there are several preprocessor directives that allow us to
selectively compile portions of the program’s source code. This process
is known as conditional compilation. This enables the programmers to
control the execution of preprocessor directives and compilation of
program code. Each of the preprocessor directives evaluates a constant
integer expression.
Conditional compilation is performed using the following preprocessor
directives:
#if
#else
#elif
#endif
The conditional preprocessor directives allow us to conditionally include
the portions of the statements based upon the outcome of a constant
expression.
Syntax:
#if constant-expression
statement sequence
#endif
If the conditional expression is true, the statement sequence that is
placed between #if and #endif is compiled. Otherwise the statement
sequence is skipped.
Ex:
#define COUNT 50
main()
{
#if count>20
printf(“The count is greater than 20! \n”) ;
#endif
}

21
22

You might also like