
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Topics covered are about file handling operations in C
Typology: Study notes
1 / 32
This page cannot be seen from the preview
Don't miss anything!

























Why use structure? In C, there are cases where we need to store multiple attributes of an entity. It is not necessary that an entity has all the information of one type only. It can have different attributes of different data types. For example, an entity Student may have its name (string), roll number (int), marks (float). To store such type of information regarding an entity student, we have the following approaches:
Here, struct is the keyword; employee is the name of the structure; id, name, and salary are the members or fields of the structure. Let's understand it by the diagram given below:
Declaring structure variable We can declare a variable for the structure so that we can access the member of the structure easily. There are two ways to declare structure variable: By struct keyword within main() function By declaring a variable at the time of defining the structure. 1st way: Let's see the example to declare the structure variable by struct keyword. It should be declared within the main function. struct employee { int id; char name[50]; float salary; }; Now write given code inside the main() function. struct employee e1, e2; The variables e1 and e2 can be used to access the values stored in the structure. Here, e1 and e2 can be treated in the same way as the objects in C++ and Java.
C Structure example Let's see a simple example of structure in C language. #include<stdio.h> #include <string.h> struct employee { int id; char name[50]; }e1; //declaring e1 variable for structure int main( ) { //store first employee information e1.id=101; strcpy(e1.name, “David");//copying string into char array //printing first employee information printf( "employee 1 id : %d\n", e1.id); printf( "employee 1 name : %s\n", e1.name); return 0; } Output: employee 1 id : 101 employee 1 name : David
C Union Like structure, Union in c language is a user-defined data type that is used to store the different type of elements. At once, only one member of the union can occupy the memory. In other words, we can say that the size of the union in any instance is equal to the size of its largest element. Advantage of union over structure It occupies less memory because it occupies the size of the largest member only.
C Union example Let's see a simple example of union in C language. #include <stdio.h> #include <string.h> union employee { int id; char name[50]; }e1; //declaring e1 variable for union int main( ) { //store first employee information e1.id=101; strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array //printing first employee information printf( "employee 1 id : %d\n", e1.id); printf( "employee 1 name : %s\n", e1.name); return 0; } Output: employee 1 id : 1869508435 employee 1 name : Sonoo Jaiswal
Comparison of Structure with Union
C Pointers The pointer in C language is a variable which stores the address of another variable. This variable can be of type int, char, array, function, or any other pointer. The size of the pointer depends on the architecture. However, in 32-bit architecture the size of a pointer is 2 byte. Consider the following example to define a pointer which stores the address of an integer. int n = 10; int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of type integer. Declaring a pointer The pointer in c language can be declared using * (asterisk symbol). It is also known as indirection pointer used to dereference a pointer. int *a;//pointer to int char *c;//pointer to char
Pointer Example An example of using pointers to print the address and value is given below. As you can see in the above figure, pointer variable stores the address of number variable, i.e., fff4. The value of number variable is 50. But the address of pointer variable p is aaa3. By the help of * (indirection operator), we can print the value of pointer variable p.
Advantage of pointer
Pointer to array int arr[10]; int *p[10]=&arr; // Variable p of type pointer is pointing to the address of an integer array arr. #include <stdio.h> int main( ) { int *p,i; int val[7] = { 11, 22, 33, 44, 55, 66, 77 } ; p = &val[0]; for ( i = 0 ; i<7 ; i++ ) { printf("val[%d]: value is %d and address is %x\n", i, *p, p); p++; } return 0; } Output: val[0]: value is 11 and address is 60feec val[1]: value is 22 and address is 60fef val[2]: value is 33 and address is 60fef val[3]: value is 44 and address is 60fef val[4]: value is 55 and address is 60fefc val[5]: value is 66 and address is 60ff val[6]: value is 77 and address is 60ff
Different operations that can be performed on a file are: 1.Creation of a new file (fopen with attributes as “a” or “a+” or “w” or “w++”) 2.Opening an existing file (fopen) 3.Reading from file (fscanf or fgetc) 4.Writing to a file (fprintf or fputs) 5.Moving to a specific location in a file (fseek, rewind) 6.Closing a file (fclose)