SlideShare a Scribd company logo
1 of 78
Unit V
Structure, Union and Files
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.
Int a=12 ;
Int a[10]={112,45};
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
There are three part of structure
1. Defining a structure
2. Declaring structure variable
3. Accessing object of structure type
Defining a structure
Syntax:
struct structure_name
{
structure element 1;
structure element 2;
….
….
….
structure element n;
} ;
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 ;
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;
struct ice_cream
{
char brand[10];
int price;
char flavor[10];
};
Struct ice_cream gc, b;
gc.brand
Gc.price
Gc.flavor
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 ;
Example:
struct fruits
{
char fruitname[20];
int price ;
int quantity;
};
struct fruits f1;
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.
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
Initialization of structure
Compile time:
struct book
{
char book_name;
int pages;
float price;
};
struct book b={“cp” , 500 , 250.75 };
Run time :
struct book
{
char book_name;
int pages;
float price;
};
struct book b ;
scanf(“%s %d %f”, &b.book_name, &b.pages ,
&b.price );
printf(“%s %d %f”, b.book_name, b.pages,
b.price);
#include<stdio.h>
#include<conio.h>
Void main()
{
struct icecream
{
char brandname[];
char flavor[];
float price;
} ;
struct icecream i={ “amul” , ”chaco” , 25.50 };
clrscr();
printf(“%s %s %f ”, i.brandname , i.flavor , i.price);
getch();
}
o/p: amul chaco 25.50
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
Example:
#include<stdio.h>
#include<conio.h>
Void main()
{
struct student
{
char name[30];
int age;
float percentage;
} ;
struct student s[5];
int i;
clrscr();
printf(“enter the student
details”);
for(i=0 ; i<5 ; i++)
{
scanf(“%s %d %f”, &s[i].name ,
&s[i].age , &s[i].percentage);
}
for(i=0 ; i<5 ; i++)
{
printf(“%s %d %f”, s[i].name ,
s[i].age , s[i].percentage);
}
getch();
}
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 ;
Example:
struct book
{
char name[20];
int pages;
float price;
};
struct book *b ;
Accessing pointer’s in a structure:
b - - > name
b - - >pages
b - - >price
STRUCTURE AND POINTER:
Example:
#include<stdio.h>
#include<conio.h>
Void main()
{
struct book
{
char name;
int pages;
float price;
};
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
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.
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)
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();
}
(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)
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();
}
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 ,
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 ;
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;
};
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 );
}
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.
Syntax:
union union_name
{
union memeber1;
union memeber2;
………
………
union member n ;
};
union union_name union_variable;
Example:
union it_company
{
float profits; // In union memory space
allocated for this element
only
char company_name[20];
int projects_count
};
union it_company ic;
Storage Classes
1. auto
2. static
3. extern
4. register
Syntax:
storage class type data type variable name;
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.
#include<stdio.h>
void main()
{
auto int a;
Printf(“enter the value of a ”);
Scanf( “%d” , &a);
printf( “%d” , a);
}
Output:
Enter the value of a 10
a= 10
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.
Example:
# include<stdio.h>
void main()
{
int i;
register int a=10;
for(i=0;i<5;i++)
a++;
printf(“%d”,a);
} // 11 12 13 14 15
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.
Example:
# include<stdio.h>
Static int b=20;
void main()
{
static int a=200;
printf(“ a=%d ” , a);
printf(“ b=%d ” , b);
}
Output:
a=200
b=20
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
Example:
# include<stdio.h>
extern int b=20;
void main()
{
int a=200;
printf(“Internal variable value=%d” , a);
printf(“external variable value=%d” ,b);
}
Output:
Internal variable value=200
External variable value= 20
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
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>
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
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
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
#include<stdio.h>
#include<conio.h>
#define c (3*3*3)
void main()
{
printf(“The result is %d”, c );
}
Nested macro:
The macro defined within another macro
called nested macros.
Example:
#define A 5
#define B A+5
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.
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
Miscellaneous directives
#progma startup and #progma 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( ) ;
#progma startup fun1
#progma exit fun2
void main( )
{
printf ( “inside main" ) ;
}
void fun1( )
{
printf ( "Inside fun1" ) ;
}
void fun2( )
{
printf ( "Inside fun2" ) ;
}
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.
• 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
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
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 )
• 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
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”);
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);
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);
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);
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
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
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);
for(i=0 ; i<n ;i++)
{
printf(“Enter the students rollno ,name , totalmark”);
scanf(“ %d %s %d ” , &rollno , &name , &totalmark );
fprintf( fptr , “ %d %s %d” , rollno , name , totalmark);
}
fclose(fptr);
fptr=fopen(“student.txt” , “r”);
for(i=0 ; i<n ;i++)
{
printf(“The student detail of %d” , i+1);
fscanf( fptr , “ %d %s %d ” , &rollno , &name , &totalmark );
printf( “ %d %s %d” , rollno , name , totalmark);
}
getch();
fclose(fptr);
}
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
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)
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 );
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);
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 );
• 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);
}
• 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
Declarion
list of arguments
int main( int argc , char *argv[])
number of arguments
argv[0]- points first argument
argv[1]- points next argument
Syntax
#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
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.
#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);
}

