Tutorial Playlist
132 Lessons1. Introduction to C Tutorial
2. Addition of Two Numbers in C
3. Anagram Program in C
4. Armstrong Number in C
5. Array in C
6. Array of Pointers in C
7. Array of Structure in C
8. C Program to Find ASCII Value of a Character
9. Assignment Operator in C
10. Binary Search in C
11. Binary to Decimal in C
12. Bitwise Operators in C
13. Boolean in C
14. C Compiler for Mac
15. C Compiler for Windows
16. C Function Call Stack
17. C Language Download
18. Operators in C
19. C/C++ Preprocessors
20. C Program for Bubble Sort
21. C Program for Factorial
22. C Program for Prime Numbers
23. C Program for String Palindrome
24. C Program to Reverse a Number
25. Reverse a String in C
26. C string declaration
27. String Input Output Functions in C
28. Calculator Program in C
29. Call by Value and Call by Reference in C
30. Ceil Function in C
31. Coding Vs. Programming
32. Command Line Arguments in C/C++
33. Comments in C
34. Compilation process in C
35. Conditional Statements in C
36. Conditional operator in the C
37. Constant Pointer in C
38. Constants in C
39. Dangling Pointer in C
40. Data Structures in C
41. Data Types in C
42. Debugging C Program
43. Convert Decimal to Binary in C
44. Define And include in C
45. Difference Between Arguments And Parameters
46. Difference Between Compiler and Interpreter
47. Difference Between If Else and Switch
48. Do While Loop In C
49. Double In C
50. Dynamic Array in C
51. Dynamic Memory Allocation in C
52. Enumeration (or enum) in C
53. Evaluation of Arithmetic Expression
54. Factorial of A Number in C
55. Features of C Language
56. Fibonacci Series Program in C Using Recursion
57. File Handling in C
58. For Loop in C
59. Format Specifiers in C
60. Functions in C
61. Function Pointer in C
62. goto statement in C
63. C Hello World Program
64. Header Files in C
65. Heap Sort in C Program
66. Hello World Program in C
67. History of C Language
68. How to compile a C program in Linux
69. How to Find a Leap Year Using C Programming
70. Identifiers in C
71. If Else Statement in C
72. If Statement in C
73. Implementation of Queue Using Linked List
74. Increment and decrement operators in c
75. Input and Output Functions in C
76. How To Install C Language In Mac
77. Jump Statements in C
78. Lcm of Two Numbers in C
79. Length of an Array in C
80. Library Function in C
81. Linked list in C
82. Logical Operators in C
83. Macros in C
84. Matrix multiplication in C
85. Nested if else statement in C
86. Nested Loop in C
87. One Dimensional Array in C
88. Operator Precedence and Associativity in C
89. Overflow And Underflow in C
90. Palindrome Program in C
91. Pattern Programs in C
92. Pointer to Pointer in C
93. Pointers in C: A Comprehensive Tutorial
94. Pre-increment And Post-increment
95. Prime Number Program in C
96. Program for Linear Search in C
97. Pseudo-Code In C
98. Random Access Files in C
99. Random Number Generator in C
100. Recursion in C
101. Relational Operators in C
102. Simple interest program in C
103. Square Root in C
104. Stack in C
105. Stack Using Linked List in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
112. String Comparison in C
113. String Functions in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
Now Reading
118. Structure of C Program
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
122. Toupper Function in C
123. Transpose of a Matrix in C
124. Two Dimensional Array in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
129. User Defined Functions in C
130. What is Variables in C
131. Is C language case sensitive
132. Fibonacci Series in C
Structures in C are user-defined data types that compile two or more alike or diverse data structures or data types together in a single variant. You can create a structure variable with the help of the keyword struct and the structure tag name. Using the data type formed via structure in C, you can define a pointer, include structure as a return type in a function, or pass structure as a function argument.
For example, you can create a structure for an employee. You can use a character array to store the title, an integer to store the employee id, etc.
Using structures in C tackles the limitation of arrays. In C, arrays are restrained to store variables of identical data types. By creating a structure, you can declare multiple variables of various data types. C treats these data types as a single entity.
For example, the following program helps you to store some data of an employee like a name and employeeid. We use char data type to store the name of the employee and int data type to store the employeeid.
From the above structure in C example, we can infer that it is easy to store data of a single employee but it's difficult to create variables for 100 or more employees. To deal with this issue, you can create a structure in C that stores or binds various data types together.
char Name[100]; // defines a character array to store the name of each employee |
The structure data type is widely used in scenarios where you want to group associated variables to denote a single concept or entity. It is useful to signify real-world entities like a book, car, person, etc.
It is useful to denote records in a database or read data from a file. Each structure will relate to a single record. The corresponding variables would signify the various fields of the record.
If you want to form a structure in C, you must first use the keyword struct and then that structure’s tag name. The next part is the structure’s body, in which you can add the necessary data members (user-defined or primitive data types).
Here’s the structure syntax in C
struct structure_name |
Here’s an example of structure in C:
struct Employee |
As seen from the mentioned syntax and example, the data type of the data_members can be char, int, array, double, or other user-defined types of data. Irrespective of the data type, the data_member_definition is simply a variable title like name, employeeid, etc.
Creating a structure comprising all data members is not enough. You must create structure variables to use different properties of your devised structure in C. The following section discusses two ways to declare structure variables in C.
Approach-1:
Syntax:
struct structure_name { |
Example:
struct Employee { |
In this example, an Employee structure is formed, and the employee1 variable is declared for it after the structure definition.
Approach-2:
After creating a structure within C, you can create a user-defined data type. It can be seen as the primitive data type when declaring a variable for the structure.
Syntax:
struct Employee |
In the above example, the structure Employee is first created, and its variable is declared in the main() function.
While declaring the structure variables with the structure definition, C treats them as global variables. You can use the first approach to declare global variables with the structure. If you don’t need global variables, then the next approach is suitable because it is simple to initialise or maintain variables.
Initialising structure members refer to assigning values to these members as per their data types. The declaration process doesn’t assign memory for the structure. The memory is allocated to the structure variable only when you declare a variable for the particular structure. The structure members can’t be initialised during the declaration.
Defining structure in the following way gives a compile error.
Syntax:
struct Employee |
Since we get a compile error, the question is -how to initialise the members correctly? The following section discusses three ways to initialise structure members:
i. Using Dot '.' operator
ii. Using curly braces ‘{}’
iii. Designated initializers
i. Using Dot '.' operator
With the help of the dot (.) operator, you can access any structure member and, subsequently, initialise or allocate its value as per its data type. Here’s the syntax:
Syntax:
struct structure_name variable_name; |
The syntax implies that first, we create a structure variable, and then, using the dot operator, we access its member to initialise them.
ii. Using curly braces ‘{}’
If you want to initialise all the members during the declaration of the structure variable, you can use curly braces for the declaration. Here’s the syntax:
Syntax:
struct stucture_name x1 = {value1, value2, value3, ..}; |
The dot(.) operator allows you to directly access the structure member. This operator is used between the structure variable’s name and the structure member’s name you want to access. Here’s the syntax:
structure_variable.structure_member; |
Here’s an example code to understand how to access structure elements:
#include <stdio.h> { { |
Output:
Name is: James Williams |
In the above example program, the structure ‘Man’ declares name and age. In the main() function, we declare and access the variables of this structure. The later section prints the values of these variables.
Designated initialization feature is introduced in the C99 standard. It lets you initialise a structure’s members through their designated indices or names. This feature allows you to easily initialise certain members of a structure without explicitly initialise all members or depending on their order.
You can create an array of structures in C as you do with other data types. Each block works in the same manner as a single variable of the same data type. Here’s the syntax:
struct structure_name array_name[size_of_array]; |
Using the above syntax, you create an array of structures with every memory block holding a single structure variable.
Let’s look at the following example program to better understand it:
#include <stdio.h> { { |
Output:
Please enter the number of students: 3 |
The above code creates an array of details of students. It prompts the user to enter the details of each student and accordingly prints the output for each of them.
In C, nested structures mean defining a structure within another structure. They allow you to establish a hierarchical relationship between structures.
A structure pointer refers to a pointer variable that stores the memory address of a structure. You can use it to access and dynamically operate on a structure’s members.
Structure member alignment in C refers to the way the members of a structure are arranged in memory. It ensures that each member is allocated at a memory address that is a multiple of its required alignment.
The structures in C can be passed as function arguments to let the function operate on the structure's data. You can pass structures to functions either by reference (using pointers) or by value.
Let’s explore a few limitations of the C structures to ensure their apt implementation while running a C program.
Storing similar data of multiple entities is simplified with the use of structure in C. You can easily access a structure’s data members for further operations. Practising the use of structure in C benefits you with improved code readability and reduced code complexity.
Besides practising the tutorials, pursuing upGrad’s Master of Science in Computer Science at Liverpool John Moores University can help you elevate your career in the tech industry. The industry experts impart the content in an easy-to-understand approach to help you thoroughly grasp the concepts. Covering all the relevant and in-demand topics, upGrad paves the path to your career advancement in STEM!
Enroll now to start your journey!
Q. What are the benefits of using structures in C programming?
Structure in C lets you define user-defined data types that can store items with varied data types. It reduces code complexity because you only need to create a structure and its array. While improving code readability, it can easily handle records containing heterogeneous data items. Thus, it increases productivity.
Q. What is the difference between functions and structures in C?
The reusable codes that perform a particular task when being called are known as functions. Structures are user-defined data types. They can have several fundamental data types called structure members sorted into a single user-defined data type.
Q. How can you pass structure members to functions?
You can use the dot (.) operator to access each member of the structure and then pass them to the function. This approach is useful when you don’t want to pass the whole structure to the function but only pass a few of its members.
Q. What is the difference between structure and union in C?
Structures in C are custom data types that store multiple members of varied data types within a single unit. Unions are user-defined data types that combine objects of various data types in the exact memory location. Structures allow you to initialise multiple members at once, whereas unions allow you to initialise only the first member at a time.
PAVAN VADAPALLI
Director of Engineering
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working …Read More
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...