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.

Similar Reads

C Structure Declaration

We have to declare structure in C before using it in our program. In structure declaration, we specify its member variables along with their datatype. We can use the struct keyword to declare the structure in C using the following syntax:...

C Structure Definition

To use structure in our program, we have to define its instance. We can do that by creating variables of the structure type. We can define structure variables using two methods:...

Access Structure Members

We can access structure members by using the ( . ) dot operator....

Initialize Structure Members

Structure members cannot be initialized with the declaration. For example, the following C program fails in the compilation....

Example of Structure in C

The following C program shows how to use structures...

typedef for Structures

...

Nested Structures

The typedef keyword is used to define an alias for the already existing datatype. In structures, we have to use the struct keyword along with the structure name to define the variables. Sometimes, this increases the length and complexity of the code. We can use the typedef to define some new shorter name for the structure....

Structure Pointer in C

...

Self-Referential Structures

C language allows us to insert one structure into another as a member. This process is called nesting and such structures are called nested structures. There are two ways in which we can nest one structure into another:...

C Structure Padding and Packing

...

Bit Fields

We can define a pointer that points to the structure like any other variable. Such pointers are generally called Structure Pointers. We can access the members of the structure pointed by the structure pointer using the ( -> ) arrow operator....

Uses of Structure in C

...

Limitations of C Structures

The self-referential structures in C are those structures that contain references to the same type as themselves i.e. they contain a member of the type pointer pointing to the same structure type....