Module 5
Module 5
MODULE-5
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.
For example:
It can be used to represent a set of attributes, such as student_name,
Roll_number and marks.
Consider a book database consisting of book name, author, number of pages, and
price.
book_bank
struct tag_name
{
data_type member1;
data_type member2;
:
:
:
};
example:
struct book_bank
{
char book_name[20];
char author[15];
int pages;
float price;
};
Eample:
Each one of these variables has four members as specified by the template.
struct book_bank
{
char title[20],
char author[15];
int pages;
float price;
};
[OR]
struct book_bank
{
char title[20],
char author[15];
int pages;
float price;
} boolk1,book2, book3;
example : book1.price
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);
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();
}
Like any other data type, a structure variable can be initialized at compile time.
First method:
main( )
{
struct
{
int weight;
float height;
……………..
……………..
Second Method:
main( )
{
struct st_record
{
int weight;
float height;
};
……………….
……………..
}
Third Method:
struct st_record
{
int weight;
float height;
} student1 = { 60, 180.75 };
main( )
{
……………….
……………..
}
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)
{
getch();
}
output:
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)
Example:
#include<stdio.h>
#include<string.h>
struct book_bank
{
int book_id;
char title[50];
char author[50];
};
int main( )
{
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
Student1.avg = Student1.total/3;
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.
}
Output:
Value of i = 179
Address of i = 5000
4.13 DECLARING
DECLARING POINTER VARIABLE:
Syntax:
example:
4.14 INITIALIZATION
INITIALIZATION OF POINTER VARIABLES
There are a few important operations, which we will do with the help of pointers
very frequently
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:
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:
#include<sdtio.h>
void main()
{
output:
Sum of two pointer: 16
Subtraction of two pointers: 4
Write program to display the value stored in an array without using any index
value.
#include<stdio.h>
int main( )
{
int a[3] = { 10,20,30};
int *ip, i;
ip = a;
output
10 20 30
#include<sdtio.h>
void main()
{
syntax:
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
The member can access using pointer with help of Arrow operator ( )
example:
Write a program to illustrate the use of structure pointers
#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
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.
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.
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
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.
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:
Example:
#define COUNT 100
#define FALSE 0
#define PI 3.1415926
#define CAPITAL “DELHI”
Program example:
#include<stdio.h>
#define LIMIT 5
int main( )
{
int i ;
for (i=0; i< LIMIT; i++)
{
printf(“%d”,i);
}
return 0;
}
Example 1
#define AREA 5*12.36
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
#include<stdio.h>
//macro code
void main( )
START
--------
--------
if( total EQUALS 240 AND avg EQUALS 60)
START
INCREMENT count;
END
------
}
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
------
}
The pre-processor permits us to define more complex and more useful form of
replacements.
syntax:
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
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
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.
#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
#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
Syntax :
#ifdef MACRONAME
Statement_block;
#endif
EXAMPLE 1:
#include<stdio.h>
#define NUM 10
void main()
{
// Define another macro if MACRO NUM is defined
#ifdef NUM
#define MAX 20
#endif
OUTPUT:
MAX Number is 20
Example 2:
#include<stdio.h>
void main()
{
#ifdef MAX
#define MIN 90
#else
#define MIN 100
#endif
output:
example 3:
#include<stdio.h>
#define MAX 10
void main()
{
#ifdef MAX
#define MIN 90
#else
#define MIN 100
#endif
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
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
}
output:
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__);
#else
printf("__DATE__ is not defined");
#endif
return 0;
}
output:
May 29 2019