33
Total Posts
3
Categories
24/7
Updated

C++

Write a C++ program to declare Struct. Initialize and display the contents of member variables

Published on Dec 22, 2025
#include <iostream>
using namespace std;

struct Student {
    int rollNo;
    char name[20];
    char grade;
};

int main() {
    // Initializing structure variables
    Student s1 = {101, "Rahul", 'A'};

    // Displaying structure contents
    cout << "Student Details" << endl;
    cout << "Roll No: " << s1.rollNo << endl;
    cout << "Name: " << s1.name << endl;
    cout << "Grade: " << s1.grade << endl;

    return 0;
}

Documentation

Output

 

Student Details
Roll No: 101
Name: Rahul
Grade: A

 

 

  1. struct is used to group different data types.
  2. Members are accessed using the dot (.) operator.

  3. Structure variables can be initialized at declaration.