structures in C
A structure is a user-defined data type available in C that allows to combining data items of
different kinds. Structures are used to represent a record.
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:
struct [structure name]
{
member definition;
member definition;
...
member definition;
};
#include <stdio.h>
#include <string.h>
struct student
int rollno;
char name[60];
}s1; //declaring s1 variable for structure
void main( )
s1.rollno=1;
strcpy(s1.name, “intellipaat”);//copying string into char array
printf( "Rollno : %d\n", s1.rollno);
printf( "Name : %s\n", s1.name);
// Program to add two distances which is in feet and inches
#include <stdio.h>
struct Distance
{
int feet;
float inch;
} dist1, dist2, sum;
int main()
{
printf("1st distance\n");
printf("Enter feet: ");
scanf("%d", &dist1.feet);
printf("Enter inch: ");
scanf("%f", &dist1.inch);
printf("2nd distance\n");
printf("Enter feet: ");
scanf("%d", &dist2.feet);
printf("Enter inch: ");
scanf("%f", &dist2.inch);
// adding feet
sum.feet = dist1.feet + dist2.feet;
// adding inches
sum.inch = dist1.inch + dist2.inch;
// changing feet if inch is greater than 12
while (sum.inch >= 12)
{
++sum.feet;
sum.inch = sum.inch - 12;
}
printf("Sum of distances = %d\'-%.1f\"", sum.feet, sum.inch);
return 0;
}
array of structures in C:
This program is used to store and access “id, name and percentage” for 3 students. Structure
array is used in this program to store and display records for many students. You can store “n”
number of students record by declaring structure variable as ‘struct student record[n]“, where n
can be 1000 or 5000 etc.
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[30];
float percentage;
};
int main()
{
int i;
struct student record[2];
// 1st student's record
record[0].id=1;
strcpy(record[0].name, "Raju");
record[0].percentage = 86.5;
// 2nd student's record
record[1].id=2;
strcpy(record[1].name, "Surendren");
record[1].percentage = 90.5;
// 3rd student's record
record[2].id=3;
strcpy(record[2].name, "Thiyagu");
record[2].percentage = 81.5;
for(i=0; i<3; i++)
{
printf(" Records of STUDENT : %d \n", i+1);
printf(" Id is: %d \n", record[i].id);
printf(" Name is: %s \n", record[i].name);
printf(" Percentage is: %f\n\n",record[i].percentage);
}
return 0;
}
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 purposes.
Defining a Union: To define a union, you must use the union statement in the same way as you
did while defining a structure. The union statement defines a new data type with more than one
member for your program. The format of the union statement is as follows:
union [union name]
{
member definition;
member definition;
...
member definition;
};
#include <stdio.h>
#include <string.h>
union student
{
int rollno;
char name[60];
}s1; //declaring s1 variable for union
void main( )
{
s1.rollno=1;
strcpy(s1.name, “intellipaat”);//copying string into char array
printf( "Rollno : %d\n", s1.rollno);
printf( "Name : %s\n", s1.name);
Basics of File Handling in C
So far the operations using C program are done on a prompt / terminal which is not stored
anywhere. But in the software industry, most of the programs are written to store the information
fetched from the program. One such way is to store the fetched information in a file. 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 (filePointerrintf or filePointeruts)
5. Moving to a specific location in a file (fseek, rewind)
6. Closing a file (fclose)
Types of Files
When dealing with files, there are two types of files you should know about:
1. Text files
2. Binary files
1. Text files
Text files are the normal .txt files that you can easily create using Notepad or any simple text
editors.
2. Binary files
Binary files are mostly the .bin files in your computer. Instead of storing data in plain text, they
store it in the binary form (0's and 1's).
Opening a File or Creating a File
The fopen() function is used to create a new file or to open an existing file.
General Syntax:
*fp = FILE *fopen(const char *filename, const char *mode);
Closing a File
The fclose() function is used to close an already opened file.
General Syntax :
int fclose( FILE *fp);
READING AND WRITING CHARACTERS IN A FILE
#include<stdio.h>
int main()
{
FILE *fp;
char ch;
fp = fopen("one.txt", "w");
printf("Enter data...");
while( (ch = getchar()) != EOF) {
putc(ch, fp);
}
fclose(fp);
fp = fopen("one.txt", "r");
while( (ch = getc(fp)! = EOF)
printf("%c",ch);
// closing the file pointer
fclose(fp);
return 0;
}
READING AND WRITING TEXT IN A FILE USING FSCANF AND FPRINTF
Example of fprintf() function
#include<stdio.h>
void main()
{
FILE *fp;
int roll;
char name[25];
float marks;
char ch;
fp = fopen("file.txt","w"); //Statement 1
if(fp == NULL)
{
printf("\nCan't open file or file doesn't exist.");
exit(0);
}
do
{
printf("\nEnter Roll : ");
scanf("%d",&roll);
printf("\nEnter Name : ");
scanf("%s",name);
printf("\nEnter Marks : ");
scanf("%f",&marks);
fprintf(fp,"%d%s%f",roll,name,marks);
printf("\nDo you want to add another data (y/n) : ");
ch = getche();
}while(ch=='y' || ch=='Y');
printf("\nData written successfully...");
fclose(fp);
}
fscanf() function
#include<stdio.h>
void main()
{
FILE *fp;
char ch;
fp = fopen("file.txt","r"); //Statement 1
if(fp == NULL)
{
printf("\nCan't open file or file doesn't exist.");
exit(0);
}
printf("\nData in file...\n");
while((fscanf(fp,"%d%s%f",&roll,name,&marks))!=EOF)
printf("\n%d\t%s\t%f",roll,name,marks);
fclose(fp);
}