Saturday, 17 June 2023

Declaring Structure Variables in C

What is declaring structure variables?

By explain a structure we have only generate a format, the real use of structures will be when we declare variables or flexible based on this format. We can state or declare structure variables in two ways –

C language, structure definition, using the structure tag
Declaring structure variables image-1

1.       With structure definition

2.       Using the structure tag

1.1   With structure definition

Struct student{

                                char name [10];

                                int rollno;

                                float marks;

                                }stu1,stu2,stu3;

Here stu 1, stu2 and stu 3 are variables or adjustable of type struct student. When we declare a variable while explain or defining the structure device or template, the tag name is optional.

 

1.2   Using the structure tag

We can also state or declare structure variables using organization or structure tag. This can be written as –

      struct student{

                                char name [10];

                                int rollno;

                                float marks;

                                };

struct student stu1, stu2;

struct student stu3;

Here stu1, stu2, and stu3 are structure or organization variables that are state or declared using the structure tag student. Declaring a structure variable set aside or reserves space in memory. Each structure variable state or declared to be of type struct student has three members viz. rollno, name and marks. The editor or compiler will reserve space for each variable enough or sufficient to hold all the members. For example each variable of type struct student will settled or occupy 28 (20+4+4) bytes.

 

1.3   Initialization of structure variables

The pattern or syntax of establishes structure variables are alike to that of arrays. All the worth is given in wavy or curly braces and the number, order and type of these values should be same as in the structure device definition. The establishes values can only be continuous or constant expressions.

                                sturct student {

                                                char name [10];

                                                int rollno;

                                                float marks;

                                                }stu1={“Gopi”,25,9};

                                                Struct student stu2={“Pasi”, 24,67.5};

Here value of members of stu1 will be “Gopi” for name, 25 for rollno, 98 for marks. The values of members of stu2 will be “Pasi” for name, 24 for rollno, 67.5 for marks.

Note: we cannot establish members while explain the structure.

 If the number of establish is less than the number of members then the carry on members are establishes with zero. For example if we have this establishes –

   struct student stu1 = {“Gopi”};

Here the members rollno and marks of stu1 will be establishes to zero. This is equal to the initialization –

          struct student stu1 = {“Gopi”, 0, 0};


Computer stuff kit tricks of Topics 59.

What are control statement in C language?