0% found this document useful (0 votes)
134 views25 pages

Module 5

The document discusses structures and pointers in C. It begins by defining a structure as a way to group different data types together under a single name. It then provides examples of how to declare and define a structure, including creating a "book" structure to store book details. The document also discusses how to declare structure variables, access structure members, initialize structures, copy structures, and use arrays of structures. It concludes by discussing pointers in C and how they contain the address of another variable.

Uploaded by

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

Module 5

The document discusses structures and pointers in C. It begins by defining a structure as a way to group different data types together under a single name. It then provides examples of how to declare and define a structure, including creating a "book" structure to store book details. The document also discusses how to declare structure variables, access structure members, initialize structures, copy structures, and use arrays of structures. It concludes by discussing pointers in C and how they contain the address of another variable.

Uploaded by

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

STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

MODULE-5

STRUCTURE AND POINTERS, PREPROCESSOR


DIRECTIVES

4.1 INTRODUCTION TO STRUCTURE

We have seen that arrays can be used to represent a group of data items that
belong to the same type, such as int or float. However, we cannot use an array if
we want to represent a collection of data items of different types using a single
name. Therefore C supports a constructed USER DEFINED DATA TYPE
known as STRUCTURES.

Definition: A mechanism for packing data of different types or it is the collection


of heterogeneous data types grouped together.

For example:
It can be used to represent a set of attributes, such as student_name,
Roll_number and marks.

More example of structured data types are

• time : seconds, minutes, hours


• date : day,month,year
• book : author, title, price year
• city : name, country,population
• address : name, door-number, street, city
• inventory: item, stock , value
• customer : name, telephone, city, category.

4.2 HOW TO CREATE USER DEFINE DATA TYPE IN C

Consider a book database consisting of book name, author, number of pages, and
price.
book_bank

Book_name Author Number of pages price


20 character 15 character int float
template

Dept. of ISE, Sapthagiri College of Engg. 1


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

How to create User defined data types or template using C syntax

struct tag_name
{
data_type member1;
data_type member2;
:
:
:
};

example:

struct book_bank
{
char book_name[20];
char author[15];
int pages;
float price;
};

4.3 DECLARING STRUCTURE VARIABLES

After defining a structure format we can declare variables of that type. A


structure variable declaration is similar to the declaration of variables of any other
data types. It includes the following elements

• The keyword struct,


• The structure tag name.
• List of variable names separates by commas
• A terminating semicolon.

Eample:

struct book_bank book1, book2, book3;

Each one of these variables has four members as specified by the template.

Dept. of ISE, Sapthagiri College of Engg. 2


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

The complete declaration might look like this:

struct book_bank
{
char title[20],
char author[15];
int pages;
float price;
};

struct book_bank boolk1,book2, book3;

[OR]

struct book_bank
{
char title[20],
char author[15];
int pages;
float price;
} boolk1,book2, book3;

4.4 ACCESSING STRUCTURE MEMBERS

we can access and assign values to the members of a structure in a number of


ways.
if any structure member can access only with help of structure variable followed
by member operator ‘.’ then member name.

example : book1.price

Note: Different way can assign the value to structure members.

strcpy(book1.title, “C PROGRAM”);
strcpy(book1.author, “BALAGURUSWAMY”);
book1.pages = 250;
book1.price = 120.50;

we can also use scanf to give the values through the keyword.

scanf(“%s\n”,book1.title);
scanf(“%d\n”,&book1.pages);

Dept. of ISE, Sapthagiri College of Engg. 3


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

EXAMPLE 1:

Define a structure type, struct personal that would contain person name, date of
joining and salary. Using this structure, write a program to read this information
for one person from the keyboard and print the same on the screen.

#include<stdio.h>
#include<conio.h>
struct personal
{
char name[20];
int day;
char month[10];
int year;
float salary;
};

void main()
{
struct personal person;
clrscr();
printf("enter the personal details");
scanf("%s %d %s %d %f", person.name, &person.day, person.month, &person.year,
&person.salary);
printf("%s %d %s %d %f", person.name, person.day, person.month, person.year,
person.salary);
getch();
}

