Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Structure in C Notes for GATE

Structure is an important topic belonging to the world of programming. And, when it comes to a competitive examination like GATE, you have to read the whole topic quite exhaustively. In this article, we have covered all the topics relevant to the structure. We hope that the notes for the CSE topics will help you understand this topic in a better way.

What is a Structure in C?

Structures are the user-defined data type, which allow us to collect the group of different data types. Here, all the individual components or elements of structures are known as a member.

What is Structure

Syntax to Define the Structure in C

Syntax to Define the Structure in C

Example of Structure in C

You can understand the structure in C with the help of the example below:

struct student name

{ int rollnumber;

char name[20];

Float percentage;

};

Here struct Student is keeping the information of a student which consists of three data fields, roll number, name, and percentage. These fields are known as structure elements or members.

These elements can be of different data types. For example, here roll number is int type, the name is char type, etc.

Memory Allocation In Structure in C

Memory Allocation In Structure in C

How to Declare Variables of a Structure?

We can declare the variables in two ways, the syntax is given below:

struct struct_name var_name;

Or

struct struct_name {

DataType member1_name;

DataType member2_name;

DataType member3_name;

} var_name;

How to Access Data Members of a Structure Using a Struct Variable?

var_name.member1_name;

Var_name.member2_name;

For Example:

struct Employee

{

char name[20];

int age;

char department[15];

//F for female and M for male

char gender;

};

struct Employee e1, e2; //declaring variables of struct Employee

In this above code, we have declared the variables separately. We can also declare the variables within the definition of structure.

For Example:

struct Employee

{

char name[20];

int age;

char department[15];

//F for female and M for male

char gender;

}E1, E2;

Detailed Example of Structure in C

#include <stdio.h>

/* Created a structure here. The name of the structure is

* EmployeeData.

*/

struct EmployeeData{

char *emp_name;

int emp_id;

int emp_age;

};

int main()

{

/* employee is the variable of structure EmployeeData*/

struct EmployeeData employee;

/*Assigning the values of each struct member here*/

employee.emp_name = “John”;

employee.emp_id = 1234;

employee.emp_age = 40;

/* Displaying the values of struct members */

printf(“Employee Name is: %s”, employee.emp_name);

printf(“\nEmployee Id is: %d”, employee.emp_id);

printf(“\nEmployee Age is: %d”, employee.emp_age);

return 0;

}

Output of the Above Code:

Employee Name is: John

Employee Id is: 1234

Employee Age is: 40

Practice Problems – Structure in C

Q. Consider the following C program:

#include <stdio.h>

int main()

{

int a[ ] = {2, 4, 6, 8, 10};

int i, sum = 0, *b = a + 4;

for (i = 0; i < 5; i++)

sum = sum + (*b – i) – *(b – i);

printf (“%d\n”, sum);

return 0;

}

The output of the above C program is __________.

Q. Consider the following C program:

#include <stdio.h>

int main(){

float sum = 0.0, j = 1.0, i = 2.0;

while (i/j > 0.0625){

j = j + j;

sum = sum + i/j;

printf(“%f\n”, sum);

}

return 0;

}

The number of times the variable sum will be printed, when the above program is executed, is _________________.


Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility CriteriaGATE 2023GATE Admit CardGATE Syllabus for CSE (Computer Science Engineering)GATE CSE NotesGATE CSE Question Paper, and more.

Also Explore,

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*