Pps Unit 05 Notes
Pps Unit 05 Notes
1. Structure
Structure in c is a user-defined data type that enables us to store the collection of
different data types. Each element of a structure is called a member. The
struct keyword is used to define the structure.
The syntax to define the structure:
struct structure_name
{
data_type member1;
data_type member2;
.
data_type memeberN;
};
The following image shows the memory allocation of the structure employee
that is defined in the above example.
/* Book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Ashok Kamthane");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495;
/* Book 2 specification */
strcpy( Book2.title, "Programming C");
strcpy( Book2.author, "Balaguru Swami");
Output:
Book 1 title : C Programming
Book 1 author : Ashok Kamthane
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495
2. Write a C program to create Student Details using structure. And store the
Information of one student and Display it.
#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
} s;
int main()
{
printf("Enter information:\n");
printf("Enter name: ");
scanf("%s", &s.name);
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter marks: ");
scanf("%f", &s.marks);
printf("\nDisplaying Information:\n");
printf("Name: ");
puts(s.name);
printf("Roll number: %d\n",s.roll);
printf("Marks: %.1f\n", s.marks);
return 0;
}
Output:
Enter information:
Enter name: Amit
Enter roll number: 2
Enter marks: 56
Displaying Information:
Name: Amit
Roll number: 2
Marks: 56
2. union
A union is a special data type available in C that allows storing different data types in
the same memory location. You can define a union with many members, but only one
member can contain a value at any given time. Unions provide an efficient way of
using the same memory location for multiple-purpose.
The syntax to define union in c.
union union_name
{
data_type member1;
data_type member2;
.
.
data_type memeberN;
};
Example:
union Data
{
int i;
float f;
char str[20];
} d1;
The memory occupied by a union will be large enough to hold the largest member of
the union. Data type will occupy 20 bytes of memory space because this is the
maximum space which can be occupied by a character string.
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
printf( "Memory size occupied by data : %d
%d\n", sizeof(data));
return 0;
}
Output:
Memory size occupied by data: 20
Difference
Output
The value of week: 10 11 12 13 14 15 16
Arrays of Structures
An array of structures is an array where each element is a structure. This allows you to
store multiple records of different data types in a single array.
Example:
#include <stdio.h>
struct Student
{
int id;
char name[50];
float grade;
};
int main()
{
struct Student students[3] = {{1, "Alice", 85.5}, {2, "Bob", 92.0},{3, "Charlie", 78.4}
};
for (int i = 0; i < 3; i++)
{
printf("Student ID: %d\n", students[i].id);
printf("Name: %s\n", students[i].name);
printf("Grade: %.2f\n\n", students[i].grade);
}
return 0;
}
Pointers to Structures
Pointers to structures allow you to access structure members using pointers. This is
particularly useful for dynamic memory allocation or when passing structures to
functions.
Example:
#include <stdio.h>
struct Student
{
int id;
char name[50];
float grade;
};
int main()
{
struct Student student1 = {1, "Alice", 88.5};
struct Student *ptr = &student1;
printf("Student ID: %d\n", ptr->id);
printf("Name: %s\n", ptr->name);
printf("Grade: %.2f\n", ptr->grade);
return 0;
}
Self-Referential Structures
A self-referential structure is a structure that contains a pointer to an instance of the
same structure. This is commonly used in the creation of linked lists.
Example:
#include <stdio.h>
struct Node
{
int data;
struct Node *next;
};
int main()
{
struct Node node1 = {10, NULL};
struct Node node2 = {20, NULL};
node1.next = &node2;
printf("Node 1 data: %d\n", node1.data);
printf("Node 2 data: %d\n", node1.next->data);
return 0;
}
Answer:
Opening a file:
The fopen() function is used to create a new file or to open an existing file.
Syntax:
*fp = FILE *fopen(const char *filename, const char *mode);
Here, *fp is the FILE pointer (FILE *fp), which will hold the reference to the opened (or
created) file.
Filename is the name of the file to be opened and mode specifies the purpose of opening
the file. Mode can be of following types,
mode description
r Opens a text file in reading mode.
w Opens or create a text file in writing mode.
a Opens a text file in append mode.
r+ Opens a text file in both reading and writing mode.
w+ Opens a text file in both reading and writing mode.
a+ Opens a text file in both reading and writing mode.
rb Opens a binary file in reading mode.
wb Opens or create a binary file in writing mode.
ab Opens a binary file in append mode.
rb+ Opens a binary file in both reading and writing mode.
wb+ Opens a binary file in both reading and writing mode.
ab+ Opens a binary file in both reading and writing mode.
STEP 3: Now a new file will be created with a name "xyz.c" and all the content would
copy from abc.txt to xyz.txt.
Answer:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp1 = fopen("file1.txt", "r");
FILE *fp2 = fopen("file2.txt", "r");
FILE *fp3 = fopen("file3.txt", "w");
char c;
if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
{
puts("Could not open files");
exit(0);
}
while ((c = fgetc(fp1)) != EOF)
fputc(c, fp3);
while ((c = fgetc(fp2)) != EOF)
fputc(c, fp3);
printf("Merged file1.txt and file2.txt into file3.txt");
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
STEP 3: Now a new file will be created with a name “file3.c" and all the content of file1
and file2 would concatenate in file3.txt.
Answer:
File Handling in C
File handling in C enables us to create, update, read, and delete the files stored on the
local file system through our C program. The following operations can be performed on a
file.
1. Creation of the new file
2. Opening an existing file
3. Reading from the file
4. Writing to the file
5. Deleting the file
# include <stdio.h>
# include <string.h>
int main( )
{
FILE *fp ;
char data[50];
printf( "Opening the file test.c in write mode" ) ;
fp = fopen("test.c", "w") ;
if ( fp == NULL )
{
printf( "Could not open file test.c" ) ;
return 1;
}
printf( "\n Enter some text from keyboard to write in the file test.c" ) ;
while ( strlen ( gets( data ) ) > 0 )
{
fputs(data, fp) ;
fputs("\n", fp) ;
}
printf("Closing the file test.c") ;
fclose(fp) ;
return 0;
}