2. Structures –Pointers to Structures – Array
of Structures – Structures within a Structure –
Functions and Structures – typedef and
Structures – Unions–Practical Applications of
Unions – Enumerations – Bit fields – Storage
Classes –C Preprocessor –Files: Streams – File
type – File operations – Command line
arguments.
4. STRUCTURES
Definition:
Structure having collection of dissimilar data
items that are stored under a single common
name.
The structure is a convenient way for handling
different type of logically related data type
variables.
Example:
student :register number , name , CGPA..etc
employee : employee id , name age ….etc
ice-cream : flavor , price , brand name …etc
5. There are three part of structure
1. Defining a structure
2. Declaring structure variable
3. Accessing object of structure type
7. Description:
struct - it is a keyword
structure element - it is individual variable
declaration, it may be
pointer , normal variable ,
arrays…
; - it is a symbol , A structure must be
ended with semicolon ;
8. Example: keyword
struct book
{
char book_name[];
int pages; structure element
float price;
};
struct book b; structure variable
structure name
Accessing structure element:
b.book_name;
b.pages;
b.price;
10. Declaring a structure variable
A structure variable declaration is similar to
the declaration of normal variable .it having
following statement .
1. Struct keyword
2. Structure name
3. List of structure variable name
4. Terminating with semicolon.
Syntax:
struct structure name var1 , var2…var n ;
12. Rules for declaring a structure
1. A structure must be ended with semicolon .
2. Usually a structure appears at the top of the
source program.
3. The structure element must be accessed with
structure variable with dot operator.
13. Initialization of structure
A structure can be initialized same as other
data types initialization.
Here we assign some values to the
structure elements.
The structure element initialized into two ways:
1. Run time initialization
2. Compile time initialization
17. Array of structure
‘C’ language permits to declare an array of structure
variable.
Example:
If you want to handle more records with in one
structure , we need not specify the number of structure
variable, simply use array of structure variable.
Example:
struct book
{
char name;
int pages;
float price;
};
struct book b[3];
B[0].name
B[0].pages
B[0].price
B[1].name
B[1].pages
B[1].price
B[2].name
B[2].pages
B[2].price
19. Pointer to structure
Here we declare the structure variable as a pointer
then it points all the elements with in the same structure.
Here we add * (asterisk) symbol before the structure
pointer variable.
Syntax:
struct structure name
{
element 1;
element 2;
…….
…….
element n ;
};
struct structure name * pointer variable ;
22. struct book b={“cp” , 150 , 120.75};
struct book *p;
p = &b;
clrscr();
printf(“%s %d %f”, b.name , b.pages , b.price);
printf(“%s %d %f”, p --> name , p -->pages ,
p--> price);
getch();
}
o/p:
cp 150 120.75
cp 150 120.75
23. Structure with function
The main advantage of the C language we
use the function with in the structure.
There are following ways available to pass
the structure variables to the function there
are.
(1) Pass each member value of the structure
as an actual argument of the function.
(2) pass the address location of the structure
to the called function.
(3) Pass a copy of the entire structure to the
called function.
24. 1. Pass each member of the structure as
an actual argument of the function:
Here we pass the entire structure element
value passed from function call to function
definition
So here all the structure values accessed
by the called function( in function definition)
25. 1 - Call by value: Example:
#include<stdio.h>
#include<conio.h>
void main()
{
void show(int , int ); //function declaration
struct student
{
int marks;
int age;
};
struct student s={97, 18};
show(s.marks , s.age); // function call
}
void show(int a , int b) // function definition
{
printf(“%d%d”, a, b);
getch();
}
26. (2) Pass the address location of the structure to
the called function.
Here we pass the entire structure is
passed to another function by address ,
it means the address of the structure is
passed to the called function(function
definition)
27. 2& 3 rd type - Call by reference:
#include<stdio.h>
#include<conio.h>
void main()
{
void show(struct book *); //function declaration
struct book
{
char name[20];
int pages;
float price;
};
struct book b={“cp” , 150 , 255.50};
show(&b); //function call
}
void show(struct book *p) function definition
{
printf(“%s%d%f”, p--> name , p--> pages , p--> price );
getch();
}
28. Nested structure (or) structure with in
a structure
A structure having inside another structure
then that is called as nested structure.
Here we declare the one structure variable
as a member element of another structure , it
is mostly used in complex data structure.
The element of nested structure are
accessed by using dot operator ,
29. Syntax:
struct structure name 1
{
member element 1;
member element 2;
………………………..
member element N ;
};
Struct structure name 2
{
member element 1 …….. N ;
struct structure name 1 variable;
};
struct structure name 2 variable2 ;
30. Self-referential structure
A structure consist of at least a pointer member
pointing to the same structure is known as self
referential structure.
self referential structure is mostly used in linked
data structures , Such as list and trees.
Syntax:
struct book
{
member element 1;
member element 2 ;
……………….
struct book *b1;
};
31. Example:
#include<stdio.h>
Void main()
{
struct book;
{
char bookname[20];
int pages ;
float price;
struct book *b1;
};
struct book b2={“pc” , 350 , 500.00};
b1=&b2;
printf(“%s%d%f” , b1 - - > book name ,
b1 - - > pages ,
b1 - - > price );
}
32. Union
Union holds collection of dissimilar data
items that are stored under a single common
name.
union and structure differs only in the
terms of storage.
In structure memory will be allocated for
all the elements with in the structure.
In union memory allocated for the first
element with in the union , all the other
elements share the common memory space.
35. Storage Classes
1. auto
2. static
3. extern
4. register
Syntax:
storage class type data type variable name;
36. The auto storage class
Automatic variables are also called as auto
variables, which are defined inside a function.
Auto variables are stored in main memory.
Variables has automatic (local) lifetime.
It is a default storage class if the variable is
declared inside the block.
Here initial value is garbage value.
38. 2. The register storage class
Register variables can be accessed faster than
others
Variables are stored in CPU.
It is also declared with in the program.
Variables have automatic (local) lifetime.
Register variables are used for loop counter to
improve the performance of a program.
40. 3. The static storage class
Static variables have static (global) lifetime.
Static variables are stored in the main
memory.
Static variables can be declared in local scope
as well as in the global scope.
Here Last change made in the value of static
variable, remains throughout the program
execution.
42. 4. The extern storage class
Variables that are used by all functions are
called external or global variables.
External variables are stored in main memory.
External variables have static (global) lifetime.
It is declared outside of the function
44. Preprocessor directives
These are placed before the main function in
source program.
If there is any preprocessor directives ,
The appropriate actions are taken and then the
source program is moved to compilation.
preprocessor directives is begin with “#” symbol.
It having two major types conditional and
unconditional.
Types:1.File inclusion
2.Macro substitution
3.Miscellaneous
4.Conditional inclusion
45.
46. File inclusion:
File inclusion is the process of include an
external file. which contains functions or some
other macro definition.
#include <stdio. h> it means to get
stdio.header file from System Libraries
#include "myheader.h“ tells to get
myheader.h from the local directory
Syntax:
#include<filename>
Example:
#include<stdio.h>
#include<add.c>
47. Macro substitution:
It is used to define a macro and use it into
source program, it may be string or integer.
Syntax:
#define identifier string/integer
Example:
#define Pi 3.14
#define name “ragul”
It having three Type:
1. Simple macro - It is used to define some
constants.
2. Augmented macro
3. Nested macro
48. Argumented macro
It is used to define some complex forms in
the source program.
Syntax:
#define identifier(v1 ,v2 ,v3 …. Vn)
string/integer
Example:
#define cube(n) (n*n*n)
#define a,b 10
49. Example for argumented macro:
#include<stdio.h>
#define cube(n) (n*n*n)
void main()
{
int a=3 , b;
b=cube(a);
printf(“The cube of 3 is %d”, b );
}
o/p: The cube of 3 is 27
51. Nested macro:
The macro defined within another macro
called nested macros.
Example:
#define A 5
#define B A+5
52. Conditional inclusion:
#ifdef - used to test the macro, if the macro
is defined or not
#else - used to specify alternative in ifdef
#undef - used to undefined a macro
#endif - the conditional macro must be
terminated from if condition.
53. Example:
# include<stdio.h> file inclusion
#include<conio.h>
#define A 5 simple macro
#ifdef A
#define c 10
#else conditional inclusion
#define c 20
#endif
void main()
{
int r;
r=c*3;
printf(“The value of r is %d”, r);
getch();
}
o/p The value of r is 30
54. Miscellaneous directives
#pragma startup and #pragma exit:
These directives allow us to specify which functions is called first
(program startup) with in multiple functions or specifies which function
program exit . Their usage is as follows:
Example:
void fun1( ) ;
void fun2( ) ;
#pragma startup fun1
#pragma exit fun2
void main( )
{
printf ( “inside main" ) ;
}
void fun1( )
{
printf ( "Inside fun1" ) ;
}
void fun2( )
{
printf ( "Inside fun2" ) ;
}
55. Predefined Macros
1. __DATE__ : It containing the current date
2. __FILE__ : it containing the file name
3. __LINE__ : it representing the current line
number
4. __TIME__ : String containing the current
date.
56. • A file is a collection of related information These
information may be numeric, alphabetic or
alphanumeric.
• ‘C’ Allows data to be stored permanently in a
data file with the help of set of library function
for handling these data files.
• By using these library function to store data in a
secondary devices in the form of data file.
• Operations such as create, open, read, write and
close are performed on files.
File
57. 1. Sequential access
This type of file is to be accessed sequentially ,
Here we want to access the last record in a file you
must read all the records sequentially . It takes more
time for accessing the record.
2. Random access
This type of file allows access to the specific
data directly with out accessing its preceding data
items.
Here the data can be accessed and modified
randomly.
Types of file accessing
58. Here we read and write also manipulate
the character type of data, by using several
standard predefined library function.
Basic file Operations
– Opening a file
– Reading a file
– Writing a file
– Closing a file
Sequential access file (Text file
processing )
59. • fopen()-create a new file or opens an existing
file
• fclose()-close a already opened text file
• fprintf()-writes a set of data values to a file
• fscanf()-reads a set of data values to a file
• fgetc()-read a character from a file
• fputc() -writes a character to a file
Sequential access functions
60. fopen()
Create a new file or opens an existing file.
Opening a file means creating a new file with
specified filename with accessing mode.
Syntax:
FILE *file_pointer _name ; //
file_pointer_name = fopen(“filename.txt ” , mode”);
Example:
FILE *a ;
a = fopen( “emplyeeinfo.txt”, “w”);
61. fclose()
It close a already opened text file
A file must be closed after all the
operation have been completed
Syntax:
fclose(file_pointer_name);
Example:
FILE *a ;
a = fopen( “emplyeeinfo.txt”, “r”);
fclose(a);
62. File read:
Read the character from the file using fgetc()
The fgetc() function is used to read a character
from the file which is opened in read mode.
Syntax:
c= fgetc(file pointer name );
Reading data from the file :fscanf()
Reads a set of data values to a file ,
It is similar to the scanf () except that fscanf() to
read the data from secondary storage disk.
Syntax:
fscanf ( fptr , “format/control string ” , &v1,&v2..&vn );
Example:
fscanf( fptr , “ %d%s %d”, &rollno , &sname ,&tot);
63. File write :
Write a character to a file : fputc()
This function is used to write a character variable
into a file that is opened in write mode.
Syntax:
fputc( x , fptr1);
Write data to a file :fprintf()
This function writes a set of data values to a file , it
is similar to printf() except that fprintf() function write
data into the disk.
Syntax:
fprintf(fptr , “format/control string ” , v1,v2..);
Example:
fprintf( fptr , “%d %s %d ”, rollno,sname,total);
64. File mode Description
r Open a text file for reading.
w Create a text file for writing, if it exists, it is
overwritten.
a Open a text file and append text to the end of the
file.
r+ existing file opened for both reading and writing
w+ new file is opened for reading and writing
a+ existing file opened for both reading and writing
File Accessing modes
65. rb Open a binary file for reading.
wb Create a binary file for writing, if it
exists, it is overwritten.
ab existing binary file opened for both
reading and writing
BINARY FILE
66. Example program: to create a file to store details of
n students.
#include<stdio.h>
#include<conio.h>
Void main()
{
int rollno , totalmark , i , n;
char name[20];
FILE *fptr;
fptr = fopen( “student.txt” , “w”);
clrscr();
printf(“enter the how many students”);
scanf( “%d” , &n);
68. 1. fseek()-sets the position to desired point in
the file
2. rewind()-sets the position to the beginning
of the file
3. ftell()-gives the current position in the file
4. fgetpos()
5. fsetpos()
Random access files
69. 1. F seek()-this function is used to set the file position
pointer to the given text file .
Syntax:
int fseek(FILE *stream , long offset , int origin);
Description:
FILE * stream_pointer - file pointer name
Offset - is an integer value that gives the number of
bytes to move forward or backward.
Origin argument may have values
– SEEK_SET - Beginning of file
– SEEK_CUR - Current position of the file pointer
– SEEK_END - End of file
Example:
Int fseek( fptr, 0L , SEEK_SET)
70. 2. ftell()
This function is used to know the current position of the file
pointer , it is used to read or write the data from the current
position.
Syntax:
ftell (FILE *stream);
Example:
printf(“%d” , ftell(fptr));
3. rewind()
sets the position to the beginning of the file.
After this process the file pointer is moved into beginning of
the file .
Syntax:
void rewind( FILE *fptr);
Example:
void rewind( fptr );
71. The rewind() function is equivalent to
calling fseek() with the following parameter.
fseek( file pointer name , 0L , SEEK_SET);
Example :
FILE *fptr;
fptr= fopen( “student.txt” , “r”);
fseek( fptr , 0L , SEEK_SET);
72. 4. fgetpos()
The fgetpos() function is used to determine the current
position of the stream
Syntax:
int fgetpos (FILE *stream , fpos_t *pos);
Description:
int - return data type
FILE *stream - file pointer name
fpos_t*pos - it is predefined library file in stdio.h
Here pos represent the current position of the file pointer .
5. fsetpos()
The fsetpos() function is used to move file pointer to the
location indicated in pos.
SYNTAX:
int fsetpos( FILE stream , constant value , fpos_t
pos );
73. • fseek(), ftell() and rewind() :
#include<stdio.h>
void main()
{
FILE *fp;
int i;
fp = fopen(“stud.txt","r");
for (i=1;i<=10;i++){
printf(" %d ",ftell(fp));
fseek(fp , 0L , seek_set );
if (i == 5)
rewind(fp);
}
fclose(fp);
}
74. • C provide to give argument or input through
command line (DOS , Linux)
• Here we Pass arguments through commands.
• Till now no argument passed through main()
but in command line argument need some
argument through main() function.
• main() function accepts two arguments
– First argument integer value specifies the number
of command line arguments
– Second argument – list of all of the command line
arguments.
Command line arguments
75. Declarion
list of arguments
int main( int argc , char *argv[])
number of arguments
argv[0]- points first argument
argv[1]- points next argument
Syntax
76. #include<stdio.h>
int main(int argc, char *argv[])
{
int i;
printf(“number of arguments passed=%d” , argc);
for( i=0 ; i<argc ; i++)
printf( “ n arg=%s ” , argv[i] ) ;
return 0 ;
}
Example Program
77. typedef
The C language contains the typedef
keyword to allow users to provide alternative
names for the primitive (e.g., int) and user-
defined (e.g struct) data types.
78. #include<stdio.h>
#include<conio.h>
void main()
{
struct student
{
int rollno;
char name[10];
};
typedef struct student s;
s a;
printf("Enter the student rollno and name ");
scanf("%d%s", &a.rollno, &a.name);
printf("The student rollno and name are n");
printf("%dn%s", a.rollno, a.name);
}