Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views

Structure - Variable and Union (C Programming)

The document discusses structures in C programming. It defines a structure as a collection of variables of the same or different data types grouped under a single name. It provides the syntax for declaring and initializing structures and gives examples of declaring and initializing structure variables. It also discusses using structures in programs to store and display data records of multiple objects.

Uploaded by

shresthaaastha93
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Structure - Variable and Union (C Programming)

The document discusses structures in C programming. It defines a structure as a collection of variables of the same or different data types grouped under a single name. It provides the syntax for declaring and initializing structures and gives examples of declaring and initializing structure variables. It also discusses using structures in programs to store and display data records of multiple objects.

Uploaded by

shresthaaastha93
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

4.3 Structure and Union # Program to display the contents of structure variable.

# What is structure? How do we declare & initialize structure variable give example #include<stdio.h>
also. #include<conio.h>
Structure is collection of one or more variables of same or different data type void main( )
grouped together under a single name. {
clrscr( );
Declaring a structure: The keyword struct is used to declare structure. The
different data type and variables are defined under { } braces. struct student
{
Syntax: char name[20]; // Structure declaration
Struct name char sex[10];
{ int age;
Data type member1; };
Data type member2; struct student a = {“madan”, “male”, 18}; // Structure initialization
…………………………. struct student b = {“anita”, “female”,17};
……………………
}; printf(“\n %s %s %d”, a.name,a.sex,a.age);
printf(“\n %s %s %d”, b.name,b.sex,b.age);
, member1,member2 are different variables which are called structure members. getch( );
}
Initialization of structure : Initialization of structure is similar to array
initialization. Initialization of structure means providing data or value to each # What is structure? Differentiate between array and structure.(Imp)
member of structure. Data are enclosed within curly braces { }. Array Structure
1. Array is the collection of data of 1. Structure is the collection of one or
Example of structure declaration and initialization. same data type. more variable of same or different
struct student 2. No special keyword is used to data type.
{ declare array variable. 2. Struct keyword is used to declare
char name[20]; // Structure declaration 3. It is not so suitable for handling structure variable
char sex[10]; large data records. 3. It is suitable for handling large
int age; 4. syntax: data records.
}; Data type array_variable[size]; 4. syntax :
struct student a = {“madan”, “male”,18}; // Structure initialization Struct structurename
struct student b = {“anita”, “female”,17}; {
Data type member1;
Here, student is structure name and name,sex,age are different variables of Data type memer 2;
different data type of student type structure. a & b are student type structure …………………..
variable. };
# Write a c program using structure variable to display the name, sex & age of any int j;
5 people. struct employee p[5];
#include<stdio.h> // entering data records
#include<conio.h> for( j=0;j<5;j=j+1)
void main( ) {
{ Printf(“enter name, post, salary “);
clrscr( ); Scanf(“%s %s %d”, &p[j].name, &p[j].post, &p[j].salary);
struct student }
{ // displaying data records
char name[20]; // Structure declaration
char sex[10]; for( j=0;j<5;j=j+1)
int age; {
}; printf(“%s %s %d”, &p[j].name, &p[j].post, &p[j].salary);
struct student a[5] = { }
{“madan”, “male”, 18}, getch( );
{ “kamal”, “male”, 17}, }
{ “sarita”, “female”,19},
{ “Gita”, “female”,17}, What is union? Write syntax for declaring union and also give example.
{ “Hemant”, “male”, 18} Ans : Union is similar to structure because it can store multiple data type with
}; same name. But union keeps or reserves only one memory space for all the data
// displaying data records
type member. Only one member data can be accessed at a time because only one
for ( int j=0;j<5;j=j+1)
{ union variable is active at time.
printf(“\n %s \t %s \t %d”, a[j].name,a[j].sex,a[j].age); Syntax:
} // example of union
Union union_name
getch( ); void main( )
{
} {
Data type member1;
union test
Data type member2;
# write a c program to read and display the name, post and salary of any 5 {
………………………
employee. int a,b;
}
void main( ) };
{ union test v;
clrscr( );
v.a = 50;
struct employee
{ v.b = 100;
char name[20]; // Structure declaration printf(“a = %d”, v.a);
char post[10]; printf(“b= %d”, v.b);
int salary;
}; getch( );
}

You might also like