Lesson 8. Structures & Unions PDF

Document Details

GratefulSwan3091

Uploaded by GratefulSwan3091

University of Papua New Guinea

Tags

programming structures unions C programming

Summary

This document provides an introduction to structures and unions in programming, specifically C programming. It details how to use structures to group related data and unions to store different data types in the same memory location.

Full Transcript

Structures Structures provide a way of storing many different values in variables of potentially different types under the same name. This makes it a more modular program, which is easier to modify because its design makes things more compact. Structures are generally useful whenever a lot of data n...

Structures Structures provide a way of storing many different values in variables of potentially different types under the same name. This makes it a more modular program, which is easier to modify because its design makes things more compact. Structures are generally useful whenever a lot of data needs to be grouped together. struct employee { int id_number; int age; float salary; }; void main() { struct employee e; e.age = 22; e.id_number = 1; e.salary = 12000.21; } ____________________________________________________ //Example 1 #include #include struct student { char name; int roll; float marks; }; void main() { struct student s; clrscr(); printf("Enter information of student:\n\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: %s\n",s.name); printf("Roll: %d\n",s.roll); printf("Marks: %.2f\n",s.marks); getch(); } ___________________________________________________________ //Example 2 //Structure array #include #include struct student { char name; int roll; int marks; }; void main() { struct student s; int i; clrscr(); printf("Enter information of 3 students:\n"); for(i=0;i

Use Quizgecko on...
Browser
Browser