Example of Structure in C
The following C program shows how to use structures
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; } |
Output
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 Structures
The structure in C is a user-defined data type that can be used to group items of possibly different types into a single type. The struct keyword is used to define the structure in the C programming language. The items in the structure are called its member and they can be of any valid data type.