More Related Content

Similar to U5 SPC.pptx

Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structuresKrishna Nanda
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing conceptskavitham66441
 
slideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdfslideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdfHimanshuKansal22
 
Structures and Pointers
Structures and PointersStructures and Pointers
Structures and PointersPrabu U
 
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docxCS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docxfaithxdunce63732
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationRabin BK
 
Introduction to structures in c lang.ppt
Introduction to structures in c lang.pptIntroduction to structures in c lang.ppt
Introduction to structures in c lang.pptshivani366010
 
VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4YOGESH SINGH
 

Similar to U5 SPC.pptx (20)

Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
Unit4 C
Unit4 C Unit4 C
Unit4 C
 
SRAVANByCPP
SRAVANByCPPSRAVANByCPP
SRAVANByCPP
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Unit-V.pptx
Unit-V.pptxUnit-V.pptx
Unit-V.pptx
 
Structures
StructuresStructures
Structures
 
C structure and union
C structure and unionC structure and union
C structure and union
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
 
slideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdfslideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdf
 
Structures and Pointers
Structures and PointersStructures and Pointers
Structures and Pointers
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docxCS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
 
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
structures.ppt
structures.pptstructures.ppt
structures.ppt
 
Introduction to structures in c lang.ppt
Introduction to structures in c lang.pptIntroduction to structures in c lang.ppt
Introduction to structures in c lang.ppt
 
C++ language
C++ languageC++ language
C++ language
 
VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4
 
Bt0065
Bt0065Bt0065
Bt0065
 

More from Kongunadu College of Engineering and Technology (19)

Unit V - ppt.pptx
Unit V - ppt.pptxUnit V - ppt.pptx
Unit V - ppt.pptx
 
C++ UNIT4.pptx
C++ UNIT4.pptxC++ UNIT4.pptx
C++ UNIT4.pptx
 
c++ Unit I.pptx
c++ Unit I.pptxc++ Unit I.pptx
c++ Unit I.pptx
 
c++ Unit III - PPT.pptx
c++ Unit III - PPT.pptxc++ Unit III - PPT.pptx
c++ Unit III - PPT.pptx
 
c++ UNIT II.pptx
c++ UNIT II.pptxc++ UNIT II.pptx
c++ UNIT II.pptx
 
UNIT 1.pptx
UNIT 1.pptxUNIT 1.pptx
UNIT 1.pptx
 
UNIT 4.pptx
UNIT 4.pptxUNIT 4.pptx
UNIT 4.pptx
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
 
UNIT 3.pptx
UNIT 3.pptxUNIT 3.pptx
UNIT 3.pptx
 
Unit - 1.ppt
Unit - 1.pptUnit - 1.ppt
Unit - 1.ppt
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
 
Unit - 2.ppt
Unit - 2.pptUnit - 2.ppt
Unit - 2.ppt
 
Unit - 3.pptx
Unit - 3.pptxUnit - 3.pptx
Unit - 3.pptx
 
Unit - 4.ppt
Unit - 4.pptUnit - 4.ppt
Unit - 4.ppt
 
U1.ppt
U1.pptU1.ppt
U1.ppt
 
U2.ppt
U2.pptU2.ppt
U2.ppt
 
U4.ppt
U4.pptU4.ppt
U4.ppt
 
