Initializing & Accessing the Structure Members in C


In C, a structure (also known as a struct) is a user-defined data type that groups together related data items. To initialize a struct, you can use the following syntax:

struct struct_name { data_type member1; data_type member2; ... } variable_name = { value1, value2, ... };

  • This program that demonstrates the use of a struct. The struct "student" is defined at the beginning of the program with three members: name, age, and per. The name member is of the type char pointer, age is of type int and per is of type float.
  • The main function of the program starts by initializing a struct student object named "o" with the values "Tutor Joes" for the name member, 30 for the age member and 85.5 for the per member.
  • The program then uses the printf function to print the values of the members of the struct student object "o" by using the dot operator (.) to access the members. The program prints the name of the student, the age of the student, and the percentage of the student.
  • Finally, the program returns 0 indicating that the program executed successfully.

Source Code

//Initializing & Accessing the Structure Members in C Programming
 
#include<stdio.h>
 
struct student
{
    char *name;
    int age;
    float per;
};
int main()
{
    struct student o={"Tutor Joes",30,85.5};
 
    printf("\nName        : %s",o.name);
    printf("\nAge         : %d",o.age);
    printf("\nPercent     : %f",o.per);
    return 0;
}
 
To download raw file Click Here

Output

Name        : Tutor Joes
Age         : 30
Percent     : 85.500000

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C