Advanced Study Material for Computer Science: Programming in C
Review targeted academic resources with the Problem Solving Through C Unit 08 Structures and Unions Notes Set 01. Built according to official standards for the 2026-27 term, these downloadable Computer Science study materials support effective daily practice and deeper conceptual understanding for Programming in C.
Practice Computer Science Resources: Programming in C
Access the complete useful resource PDF for Computer Science below. Regular practice with these targeted academic notes builds familiarity with complex topics and helps secure higher marks in final school evaluations.
This page covers structures and unions in C: defining and declaring structures, initializing and accessing members, nested structures, passing structures to functions by value and by reference, arrays of structures, and how unions share memory and differ from structures, with complete example programs.
Introduction: Data Types
- C programming language which has the ability to divide the data into different types.
- The type of a variable determines the kind of values it may take on.
- The various data types are
1. Simple Data type: Integer, Real, Void, Char.
2. Structured Data type: Array, Strings.
3. User Defined Data type: Enum, Structures, Unions.
Structure Data Type
- A structure is a user defined data type that groups logically related data items of different data types into a single unit.
- All the elements of a structure are stored at contiguous memory locations.
- A variable of structure type can store multiple data items of different data types under the one name.
- As the data of employee in company that is name, Employee ID, salary, address, phone number is stored in structure data type employee_detail.
Defining Structure
- A structure has to be defined before it can be used.
- The syntax of defining a structure is
struct <struct_name>
{
<data_type> <variable_name>;
<data_type> <variable_name>;
........
<data_type> <variable_name>;
};Example of Structure
The structure of Employee is declared as
struct employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int dept_no;
int age;
};Application of structure
- Structure is used in database management to maintain data about books in library, items in store, employees in an organization, financial accounting transaction in company.
- Structure is used in C programming for following purposes
1. Clearing screen.
2. Adjusting cursor position.
3. Drawing any graphics shape on the screen.
4. Receiving a key from the keyboard.
5. Finding out the list of equipment attached to the computer.
6. Changing the size of the cursor.
7. Formatting a floppy.
8. Hiding a file from the directory.
9. Displaying the directory of a disk.
10. Checking the memory size.
11. Sending the output to printer.
12. Interacting with the mouse.
Structure Variables
- A structure is a collection of one or more variables grouped under a single name for easy manipulation.
- The variables in a structure, unlike those in an array, can be of different variable types.
- A structure can contain any of C's data types, including arrays and other structures.
- Each variable within a structure is called a member of the structure.
Declaring a Structure Variable
- A structure has to be declared, after the body of structure has been defined.
- The syntax of declaring a structure is
struct <struct_name> <variable_name>;
The example to declare the variable for defined structure "employee".
struct employee e1;
Here e1 variable contains 6 members that are defined in structure.
Structure Variables - Program
#include <stdio.h>
struct student {
char firstName[20];
char lastName[20];
char SSN[10];
float gpa;
};
main()
{
struct student student_a;
strcpy(student_a.firstName, "Ram");
strcpy(student_a.lastName, "Kumar");
strcpy(student_a.SSN, "2333234" );
student_a.gpa = 2009.20;
printf( "First Name: %s\n", student_a.firstName );
printf( "Last Name: %s\n", student_a.lastName );
printf( "SNN : %s\n", student_a.SSN );
printf( "GPA : %f\n", student_a.gpa );
}Initialization - Initializing Structure Members
- The members of individual structure variable are initialized one by one or in a single statement.
- The example to initialize a structure variable is
1.
struct employee e1 = {1, "Hemant", 12000,
"3 vikas colony new delhi", 10, 35};Or
2.
e1.emp_id = 1; e1.dept_no = 10; strcpy(e1.name, "Hemant"); e1.age = 35; e1.salary = 12000; strcpy(e1.address, "3 vikas colony new delhi");
Accessing Structure Members
- The structure members cannot be directly accessed in the expression.
- They are accessed by using the name of structure variable followed by a dot and then the name of member variable.
- The method used to access the structure variables are e1.emp_id, e1.name, e1.salary, e1.address, e1.dept_no, e1.age.
- The data within the structure is stored and printed by this method using scanf and printf statement in c program.
- A structure named coord that contains both the x and y values of a screen location is as follows:
struct coord
{
int x;
int y;
};- The struct keyword, which identifies the beginning of a structure definition, must be followed immediately by the structure name, or tag (which follows the same rules as other C variable names).
- Within the braces following the structure name is a list of the structure's member variables.
- The variable type and name must be provided for each member.
- The above statements define a structure type named coord that contains two integer variables, x and y.
- They do not, however, actually create any instances of the structure coord.
- In other words, they don't declare (set aside storage for) any structures.
- There are two ways to declare structures.
- One is to follow the structure definition with a list of one or more variable names.
struct coord
{
int x;
int y;
} first, second;- These statements define the structure type coord and declare two structures, first and second, of type coord.
- First and second are each instances of type coord; first contains two integer members named x and y, and so does second.
- This method of declaring structures combines the declaration with the definition.
- The second method is to declare structure variables at a different location in the source code from the definition.
- The statements also declare two instances of type coord:
struct coord first, second;
The following program shows the structure for recording students.
struct student{
char name[50];
int roll;
float marks;
};
int main(){
struct student s[10];
int i;
printf("Enter information of students:\n");
for(i=0;i<10;++i)
{
s[i].roll=i+1;
printf("\nFor roll number %d\n",s[i].roll);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter marks: ");
scanf("%f",&s[i].marks);
printf("\n");
}
printf("Displaying information of students:\n\n");
for(i=0;i<10;++i)
{
printf("\nInformation for roll number %d:\n",i+1);
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
}
return 0;
}Structure Assignment
- The value of one structure variable is assigned to another variable of same type using assignment statement.
- If the e1 and e2 are structure variables of type employee then the statement e1 = e2; assign value of structure variable e2 to e1.
- The value of each member of e2 is assigned to corresponding members of e1.
- Individual structure members can be used like other variables of the same type.
- Structure members are accessed using the structure member operator (.), also called the dot operator, between the structure name and the member name.
- Thus, to have the structure named first refer to a screen location that has coordinates x=50, y=100, the code is as follows:
first.x = 50; first.y = 100;
To display the screen locations stored in the structure second, the code is as follows:
printf("%d,%d", second.x, second.y);What is the advantage of using structures rather than individual variables.
- One major advantage is that a programmer can copy information between structures of the same type with a simple equation statement.
- Continuing with the preceding example, the statement:
first = second;
is equivalent to this statement as follows,
first.x = second.x; first.y = second.y;
Program to Implement the Structure
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
} record;
int main()
{
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
return 0;
}Structures within Structures
- C language allows a variable of structure type to be a member of some other structure type.
- The syntax to define the structure within structure is
struct <struct_name>
{
<data_type> <variable_name>;
struct <struct_name>
{ <data_type> <variable_name>;
........} <struct_variable>;
<data_type> <variable_name>;
};Example of Structure within Structure
#include <stdio.h>
struct Employee
{
char ename[20];
int ssn;
float salary;
struct date
{
int date;
int month;
int year;
}doj;
}emp = {"Pritesh",1000,1000.50,{22,6,1990}};
int main(int argc, char *argv[])
{
printf("\nEmployee Name : %s",emp.ename);
printf("\nEmployee SSN : %d",emp.ssn);
printf("\nEmployee Salary : %f",emp.salary);
printf("\nEmployee DOJ : %d/%d/%d", \
emp.doj.date,emp.doj.month,emp.doj.year);
return 0;
}Accessing Structures within Structures
- The data member of structure within structure is accessed by using two period (.) symbols.
- The syntax to access the structure within structure is
struct_var.nested_struct_var.struct_member;
For Example:-
e1.doj.day; e1.doj.month; e1.doj.year;
Nested Structures
- In the example a defined structure has been dealt with, that can hold the two coordinates required for a single point.
- One needs two such structures to define a rectangle.
- One can define a structure as follows (assuming, of course, that one has already defined the type coord structure):
struct rectangle
{
struct coord topleft;
struct coord bottomright;
};- This statement defines a structure of type rectangle that contains two structures of type coord.
- These two type coord structures are named topleft and bottomright.
- The preceding statement defines only the type rectangle structure.
- To declare a structure, one must include a statement such as:
struct rectangle mybox;
One could have combined the definition and declaration, as he did before for the type coord:
struct rectangle
{
struct coord topleft;
struct coord bottomright;
} mybox;- To access the actual data locations (the type int members), one must apply the member operator (.) twice.
- Thus, the expression:
mybox.topleft.x
Refers to the x member of the topleft member of the type rectangle structure named mybox.
To define a rectangle with coordinates (0, 10), (100, 200), one can write.
mybox.topleft.x = 0; mybox.topleft.y = 10; mybox.bottomright.x = 100; mybox.bottomright.y = 200;
Structures and Functions
- In C, structure can be passed to functions by two methods:
1. Passing by value (passing actual value as argument).
2. Passing by reference (passing address of an argument).
Passing Structure by Value
- A structure variable can be passed to the function as an argument as normal variable.
- If structure is passed by value, change made in structure variable in function definition does not reflect in original structure variable in calling function.
- Write a C program to create a structure student, containing name and roll.
- Ask user the name and roll of a student in main function. Pass this structure to a function and display the information in that function.
#include <stdio.h>
struct student
{
char name[50];
int roll;
};
void Display(struct student stu);
/* function prototype should be below to the structure
declaration otherwise compiler shows error */
int main(){
struct student s1;
printf("Enter student's name: ");
scanf("%s",&s1.name);
printf("Enter roll number:");
scanf("%d",&s1.roll);
Display(s1); // passing structure variable s1 as argument
return 0;
}
void Display(struct student stu){
printf("Output\nName: %s",stu.name);
printf("\nRoll: %d",stu.roll);
}Output
Enter student's name: Kevin Enter roll number: 149 Output Name: Kevin Roll: 149
Passing Structure by Reference
- The address location of structure variable is passed to function while passing it by reference.
- If structure is passed by reference, change made in structure variable in function definition reflects in original structure variable in the calling function.
- Write a C program to add two distances (feet-inch system) entered by user.
- To solve this program, make a structure. Pass two structure variable (containing distance in feet and inch) to add function by reference and display the result in main function without returning it.
#include <stdio.h>
struct distance
{
int feet;
float inch;
};
void Add(struct distance d1,struct distance d2, struct distance *d3);
int main()
{
struct distance dist1, dist2, dist3;
printf("First distance\n");
printf("Enter feet: ");
scanf("%d",&dist1.feet);
printf("Enter inch: ");
scanf("%f",&dist1.inch);
printf("Second distance\n");
printf("Enter feet: ");
scanf("%d",&dist2.feet);
printf("Enter inch: ");
scanf("%f",&dist2.inch);
Add(dist1, dist2, &dist3);
/*passing structure variables dist1 and dist2
by value whereas passing structure variable dist3 by reference */
printf("\nSum of distances = %d\'-%.1f\"",dist3.feet, dist3.inch);
return 0;
}
void Add(struct distance d1,struct distance d2, struct distance *d3)
{
/* Adding distances d1 and d2 and storing it in d3 */
d3->feet=d1.feet+d2.feet;
d3->inch=d1.inch+d2.inch;
if (d3->inch>=12) { /* if inch is greater or equal to 12,
converting it to feet. */
d3->inch-=12;
++d3->feet;
}
}Output
First distance Enter feet: 12 Enter inch: 6.8 Second distance Enter feet: 5 Enter inch: 7.5 Sum of distances = 18'-2.3"
Array of Structure
- C language allows creating an array of variables of structure.
- The array of structure is used to store the large number of similar records.
- For example to store the record of 100 employees then array of structure is used.
- The method to define and access the array element of array of structure is similar to other array.
- The syntax to define the array of structure is
struct <struct_name> <array_name> [ <value> ];
For Example:
struct employee e1[100];
Structures Containing Arrays
- One can define a structure that contains one or more arrays as members.
- The array can be of any C data type (int, char, and so on).
- For example, consider the declaration below.
struct data
{
int x[4];
char y[10];
};The above statements define a structure of type data that contains a four-element integer array member named x and a 10-element character array member named y.
Structures and Arrays
- One can then declare a structure named record of type data as follows:
struct data record;
One can access individual elements of arrays that are structure members using a combination of the member operator and array subscripts :
record.x[2] = 100; record.y[1] = 'x';
- The character arrays are most frequently used to store strings and the name of an array, without brackets, is a pointer to the array.
- Because this holds true for arrays that are structure members, the expression:
record.y
Is a pointer to the first element of array y[ ] in the structure record.
Therefore, one could print the contents of y[ ] on-screen using the statement:
puts(record.y);
Arrays of Structures
- What one needs is an array of structures of type entry.
- After the structure has been defined, one can declare an array as follows:
struct entry list[1000];
- This statement declares an array named list that contains 1,000 elements.
- Each element is a structure of type entry and is identified by subscript like other array element types.
- Each of these structures has three elements, each of which is an array of type char.
- Structure is used to store the information of One particular object but if we need to store such 100 objects then Array of Structure is used.
- Example
struct Bookinfo
{
char bname[20];
int pages;
int price;
}Book[100];- Here Book structure is used to Store the information of one Book.
- In case if we need to store the Information of 100 books then Array of Structure is used.
- Book[0] stores the Information of 1st Book, Book[1] stores the information of 2nd Book and So on. We can store the information of 100 books.
- Example Program
#include <stdio.h>
struct Bookinfo
{
char bname[20];
int pages;
int price;
}book[3];
int main(int argc, char *argv[])
{
int i;
for(i=0;i<3;i++)
{
printf("\nEnter the Name of Book : ");
gets(book[i].bname);
printf("\nEnter the Number of Pages : ");
scanf("%d",&book[i].pages);
printf("\nEnter the Price of Book : ");
scanf("%d",&book[i].price);
}
printf("\n--------- Book Details ------------ ");
for(i=0;i<3;i++)
{
printf("\nName of Book : %s",book[i].bname);
printf("\nNumber of Pages : %d",book[i].pages);
printf("\nPrice of Book : %d",book[i].price);
}
return 0;
}Output:
Enter the Name of Book : ABC Enter the Number of Pages : 100 Enter the Price of Book : 200 Enter the Name of Book : EFG Enter the Number of Pages : 200 Enter the Price of Book : 300 Enter the Name of Book : HIJ Enter the Number of Pages : 300 Enter the Price of Book : 500 --------- Book Details ------------ Name of Book : ABC Number of Pages : 100 Price of Book : 200 Name of Book : EFG Number of Pages : 200 Price of Book : 300 Name of Book : HIJ Number of Pages : 300 Price of Book : 500
Unions
- Unions are similar to structures.
- A union is declared and used in the same ways that a structure.
- The reason for this is simple; all the members of a union occupy the same area of memory.
- Unions are defined and declared in the same fashion as structures.
- In unions all the members share the space which is according to the space requirement of the largest member.
- A union can be initialized on its declaration.
- Because only one member can be used at a time, only one can be initialized.
- To avoid confusion, only the first member of the union can be initialized.
- The following code shows an instance of the shared union being declared and initialised:
union shared generic_variable = {'@'};- The generic variable union was initialized just as the first member of a structure would be initialized.
- Note: The union can hold only one value at a time.
Defining of Union
- A union has to be defined before it can be used.
- The syntax of defining a union is
union <union_name>
{
<data_type> <variable_name>;
<data_type> <variable_name>;
........
<data_type> <variable_name>;
};To define a simple union of a char variable and an integer variable, one would write the following:
union shared
{
char c;
int i;
};- This union, shared, can be used to create instances of a union that can hold either a character value c or an integer value i.
- This is an OR condition.
- Unlike a structure that would hold both values, the union can hold only one value at a time.
Union Data Type
- A union is a user defined data type like structure.
- The union groups logically related variables into a single unit.
- The union data type allocates the space equal to space needed to hold the largest data member of union.
- The union allows different types of variable to share same space in memory.
- There is no other difference between structure and union than internal difference.
- The method to declare, use and access the union is same as structure.
union Data
{
int i;
float f;
char str[20];
} data;Accessing Union Members
- Individual union members can be used in the same way that structure members can be used--by using the member operator (.).
- However, there is an important difference in accessing union members.
- Only one union member should be accessed at a time.
- Because a union stores its members on top of each other, it's important to access only one member at a time.
- The following is an example for this:
union shared
{
char c;
int i;
};
union shared u;
u.c = 'a';
u.i = 1;Difference between Structures & Union
- The memory occupied by structure variable is the sum of sizes of all the members but memory occupied by union variable is equal to space hold by the largest data member of a union.
- In the structure all the members can be accessed at any point of time but in union only one of union member can be accessed at any given time.
#include <stdio.h>
union job { //defining a union
char name[32];
float salary;
int worker_no;
}u;
struct job1 {
char name[32];
float salary;
int worker_no;
}s;
int main(){
printf("size of union = %d",sizeof(u));
printf("\nsize of structure = %d", sizeof(s));
return 0;
}Output
size of union = 32 size of structure = 40
Free study material for Computer Science
Free CBSE Study Guides: Computer Science Programming in C
Quick Revision Material for Programming in C
Review targeted study resources for Programming in C tailored for learners. Utilizing these structured notes and quick-revision tools ensures complete alignment with current CBSE evaluation standards.
Expert Notes and Solved Exam Questions
Designed around the official curriculum, these study guides guarantee standard compliance. Reviewing step-by-step solutions clarifies complex sub-topics within Programming in C and demystifies standard marking schemes for Computer Science evaluations.
Additional Study Resources for
Maximize your grade potential by utilizing Computer Science practice papers in tandem with these structured notes. All printable assignments and interactive mock tests on our platform are updated for the 2026 session and available free of charge.
FAQs
The latest 2026-27 advanced study resources for Computer Science are available for free on StudiesToday.com which includes NCERT Exemplars, high-order thinking skills (HOTS) questions, and deep-dive concept summaries.
Our exhaustive Computer Science package includes chapter wise revision notes, solved practice sheets, important formulas and Concept Maps to help in better understanding of all topics.
Yes. For , our resources have been developed to help you get better marks in school exams and also build fundamental strength needed for entrance tests including Competency Based learning.
in , students should use Active Recall method, read the concept summary, then solve the Important Questions section without looking at the answers and then check your answers.
All Computer Science study materials are provided in mobile-friendly PDF. You can download and save them on your device.
Yes, our team has ensured that all Computer Science materials for are strictly aligned with the National Education Policy (NEP) 2020 and the latest 2026-27 syllabus.