U3.pptx
U3.pptxU3.pptx
U3.pptx
 

Recently uploaded

Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 

Recently uploaded (20)

Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 

U5 SPC.pptx

  • 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.
  • 3. Int a=12 ; Int a[10]={112,45};
  • 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
  • 6. Defining a structure Syntax: struct structure_name { structure element 1; structure element 2; …. …. …. structure element n; } ;
  • 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;
  • 9. struct ice_cream { char brand[10]; int price; char flavor[10]; }; Struct ice_cream gc, b; gc.brand Gc.price Gc.flavor
  • 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 ;
  • 11. Example: struct fruits { char fruitname[20]; int price ; int quantity; }; struct fruits f1;
  • 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
  • 14. Initialization of structure Compile time: struct book { char book_name; int pages; float price; }; struct book b={“cp” , 500 , 250.75 };
  • 15. Run time : struct book { char book_name; int pages; float price; }; struct book b ; scanf(“%s %d %f”, &b.book_name, &b.pages , &b.price ); printf(“%s %d %f”, b.book_name, b.pages, b.price);
  • 16. #include<stdio.h> #include<conio.h> Void main() { struct icecream { char brandname[]; char flavor[]; float price; } ; struct icecream i={ “amul” , ”chaco” , 25.50 }; clrscr(); printf(“%s %s %f ”, i.brandname , i.flavor , i.price); getch(); } o/p: amul chaco 25.50
  • 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
  • 18. Example: #include<stdio.h> #include<conio.h> Void main() { struct student { char name[30]; int age; float percentage; } ; struct student s[5]; int i; clrscr(); printf(“enter the student details”); for(i=0 ; i<5 ; i++) { scanf(“%s %d %f”, &s[i].name , &s[i].age , &s[i].percentage); } for(i=0 ; i<5 ; i++) { printf(“%s %d %f”, s[i].name , s[i].age , s[i].percentage); } getch(); }
  • 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 ;
  • 20. Example: struct book { char name[20]; int pages; float price; }; struct book *b ; Accessing pointer’s in a structure: b - - > name b - - >pages b - - >price
  • 21. STRUCTURE AND POINTER: Example: #include<stdio.h> #include<conio.h> Void main() { struct book { char name; int pages; float price; };
  • 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.
  • 33. Syntax: union union_name { union memeber1; union memeber2; ……… ……… union member n ; }; union union_name union_variable;
  • 34. Example: union it_company { float profits; // In union memory space allocated for this element only char company_name[20]; int projects_count }; union it_company ic;
  • 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.
  • 37. #include<stdio.h> void main() { auto int a; Printf(“enter the value of a ”); Scanf( “%d” , &a); printf( “%d” , a); } Output: Enter the value of a 10 a= 10
  • 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.
  • 39. Example: # include<stdio.h> void main() { int i; register int a=10; for(i=0;i<5;i++) a++; printf(“%d”,a); } // 11 12 13 14 15
  • 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.
  • 41. Example: # include<stdio.h> Static int b=20; void main() { static int a=200; printf(“ a=%d ” , a); printf(“ b=%d ” , b); } Output: a=200 b=20
  • 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
  • 43. Example: # include<stdio.h> extern int b=20; void main() { int a=200; printf(“Internal variable value=%d” , a); printf(“external variable value=%d” ,b); } Output: Internal variable value=200 External variable value= 20
  • 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
  • 50. #include<stdio.h> #include<conio.h> #define c (3*3*3) void main() { printf(“The result is %d”, c ); }
  • 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 #progma startup and #progma 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( ) ; #progma startup fun1 #progma 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);
  • 67. for(i=0 ; i<n ;i++) { printf(“Enter the students rollno ,name , totalmark”); scanf(“ %d %s %d ” , &rollno , &name , &totalmark ); fprintf( fptr , “ %d %s %d” , rollno , name , totalmark); } fclose(fptr); fptr=fopen(“student.txt” , “r”); for(i=0 ; i<n ;i++) { printf(“The student detail of %d” , i+1); fscanf( fptr , “ %d %s %d ” , &rollno , &name , &totalmark ); printf( “ %d %s %d” , rollno , name , totalmark); } getch(); fclose(fptr); }
  • 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); }