0% found this document useful (0 votes)
16 views3 pages

1 Structure

The document explains the concept of structures in C programming, which are user-defined data types that can hold multiple variables of different data types. It details the syntax for defining and declaring structures, as well as how to create structure variables both inside and outside of the main function. Additionally, it describes how to access structure members using the dot operator.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

1 Structure

The document explains the concept of structures in C programming, which are user-defined data types that can hold multiple variables of different data types. It details the syntax for defining and declaring structures, as well as how to create structure variables both inside and outside of the main function. Additionally, it describes how to access structure members using the dot operator.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Unit IV-Structures and File Handling

 Structure
Array is used to represent a group of data items (elements)
having same data type such as int, float etc. If we want to represent a
collection of data items (elements) of different data types within a
single name then we cannot use an array for that ‘C’ supports a data
type known as “structure”.

** definition of structure
“A structure is a user defined data type which contains
similar or different data types.

** How to define/declare structure


The syntax to define/declare a structure is as follows
struct structure_name
{
data type member1;
data type member2;
.
.
data type membern;
};
Here,
‘struct’ keyword declare a structure that store multiple
variables(members) of different data types. These variables
(elements) are known as “structure elements/members of
structure”. Each member may have same or different data type. The
name of the structure is known as “structure_name”.
struct student
{
int rno;
char name[20];
};
Here,
struct keyword declare a structure student with members rno and
name.
** declaration of structure_variable
structure_variable can be declare by using 2 methods,
1) outside main()
Syntax struct structure_name
{
// members of structure
}structure_variable_name1… structure_variable_namen;
e.g struct student
{
int rno;
char name[20];
} p; <----- structure_variable_name

2) inside main()
Syntax
struct structure_variable_name1… structure_variable_namen;
e.g struct student p;
**Example
#include<stdio.h>
struct student
{
int rno;
char name[20];
};
int main()
{
struct student p; // structure_variable_name
printf(“enter rno”);
scanf(“%d”,&p.rno);
printf(“enter name”);
scanf(“%s”,p.name);
printf(“\n rno is %d”,p.rno);
printf(“\n name is %d”,p.name)
return 0;
}
**Accessing structure members/elements
We can access structure elements by using member operator
‘.’, which is also called as “dot operator or period operator”.
In the above example we have read rno and name by using
scanf () and write (display) rno and name by using printf() with
dot(.) operator.

You might also like