Operations on Struct Variable

Struct Variable

Operations on struct variable can be used only by assignment operation. Any other operations are not allowed on struct variables.

Operations on Struct Variable

Syntax of Struct Variable

struct employee {
  // code
};

int main() {
  struct employee employee1, employee2, e[10];
  return 0;
}

Here we’ll discuss two problems where problem 1 runs easily without any compilation error and gives output while problem 2 doesn’t run and gives compilation error.

Problem 1

Run
#include<stdio.h>
struct Employee {
int x;
int y;
};

int main()
{
struct Employee e1 = {20000, 30000};
struct Employee e2 = e1; 
printf(" e2.x = %d, e2.y = %d", e2.x, e2.y);
getchar();
return 0;
}

output

e2.x = 20000, e2.y = 30000

Problem 2

Run
#include<stdio.h>
struct Employee
{
  int x;
  int y;
};

int main ()
{
  struct Employee e1 = { 10, 20 };
  struct Employee e2 = e1;
  if (e1 == e2)
    {
      printf ("e1 and e2 are same ");
    }
  getchar ();
  return 0;
}

Note:

We'll not get any output from the above program
Because of invalid operands to binary == (have 'struct Employee' and 'struct Employee')

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription