×

Use of Structure in C

We can usually use arrays in C programming to store elements of the same data type. We can use strings to store multiple elements of a character data type. Still, there is a programming need to store multiple items of different data types, for example, when collecting data from students whose names are of the character data type. The second type of data that a roll number has is a number.

Structures

This structure is used to store multiple elements of different data types. The structure can contain "n" elements. This means any number of elements. Now let's look at the syntax. 

Syntax

struct structurename
{
datatype ele1;
datatype ele2;
.
.
.
.
};

We should always start with the keyword "struct" and add the struct name that the programmer can want. Next, we need to open the square bracket declaration. Finally, close the parentheses after declaring all the elements with the data type. It is important to add a semicolon at the end.

Syntax Example

struct Customer
{
int customerid;
char name[20];
};
main()
{
int a;
Struct Customer c[25];
}

When we declare a variable in the main function, we need to add the data type and then the variable name. For structures, the data type name is "struct structurename" or "struct Customer". Next, we need to add the variable name "c". This allocates memory for the variable. Structure variables always containa base address. This refers only to the data type, but sinceit is an internal pointer value, there is no pointer here. A " c " structural variable is allocated 24 bytes of memory [20]. Each character occupies 1 byte of memory. Characters mean 20 bytes for 20 characters, and integer variables are 4 bytes on 64-bit systems for 24 bytes. We have saved the information so far. Let's move to the access part.

Accessing

We can access the structure simply by entering the dot ".". Operators such as "c.customerid" and "c.name". Before we go any further, we need to know how to declare the size of the structure. 

Size of Structure

We can use the keyword "sizeof" to find the size, sizeof (variable name). We can also use sizeof (struct structname). Check out the examplesbelow to get a quick idea. 

Use Of Structure In C
#include<stdio.h>
struct Customer
{
int customerid;
char name[20];
};
void main()
{
struct Customer c;
printf("size of Customer structure is %lu  \n",sizeof(c));
printf("size of Customer is %lu ",sizeof(struct Customer));


}

Output:

Use Of Structure In C

We used "% ul" here. This represents an unsigned long or unsigned int. We can also use “%d”, but the C compiler will display a small warning. Finally, sizeof () allows us to pass either a variable name or a data type name. Next, let's move on to the typeof structure.

Types of Structures

There are two maintypes of structures.

Use Of Structure In C

1. Global Structure

The structure definition, which is common for all functions that are entire programs is called global structure.

Global Structure Declaration

#include<stdio.h>


struct globalstruct
{
int a,b;
};
void main()
{
struct globalstruct g;
}
int try()
{
struct globalstruct l;
//It also executes here
}


Output

Use Of Structure In C

Here the program is executing successfully.

2. Local Structure

The structure which is only for a specific function is called Local structure. Here, is the declaration of structures inside a particular function.

Declaration of local structure

#include<stdio.h>
void main()
{
struct localstruct
{
int a,b;
};
struct localstruct l;
//It will work here
}
int try()
{
struct localstruct l;
//It won't execute here 
}


Output:

Use Of Structure In C

Here we can access it only with the main function. That is why we got compilation error that “l” is not known. Simply it is like local and global variables concept.

Well, each type has its own application and advantages but both save time and helps in efficiently data efficiently.

Advantages of structures

1. Unlike arrays and strings, it is possible to create data types with different data types. For example, if we want to keep student records, including student names, roll numbers, grades, and addresses, the items can be of different types. For example, the roll number is an integer, the name is a string, the grade is a floating-point, and the address is a string. 

2. Reduce complexity and increase productivity. 

3. Because it is heterogeneous, it eliminates the heavy load of saving to different files.

4. Code maintainability is improved

5. Very suitable for some math operations


Related Topics

How to Merge Array in C?

Introduction Merging arrays is a common task for many developers but might be difficult for beginners of C programming. Fortunately, our guide will show you how to quickly merge arrays in...

6 minutes read.

While Loop in C programming examples

Introduction There is always a header file containing all the essential data regarding inputs and outputs of various functions in the C programs. The following statement describes how to use/add header file...

22 minutes read.

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

4 minutes read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.

Cbrt() function in C

Introduction: The Cbrt is a function used in C programming language. The cbrt() function is a math function. Using the cbrt function, we can do the cube root of a function. This...

4 minutes read.

Null character in C

Introduction The Null character in the C programming language is used to terminate the character strings. In other words, the Null character is used to represent the end of the string...

3 minutes read.

Operators in C

An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. It is the combination of constants and variables through expressions. Example: int c = a+b*5 Where,...

5 minutes read.

Explain the two-way selection in C

In C programming, a two-way selection statement is used to choose between two different options based on a Boolean expression. The two-way selection statement in C is the if statement. The...

6 minutes read.

Self-referential structure in C

Self-referential structure in C A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers...

3 minutes read.

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

4 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

Bar3d() function in C Graphics

The bar3d function is used to create a 2-dimensional filled-in rectangular bar. We can also create three-dimensional shapes in C using helpful function. The first step in creating this 3D...

3 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

Pure Virtual Function in C

Before knowing about the pure virtual function some of the important points about the virtual function in C++ are given below. In c++ the member function that is defined in the...

4 minutes read.

Garbage in C

Garbage in C The C programming language is a perfect fit for embedded systems as it provides low level control, structured programming, and portability. On the contrary, it does not provide...

3 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.