4.5 STRUCTURE INITIALIZATION

Like any other data type, a structure variable can be initialized at compile time.

First method:
main( )
{

struct
{
int weight;
float height;

Dept. of ISE, Sapthagiri College of Engg. 4


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

} student = { 60, 180.75};

……………..
……………..

this assigns the value 60 to student.weight and 180.75 to student.height.

Second Method:

main( )
{

struct st_record
{

int weight;
float height;
};

struct st_record student1 = { 60, 180.75 };


struct st_record student2= {53,170.60};

……………….
……………..
}

Third Method:
struct st_record
{

int weight;
float height;
} student1 = { 60, 180.75 };

main( )
{

struct st_record student2= {53,170.60};

……………….
……………..
}

Note: C language does not permit the initialization of individual structure


members within the template. The initialization must be done only in the
declaration of the actual variables.

Dept. of ISE, Sapthagiri College of Engg. 5


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

4.6 COPYING AND COMPARING STRUCTURE VARIABLES

Two variables of the same structure type can be copied the same way as ordinary
variables. If person1 and person2 belong to the same structure, then the
following statement are

person1 = person2;
person2 = person1;

Note:
However, the statement such as

person1 == person2
person2 != person2

are not permitted. C does not permit any logical operations on structure variables.
In case, we need to compare them, we may do so by comparing members
individually.

EXAMPLE:
Write a program to illustrates how a structure variable can be copied into
another of the same type. It also performs members-wise comparison to decide
whether two structure variables are identical.

#include<stdio.h>
#include<conio.h>
struct class
{
int number;
char name[20];
float marks;
};

void main()
{
int x;
struct class s1= { 111,"sudarsanan",89.00};
struct class s2= { 222,"dass",87.00};
struct class s3;

s3=s2;
x = ((s3.number == s2.number) && (s3.marks == s2.marks)) ? 1 : 0;
clrscr();
if(x == 1)
{

Dept. of ISE, Sapthagiri College of Engg. 6


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

printf("\n student2 and student3 are same\n\n");


printf("%d %s %f\n",s3.number,s3.name,s3.marks);
}
else
printf("\n student2 and student3 are different\n\n");

getch();
}

output:

4.7 OPERATIONS ON INDIVIDUAL MEMBERS

A member with the dot operator along with its structure variable can be treated
like any other variable name and therefore can be manipulated using expressions
and operators.

example:

if ( student1.number == 111)

student1.marks = student1.marks +10.00;


sum = student1.marks + student2.marks;
student1.number++;
++ student1. number;

4.8 ARRAYS OF STRUCTURE

We use structures to describe the format of a number of related variables.

Example:

#include<stdio.h>
#include<string.h>
struct book_bank
{
int book_id;
char title[50];
char author[50];
};
int main( )
{

Dept. of ISE, Sapthagiri College of Engg. 7


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

struct book_bank book[100]; /* declare array of structures, this provides space


in memory for 100 stuctures of the type struct
book_bank */
int i ;
printf(“enter details of 100 books”);
for(i=1; i<=100; i++)
scanf(“%d%s%s,&book[i].id,book[i].title,book[i].author);

printf(“Book Details are \”);


printf(“Book-id\t\tBook title\t\tBook Author\n”);

for(i=1; i<=100; i++)


printf(“%d\t\t%s\t\t%s,book[i].id,book[i].title,book[i].author);
return 0;
}

4.9 ARRAY WITHIN STRUCTURE

As we know, structure is collection of different data type. like normal data type, it
can also store an array as well. i.e sometimes, arrays may be the member within
structure, this is known as arrays within structure.

Example:
#include<stdio.h>
struct student
{
int rollnumber;
char name[25];
int marks[3];
int total;
float avg;
};

void main ( )
{
int i;
struct student Sudent1

printf(“\n\n Enter Student Roll number:”);


scanf(“%d”,&Student1.rollnumber);
printf(“\n\n Enter Student Name:”);
scanf(“%s”,Student1.name);
Student1.total=0;
for(i=1; i<=3; i++)
{
printf(“\n\n Enter Marks %d :”,i);
scanf(“%d”,&Student1.marks[i]);
Student1.total = student1.total + Student1.marks[i];

Dept. of ISE, Sapthagiri College of Engg. 8


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

Student1.avg = Student1.total/3;

printf(“\n Roll Numbe: %d”,Studetn1.rollnumber);


printf(“\n Name: %s”,Studetn1.name);

printf(“\n Total: %d”,Studetn1.total);


printf(“\n Average: %f”,Studetn1.avg);

4.10 POINTERS

A pointer is a derived data type in C. It is built from one of the fundamental data
types available in C. Definition: A pointer is a variable that store memory address
or that contains address of another variable where address is the location number
always contains whole number. So, pointer contains always the whole number. It
is called pointer because it points to a particular location in memory by storing
address of that location.
Pointers can be used to access and manipulate data stored in the memory.

4.11 ADVANTAGE OF POINTERS OR USE OF POINTERS:


POINTERS:

1. Pointers are more efficient in handling arrays and data tables.


2. Pointers can be used to return multiple values from a function via function
arguments.
3. Pointers permit references to functions and thereby facilitating passing of
function as arguments to other functions.
4. Pointers allow C to support dynamic memory management.
5. Pointers provide an efficient tool for manipulating dynamic data structure
such as structures, linked lists, queues, stacks and trees
6. pointers reduce length and complexity of programs
7. They increase the execution speed and thus reduce the program execution
time.

4.12 UNDERSTANDING POINTERS

The computer’s memory is a sequential collection of storage cells. Each cell,


commonly known as a byte, has a number called ADDRESS ASSOCIATED
with it.
Example:

int quantity = 179;

Dept. of ISE, Sapthagiri College of Engg. 9


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

quantity Location Name or variable

179 value at location

5000 Location Address

ACCESSING THE ADDRESS OF A VARIABLE


Program example:
Write a program to print the address of a variable along with its value.
#include<stdio.h>
void main( )
{

int quantity = 179;

printf(“\n Value of i = %d”, i);


printf(“\nAdress of i = %d”,&i);

}
Output:

Value of i = 179
Address of i = 5000

4.13 DECLARING
DECLARING POINTER VARIABLE:

• Pointer variables are always declared with asterisk(*) appended to a


variable.
• The data type can be int, char, float, long and double.

Syntax:

data type *Variable Name;

example:

int *ip; /* pointer to memory that stored integer value */


float *fp; /* pointer to memory that stored float values */
double *dp;
char *ch; /* pointers to memory that stored char value */

4.14 INITIALIZATION
INITIALIZATION OF POINTER VARIABLES

HOW TO USE POINTERS


POINTERS ?

Dept. of ISE, Sapthagiri College of Engg. 10


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

There are a few important operations, which we will do with the help of pointers
very frequently

• we define a pointer variable


• Assign the address of a variable to a pointer, and
• Finally access the value at the address available in the pointer variable

This is done by using indirect operator * that returns the value of the variable
located at the address specified by its operand.

EXAMPLE:

Write a program to illustrate the use of indirect operator ‘*’ to access the value
pointed to by a pointer.

#include<stdio.h>
#include<conio.h>
void main()
{
int quantity=179;
int *ip;
ip=&quantity;
clrscr();
printf("\nThe value of i =%d\n",quantity);
printf("\nThe Address of i=%u\n",&quantity);
printf("\nThe value of ip = %d\n",*ip);
printf("\nThe Address of ip= %u\n",&ip);
printf("\nThe Address of i using pointer = %u\n",*&ip);
getch();
}

output:

4.15 NULL POINTER

Dept. of ISE, Sapthagiri College of Engg. 11


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

It is always a good practice to assign a NULL value to a pointer variable in case


you do not have an exact address to be assigned. This is done at the time of
variable declaration.

The NULL pointer is a constant with a value of zero defined in several standard
libraries.

#include<stdio.h>
#include<conio.h>
void main()
{
int *ipt = NULL;
clrscr();
printf("The value of ipt is: %x\n",ipt);
getch();
}

Output:

The value of ipt is: 0

4.16 ARITHMETIC OPERATIONS USING POINTERS

#include<sdtio.h>
void main()
{

int a=10 , b=6;


int *ip1, *ip2;
ip1 = &a;
ip2 = &b;

printf(“Sum of two pointers: %d\n”, *ip1+ b);


printf(“Subtraction of two pointers: %d\n”, a-*ip2);

output:
Sum of two pointer: 16
Subtraction of two pointers: 4

4.17 INCREMENTING POINTER VARIABLE

Write program to display the value stored in an array without using any index
value.

Dept. of ISE, Sapthagiri College of Engg. 12


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

#include<stdio.h>
int main( )
{
int a[3] = { 10,20,30};

int *ip, i;

ip = a;

for( i =0; i < 3; i++)


{
printf(“ %d\t”, *ip);
ip=ip + 1;
}
return 0;
}

output

10 20 30

4.18 COMPARING POINTER VARIABLE

#include<sdtio.h>
void main()
{

int a=10 , b=6;


int *ip1, *ip2;
ip1 = &a;
ip2 = &b;

if(*ip1 > *ip2)


printf(“value stored in a is greater than b”);
else
printf(“value stored in b is greater than a”);

4.19 POINTER TO POINTER OR CHAIN


CHAIN OF POINTERS

A pointer variable containing the address of another pointer variable is known as


pointer to pointer or a chain of pointers

syntax:

Dept. of ISE, Sapthagiri College of Engg. 13


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

datatype ** variable name;

Example:

#include<stdio.h>
#include<conio.h>
void main()
{
int quantity=179;
int *ip;
int **ipp;
ip=&quantity;
ipp = &ip;
clrscr();
printf("\nThe value of i =%d\n",quantity);
printf("\nThe value available at *ip = %d\n",*ip);
printf("\nThe value available at **ipp= %d\n"**ipp);
getch();
}

output:
The value of i= 179
Value available at *ip = 179
Value available at **ipp = 179

4.20 POINTERS
POINTERS AND STRUCTURE

A pointer to structure means a pointer variable can hold the address of a structure.
The address of structure variable can be obtained by using the ‘&’ operator. All
structure members inside the structure can be accessed using pointer, by
assigning the structure variable address to the pointer.

syntax:

struct stuctname
{
data type member1;
data type member2;
:
:
:
} variable1, *ptrvariable1; //pointer to structure decalaration

ACCESSING STRUCTURE MEMBERS USING POINTERS

The member can access using pointer with help of Arrow operator ( )

Dept. of ISE, Sapthagiri College of Engg. 14


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

example:
Write a program to illustrate the use of structure pointers

A program to illustrate the use of structure pointer to manipulate the element of


an array of structures.

#include<stdio.h>
struct invent
{
char *name[20];
int number;
float price;
};

main()
{
struct invent product[3], *ptr;
printf(“\n\Ninput\n\n”);
for (ptr=product; ptr< product+3; ptr++)
scanf(“%s %d %f”,ptr->name, &ptr->number,&ptr->price);

printf(“\n\Noutput\n\n”);
ptr=product;

while(ptr<product+3)
{
printf(“%-20s %5d %10.2f\n”, ptr->name,ptr->number,ptr->price);
ptr++;
}
}

input:

washing_machine 5 7500
electric_iron1 12 350
Two_in_one 7 1250

output:

washing_machine 5 7500
electric_iron1 12 350
Two_in_one 7 1250

Dept. of ISE, Sapthagiri College of Engg. 15


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

4.21 THE
THE PREPROCESSOR

INTRODUCTION:

The unique features of the C language are the pre-processor. The pre-processor
provides several tools. The programmer can use these tools to make his program
easy to READ, EASY TO MODIFY, PORTABLE AND MORE EFFICIENT.

The pre-processor, as its name implies, is a program that processes the


source code before it passes through the compiler. It operates under the control of
what is known as pre-
pre-processor command line or directives.

Points to note:
• Pre-processor directives are placed in the source program before the main
line.
• Pre-processor directives follow 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.

4.22 COMMONLY USED PROPROCESSOR DIRECTIVES IN C

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


2. File inclusion directives
3. Compiler control directives

4.23 MACRO SUBSTITUTION

Macros: Macros are piece of code in a program which is given some name.
Whenever this name is encountered by the compiler the compiler replaces the
name with the actual piece of code.

Dept. of ISE, Sapthagiri College of Engg. 16


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

HOW TO DEFINE MACRO IN C

Syntax:
#define identifier string

Note:
• The keyword #define is written just as shown followed by the identifier
and a string,
string with at least one blank space between them.
• Note that the definition is not terminated by a semicolon.
• The string may be any text
• While the identifier must be a 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

1. SIMPLE MACRO SUBSTITUTION

Simple string replacement is commonly used to define constants.

Example:
#define COUNT 100
#define FALSE 0
#define PI 3.1415926
#define CAPITAL “DELHI”

NOTE: We have written all macros(identifiers) in capitals

Program example:

#include<stdio.h>
#define LIMIT 5
int main( )
{

int i ;
for (i=0; i< LIMIT; i++)
{
printf(“%d”,i);
}
return 0;
}

Dept. of ISE, Sapthagiri College of Engg. 17


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

SIMPLE CONSTANT VALUE USING EXPRESSION

Example 1
#define AREA 5*12.36

#define SIZE sizeof(int) * 4

#define TWO-PI 2.0*3.1415926

Example 2:

ratio = (45-22)/(78+32)

#include<stdio.h>
#define N (4+44)
#define D (4+2)

void main( )
{
float ratio;

ratio = N/D;

printf(“ratio = %f”,ratio);
}

output:

ration = 8.0

EXAMPLE 2

write a macro program for the following code

#include<stdio.h>
//macro code
void main( )
START

--------
--------
if( total EQUALS 240 AND avg EQUALS 60)
START
INCREMENT count;
END
------
}

Dept. of ISE, Sapthagiri College of Engg. 18


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

solution:

#include<stdio.h>
#define START {
#define END }
#define EQUALS ==
#define AND &&
#define INCREMENT ++
void main( )
START

--------
--------
if( total EQUALS 240 AND avg EQUALS 60)
START
INCREMENT count;
END
------
}

2. MACROS WITH ARGUMENTS OR PARAMETERIZED MACROS

The pre-processor permits us to define more complex and more useful form of
replacements.

syntax:

#define identifier(f1,f2,……fn) string

• There is no space between the macro identifier and the left


parentheses.
• The identifiers f1,f2, ….fn are the formal macro arguments
• when a macro is called, the pre-processor substitutes the string,
replacing the formal parameters with the actual parameters

EXAMPLE:

#include<stdio.h>
#define CUBE(x) (x*x*x)
void main( )
{
int result, a=5;
result=CUBE(a);
printf(“cube of %d =%d”,a,result);
}
output:
cube of 5 = 125

Dept. of ISE, Sapthagiri College of Engg. 19


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

3. NESTED OF MACROS

We can also use one macro in the definition of another macro. That is, macro
definitions may be nested.

example:
#include<stdio.h>
#include<conio.h>
#define SQUARE(x) ((x) * (x))
#define CUBE(x) (SQUARE(x) * (x))
#define SIXTH(x) (CUBE(x) * CUBE(x))

void main( )
{
int result, a=2;

result =SIXTH(a);
clrscr();
printf("SIXTH(%d) = %d",a,result);
getch();
}

output:

SIXTH(2) = 64

4.24 FILE INCLUSION

This type of pre-processor directive tells the compiler to include a file in the
source code program. There are two types of files which can be included by the
user in the program.

1. Header file or Standard file: These files contains definition of pre-defined


functions like printf() , scanf() etc. These files must be included for
working with these functions. Different function are declared in different
header files.

#include<filename.h>

example:

#include<stdio.h>

2. User defined files: When a program becomes very large, it is good practice
to divide it into smaller files and include whenever needed. These files can
be included as

Dept. of ISE, Sapthagiri College of Engg. 20


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

#include ”filename.c”
#include ”filename.h”

PROGRAM EXAMPLE:

filename: addinclu.c
#include<stdio.h>
#include "add.c"
void main()
{
int a,b,c;
clrscr();
printf("enter the value of a and b\n");
scanf("%d%d",&a,&b);
c=add(a,b);
printf("The addition of two number = %d\n",c);
getch();
}

filename: add.c

int add(int x,int y)


{
return(x+y);
}

4.25 COMPILER CONTROL DIRECTIVES OR CONDITIONAL


COMPILATION

Conditional compilation directives are type of directives which helps to compile a


specific portion of the program or to skip compilation of some specific part of the
program based on some condition. This can be done with the help of two pre-
processing commands “ifdef” and “endif”

Syntax :

#ifdef MACRONAME
Statement_block;
#endif

1. If the MACRONAME specified after #ifdef is defined previously in


#define then statement_block is followed otherwise it is skipped
2. We say that the conditional succeeds if MACRO is defined, fails if it is not.

Dept. of ISE, Sapthagiri College of Engg. 21


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

EXAMPLE 1:

#include<stdio.h>
#define NUM 10

void main()
{
// Define another macro if MACRO NUM is defined

#ifdef NUM
#define MAX 20
#endif

printf("MAX number is : %d",MAX);


}

OUTPUT:

MAX Number is 20

Example 2:
#include<stdio.h>

void main()
{

#ifdef MAX
#define MIN 90
#else
#define MIN 100
#endif

printf("MIN number : %d",MIN);


}

output:

Min number : 100

example 3:

#include<stdio.h>
#define MAX 10
void main()
{

#ifdef MAX

Dept. of ISE, Sapthagiri College of Engg. 22


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

#define MIN 90
#else
#define MIN 100
#endif

printf("MIN number : %d",MIN);


}

example 4:
#ifndef : Return true if this macro is not defined

#include"stdio.h"

void main()
{
// Define another macro if MACRO NUM is defined

#ifndef NUM
#define MAX 20
#endif

printf("MAX number is : %d",MAX);


}

output:
MAX number is: 20

example 5:

#include<stdio.h>
#define MAX 90

void main()
{
#ifndef MAX
#define MIN 90
#else
#define MIN 100
#endif

printf("MIN number : %d",MIN);

}
output:

MIN number: 100

Dept. of ISE, Sapthagiri College of Engg. 23


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

#undef : Preprocessor Directive in C Programming


#undef : it is undefined a pre-processor macro.

Syntax:
#undef <identifier>

example:

#include<stdio.h>
#define TEMP 10

#ifdef TEMP
#undef TEMP
#define TEMP 75
#else
#define TEMP 100
#endif

int main()
{
printf("%d",TEMP);
return 0;
}

Output :

75

4.26 Pre-
Pre-Defined Macros
Macro Description
_DATE_ The current date as a character literal in “MMM DD
YYYY”
_TIME_ The current time as a character literal in “HH:MM:SS”
format
_FILE_ This contains the current filename as a string literal
_LINE_ This contains the current line number as a decimal
constant

Example:

#include<stdio.h>

int main(){

#ifdef __DATE__
printf("%s",__DATE__);

Dept. of ISE, Sapthagiri College of Engg. 24


STRUCTURE AND POINTERS, PREPROCESSOR DIRECTIVES MODULE 5 Sudarsanan D

#else
printf("__DATE__ is not defined");
#endif

return 0;
}

output:

May 29 2019

Dept. of ISE, Sapthagiri College of Engg. 25

You might also like