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

Structure in C Programming

This document discusses structures in C programming. Structures allow grouping of variables of different data types under a single name. The document defines a Person structure with name, age, and height members, declares a person1 variable of struct Person type, prompts the user to enter values for members, and displays the stored information.

Uploaded by

Rajas Patil
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)
14 views3 pages

Structure in C Programming

This document discusses structures in C programming. Structures allow grouping of variables of different data types under a single name. The document defines a Person structure with name, age, and height members, declares a person1 variable of struct Person type, prompts the user to enter values for members, and displays the stored information.

Uploaded by

Rajas Patil
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

18_structres.md https://embeddedforall.

com/ 2024-01-14

structure in C programming
In C, a structure is a user-defined data type that allows you to group together variables of different
data types under a single name. Each variable within a structure is called a member or a field.

Structures provide a way to organize and represent complex data in a more meaningful and coherent manner.

The syntax for defining a structure in C is as follows:

struct StructureName {
// Member 1
DataType1 Member1;

// Member 2
DataType2 Member2;

// ...

// Member N
DataTypeN MemberN;
};

Here's a breakdown of the syntax elements:

1. struct keyword: Indicates the beginning of the structure definition.


2. StructureName: The name of the structure. This name is used to declare variables of this structure type
later in the program.
3. Members: Inside the structure, you define members, each with its own data type. Members represent
the individual attributes or fields of the structure.
4. Semicolon (;): The structure definition ends with a semicolon.

You use the dot (.) operator to access members of a structure. The syntax is as follows:

structure_variable.member

Here's a practical example to illustrate the concept of structures:

#include <stdio.h>

// Define a structure named 'Person'


struct Person {
char name[50];
int age;
float height;
};

1/3
18_structres.md https://embeddedforall.com/ 2024-01-14

int main() {
// Declare a variable of type 'struct Person'
struct Person person1;

// Access and modify the members of the structure


printf("Enter name: ");
scanf("%s", person1.name);

printf("Enter age: ");


scanf("%d", &person1.age);

printf("Enter height (in meters): ");


scanf("%f", &person1.height);

// Display information using the structure members


printf("\nDetails of the person:\n");
printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
printf("Height: %.2f meters\n", person1.height);

return 0;
}

Output:

Enter name: Veeresh


Enter age: 33
Enter height (in meters): 1.62

Details of the person:


Name: Veeresh
Age: 33
Height: 1.62 meters

In this example:

Structure Definition:

struct Person {
char name[50];
int age;
float height;
};

This defines a structure named Person containing three members: name (an array of characters), age (an
integer), and height (a float).

2/3
18_structres.md https://embeddedforall.com/ 2024-01-14

Structure Variable Declaration:

struct Person person1;

This declares a variable named person1 of type struct Person.

Accessing and Modifying Structure Members:

printf("Enter name: ");


scanf("%s", person1.name);

printf("Enter age: ");


scanf("%d", &person1.age);

printf("Enter height (in meters): ");


scanf("%f", &person1.height);

Here, the program prompts the user to input values for the name, age, and height members of the person1
structure.

Displaying Structure Information:

printf("\nDetails of the person:\n");


printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
printf("Height: %.2f meters\n", person1.height);

Finally, the program displays the information entered by the user using the structure members.

This example demonstrates how structures in C allow you to create a custom data type (struct Person) to
represent a person's information with multiple attributes. Structures are particularly useful when dealing with
entities that have various properties, making the code more organized and readable.

3/3

You might also like