×

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 "sizeofto 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

1Unlike 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. 

2Reduce 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

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch()...

3 minutes read.

How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two...

3 minutes read.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 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.

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.

Purpose of a Function Prototype in C

A function prototype in C is a declaration of a function that specifies the function's name, return type and parameters. It has the following syntax: return_type function_name(parameter_list); For example, the prototype for...

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.

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

Add two numbers using the function in C

Here we will learn how to add two numbers by creating function in C. Let’s learn this with help of example. Code: - #include <stdio.h> int add_two_no(int a, int b); int main(){   int first_num,...

1 minute read.

gets() function in C

gets() is a pre-define or built-in function present in the stdio.h header file. stdio stands for standard input and output. gets() function is used to read a stream and store...

4 minutes read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

Command line arguments in C

Command line arguments in C The arguments that are generally passed from the line of command are referred to as command line arguments. These command line arguments are always handled by...

3 minutes read.

How to lock top row in Excel

How to lock top row in excel While working with an Excel spreadsheet, the user utilizes the rows and columns to enter the details under various headings. Sometimes while scrolling the...

4 minutes read.

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 minutes read.

Why C is a middle level language

The C programming language is generally referred to as a high level language but we glance through the backend of C, i.e the working of C as a programming language...

4 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.