Structure in C/C++

Structure in C/C++

Structure is a user defined data type in C/C++. It is used to group data items of different datatypes. ‘struct‘ keyword is used to define a structure. Every element of a structure is called a member.
Syntax-

struct abc
{
    ...;
    ...;
    ...;
};

Declaration of structure members has 2 methods :

struct abc{
...
...
};
int main()
{
struct abc x;
...
...
}

or

struct abc{
...
...
}x;

Structure members cannot be initialized with declaration. Firstly they can be taken as input and then accessed as structure members. So to access any member of a structure, we use the member access operator/ dot operator (.). Secondly you can assign values inside parenthesis { }. You will see how it works in the program below.

1. Program in C :

#include<stdio.h>
struct student {
  char name[20];
  int age;
  float marks;
}x;
int main() {
  printf(" Enter name: ");
  scanf("%s", &x.name);
  printf(" Enter age: ");
  scanf("%d", &x.age);
  printf(" Enter average marks: ");
  scanf("%f", &x.marks);
  struct student y = {"Shantanu", 20, 90.30};
  printf("\n By accepting values =>");
  printf("\n Name:  %s\n Age: %d\n Average marks: %f", x.name, x.age, x.marks);
  printf("\n\n By assigning values inside parenthesis {} =>");
  printf("\n Name:  %s\n Age: %d\n Average marks: %f", y.name, y.age, y.marks);
  return 0;
}

2. Program in C++ :

#include<iostream>
using namespace std;
struct student {
  char name[20];
  int age;
  float marks;
}x;
int main() {
  cout << " Enter name: ";
  cin >> x.name;
  cout << " Enter age: ";
  cin >> x.age;
  cout << " Enter average marks: ";
  cin >> x.marks;
  struct student y = {"Shantanu", 20, 90.30};
  cout << "\n By accepting values =>";
  cout << "\n Name: " << x.name << "\n Age: " << x.age << "\n Average marks: " << x.marks;
  cout << "\n\n By assigning values inside parenthesis {} =>";
  cout << "\n Name: " << y.name << "\n Age: " << y.age << "\n Average marks: " << y.marks;
  return 0;
}

3. Output-

structure output

4. Parts of program-

1. #include is a pre-processor directive. It is used to include header files.
2. stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
3. .h is an extension for header file.
4. int, char, float are data types.
5. main() is main function that indicates the compiler that the user written programs starts from here.
6. printf/cout is the keyword use to print a message.
7. scanf/cin is the keyword use to store values in variables.
8. return statement is use to return a value. If any error occurs the program will return 0.

CheckoutCurrent Date and time program in C/C++

Conclusion-

So this was the concept of Structure in C/C++. We hope it was clear to you. If you find it useful then do share it with your programming buddies and friends. Also if you have any doubt regarding any post do tell us in the comment section.

For daily updates do follow us on Instagram. Visit again!

ErrorFreeProgram.

1 COMMENT

Leave a Reply

Your email address will not be published. Required fields are marked *