Structures
A structure is a user-defined data type that can contain items of different types as its members. In C, struct keyword is used to declare structures and we can use ( . ) dot operator to access structure members.
Structure Template
To use structure, we first have to define its template.
struct struct_name { member_type1 name1; member_type1 name1; . . };
Structure Variable Syntax
...{ ...structure template... }var1, var2..., varN;
or
strcut str_name var1, var2,...varN;
Example of Structure
C
// C program to illustrate the use of structures #include <stdio.h> // declaring structure with name str1 struct str1 { int i; char c; float f; char s[30]; }; // declaring structure with name str2 struct str2 { int ii; char cc; float ff; } var; // variable declaration with structure template // Driver code int main() { // variable declaration after structure template // initialization with initializer list and designated // initializer list struct str1 var1 = { 1, 'A' , 1.00, "w3wiki" }, var2; struct str2 var3 = { .ff = 5.00, .ii = 5, .cc = 'a' }; // copying structure using assignment operator var2 = var1; printf ( "Struct 1:\n\ti = %d, c = %c, f = %f, s = %s\n" , var1.i, var1.c, var1.f, var1.s); printf ( "Struct 2:\n\ti = %d, c = %c, f = %f, s = %s\n" , var2.i, var2.c, var2.f, var2.s); printf ( "Struct 3\n\ti = %d, c = %c, f = %f\n" , var3.ii, var3.cc, var3.ff); return 0; } |
Struct 1: i = 1, c = A, f = 1.000000, s = w3wiki Struct 2: i = 1, c = A, f = 1.000000, s = w3wiki Struct 3 i = 5, c = a, f = 5.000000
C Cheat Sheet
This C Cheat Sheet provides an overview of both basic and advanced concepts of the C language. Whether you’re a beginner or an experienced programmer, this cheat sheet will help you revise and quickly go through the core principles of the C language.
In this Cheat Sheet, we will delve into the basics of the C language, exploring its fundamental concepts that lay the groundwork for programming. We will cover topics such as variables, data types, and operators, providing you with a solid understanding of the building blocks of C programming.
Contact Us