17/03/2023

Explain the Structure of C++ Program With an Example

Briefly Explain the Structure of the C++ Program With an Example. The structure is a user-defined data type in C++. The structure is used to represent a record. Suppose you want to store a record of a Student which consists of the student's name, address, roll number and age. You can define a structure to hold this information.

explain structure of c++ program with an example

Explain the Structure of C++ Program With an Example


  • Basic Structure of C++ Program
  • Briefly Explain About Structure of a C++ Program
  • Write the Structure of a C++ Program with an Example

1. Basic Structure of C++ Program


The struct keyword is used to define a structure. struct define a new data type which is a collection of different type of data.

Structure Syntax:

struct structure_name
{
Statement...........1
Statement...........2
Statement...........3
.
.
.
Statement...........n
};

Defining a Structure


struct keyword is used to define a structure. struct defines a new data type, a collection of different data types. Below is the Syntax of the Structure for the C Program to Display Student Details Using Structure.

Syntax of Structure

struct structure_name
{
//Statements
};

Briefly Explain About Structure of a C++ Program.


Here we are Defining the Structure and the Data Types with Structure. structure program for student details in c to store the Student Name, Roll no, and Student Marks.

struct student
{
    char name[50];
    int roll;
    float marks;
};


Here we are storing single student information ie, Student Name, Student Roll Number and Student Marks without using the FOR LOOP.


printf("Enter The Information of Students :\n\n");

scanf("%s",s.name);
scanf("%d",&s.roll);
scanf("%f",&s.marks); 

Here we are Printing the Output of the Structure Details, Student Name, Student Roll Number and Student Marks without using any LOOPS.


printf("Name: %s\n",s.name);
printf("Roll: %d\n",s.roll);
printf("Marks: %.2f\n",s.marks);

Write the Structure of a C++ Program with an Example


#include <stdio.h>

struct student
{
	char name[50];
	int roll;
	float marks;
};

int main()
{
	struct student s;

	printf("Enter the Students Information:\n\n");

	printf("Enter Name: ");
	scanf("%s", s.name);

	printf("Enter Roll No.: ");
	scanf("%d", &s.roll);

	printf("Enter Marks: ");
	scanf("%f", &s.marks);

	printf("\nStudents Information\n");

	printf("Name: %s\n", s.name);
	printf("Roll: %d\n", s.roll);
	printf("Marks: %.2f\n", s.marks);
	return 0;
}

Similar to Structure 


Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: