Module 5 - Complete Notes
Module 5 - Complete Notes
Arrays allow to define type of variables that can hold several data items of the same
kind. Similarly structure is another user defined data type available in C that allows to combine
data items of different kinds.
A structure creates a data type that can be used to group items of possibly different types into a single
type. 'struct' keyword is used to create a structure. Following is an example. A structure variable can
either be declared with structure declaration or as a separate declaration like basic types.
Structures are used to represent a record. Suppose you want to keep track of your books in a
library. You might want to track the following attributes about each book −
Title
Author
Subject
Book ID
Defining a Structure
To define a structure, you must use the struct statement. The struct statement defines a new
data type, with more than one member. The format of the struct statement is as follows −
member definition;
member definition;
...
member definition;
The structure tag is optional and each member definition is a normal variable definition, such
as int i; or float f; or any other valid variable definition. At the end of the structure's definition,
before the final semicolon, you can specify one or more structure variables but it is optional.
Here is the way you would declare the Book structure −
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main( )
/* book 1 specification */
Book1.book_id = 6495407;
/* book 2 specification */
Book2.book_id = 6495700;
return 0;
When the above code is compiled and executed, it produces the following result −
Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700
char name[50];
char language[50];
int population;
char name[50];
char address[50];
int year_of_birth;
int month_of_birth;
int day_of_birth;
char name[50];
char language[50];
int population;
}India={“India”,”Hindi”,136},
japan={“japan,”Japaneese”,12},
Indonesia ={“Indonesia”,”english”,3}
char name[50];
char language[50];
int population;
};
#include <stdio.h>
#include<conio.h>
struct student
{
char name[50];
int roll;
float marks;
} s[100];
int main()
{
int i,n;
printf("\n");
}
printf("Displaying Information:\n\n");
// displaying information
for(i=0; i<n; ++i)
{
printf("\nRoll number: %d\n",i+1);
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
printf("\n");
}
getch();
}
------------------------------*-----------OUTPUT----------------*------------------------
Displaying Information:
Roll number: 1
Name: John
Marks: 87.0
Roll number: 2
Name: Sam
Marks: 25.0
Roll number: 3
Name: Jack
Marks: 77.0
a. Program to demonstrate the difference between structure and union by displaying size of a
structure and a union.
#include <stdio.h>
#include<conio.h>
#include <string.h>
struct student_Structure
{
char name[50];
int roll;
float marks;
} s;
union student_Union
{
char name[50];
int roll;
float marks;
} u;
int main()
{
strcpy(s.name,"John");
s.roll = 1;
s.marks= 100;
strcpy(u.name,"John");
u.roll = 1;
u.marks= 100;
------------------------------*-----------OUTPUT----------------*------------------------
Size of structure 60
Size of Union 52
Printing data in a structure
John 1 100.000000
Printing data in a union
1120403456 100.000000
Arrays Structures
1. An array is a collection of related 1. Structure can have elements of
data elements of the same type. different types
2. An array is a derived data type 2. A structure is a programmer-
defined data type
3. Any array behaves like a built-in 3. But in the case of structure, first,
data types. All we have to do is to we have to design and declare a data
declare an array variable and use it. structure before the variable of the
structure type are declared and used.
4. Array allocates static memory 4. Structures allocate dynamic
memory
5. Array uses index/subscript for 5. Structures uses (.) operator for
accessing elements of the array. accessing the member of a structure.
5. An array is a pointer to the first
5. Structure is not a pointer
element of it
6. Element access takes relatively 6. Structure attributes access takes
less time. relatively large time.
The C Preprocessor
It is an instruction that processes source program before it is passed to the compiler often
called as directive.
Features of C Preprocessor
They begin with #symbol
Can be placed anywhere in a program
Offers preprocessor directives
i) Macro expansion
Conditional Compilation
We can have the compiler skip over part of a source code by inserting the
preprocessor commands #ifdef and #endifwhich would havethe general format
#ifdef macroname
Statement 1;
Statement 2;
Statement 3;
#endif
If the macroname has been #defined,the block of code will be processed as usual
otherwise not
Where can I make use of ifdef be used? when would you like to the compile only a
part of your program:
Case1:
To “comment out” obsolute line of code
Case2: to make them work on two totally different computers
Example:
main( )
{
#ifdef OKAY
Statement 1;
Statement 2;
Statement 3;
#endif
Statement 4;
Statement 5;
}
Here, statement 1,2,3 would be compiled only the macro OKAY has been defined and we have
purposefully omitted the definition of the macro OKAY. if we want them to compile all the
codes in the program we can omit the statements #ifdef and #endif.
Example:
main ( )
{
#ifdef PCAT
Code suitable for a PC/AT
#else
Code suitable for a PC/AT
#endif
Code common to both the computers
}
When you compile this program it would compile only the code suitable for a PC/XT and
the common code. This is because the macro PCAT has not been defined, the working of #ifdef-
#else-#endif is similar to the ordinary if-else controls instruction.
Below is the list of preprocessor directives that C programming language offers with few more
examples
Syntax/Description
Preprocessor
Syntax: #define
Macro This macro defines constant value and can be any of the
basic data types.
Syntax: #include<file_name>
Header file
The source code of the file “file_name” is included in the
inclusion
main program at the specified place.
#include <stdio.h>
void main()
{
printf("value of height : %d \n", height );
printf("value of number : %f \n", number );
printf("value of letter : %c \n", letter );
printf("value of letter_sequence : %s \n", letter_sequence);
printf("value of backslash_char : %c \n", backslash_char);
#include <stdio.h>
#define RAJU 100
int main()
{
#ifdef RAJU
printf("RAJU is defined. So, this line will be added in " \
"this C file\n");
#else
printf("RAJU is not defined\n");
#endif
return 0;
}
#include <stdio.h>
#define RAJU 100
int main()
{
#ifndef SELVA
{
printf("SELVA is not defined. So, now we are going to " \
"define here\n");
#define SELVA 300
}
#else
printf("SELVA is already defined in the program”);
#endif
return 0;