0% found this document useful (0 votes)
28 views13 pages

Structure and ENUM in C

The document discusses structures in C. It explains that structures allow grouping different data types together and accessing them via a common variable name. It provides examples of defining structures, declaring structure variables, and accessing members. It also covers related topics like memory allocation for structures and using structures with functions.

Uploaded by

kumar9758028
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views13 pages

Structure and ENUM in C

The document discusses structures in C. It explains that structures allow grouping different data types together and accessing them via a common variable name. It provides examples of defining structures, declaring structure variables, and accessing members. It also covers related topics like memory allocation for structures and using structures with functions.

Uploaded by

kumar9758028
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

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.

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

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
_________________.

Frequently Asked Questions Related to Structure in C


Q1
What is the use of struct?
‘Struct’ is a keyword that is used to create a structure.
Q2
What is a Structure in C?
Structures are the user-defined data type, which allow us to collect the group of different data
types.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Enum in C
The enum in C is also known as the enumerated type. It is a user-defined data type
that consists of integer values, and it provides meaningful names to these values. The
use of enum in C makes the program easy to understand and maintain. The enum is
defined by using the enum keyword.

The following is the way to define the enum in C:

1. enum flag{integer_const1, integer_const2,.....integter_constN};


In the above declaration, we define the enum named as flag containing 'N' integer
constants. The default value of integer_const1 is 0, integer_const2 is 1, and so on. We
can also change the default value of the integer constants at the time of the
declaration.

For example:

1. enum fruits{mango, apple, strawberry, papaya};

The default value of mango is 0, apple is 1, strawberry is 2, and papaya is 3. If we


want to change these default values, then we can do as given below:

1. enum fruits{
2. mango=2,
3. apple=1,
4. strawberry=5,
5. papaya=7,
6. };

Enumerated type declaration


As we know that in C language, we need to declare the variable of a pre-defined type
such as int, float, char, etc. Similarly, we can declare the variable of a user-defined
data type, such as enum. Let's see how we can declare the variable of an enum type.

Suppose we create the enum of type status as shown below:

1. enum status{false,true};

Now, we create the variable of status type:

1. enum status s; // creating a variable of the status type.

In the above statement, we have declared the 's' variable of type status.

To create a variable, the above two statements can be written as:

1. enum status{false,true} s;

In this case, the default value of false will be equal to 0, and the value of true will be
equal to 1.

Let's create a simple program of enum.


1. #include <stdio.h>
2. enum weekdays{Sunday=1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
};
3. int main()
4. {
5. enum weekdays w; // variable declaration of weekdays type
6. w=Monday; // assigning value of Monday to w.
7. printf("The value of w is %d",w);
8. return 0;
9. }

In the above code, we create an enum type named as weekdays, and it contains the
name of all the seven days. We have assigned 1 value to the Sunday, and all other
names will be given a value as the previous value plus one.

Output

Let's demonstrate another example to understand the enum more clearly.

1. #include <stdio.h>
2. enum months{jan=1, feb, march, april, may, june, july, august, september, october, n
ovember, december};
3. int main()
4. {
5. // printing the values of months
6. for(int i=jan;i<=december;i++)
7. {
8. printf("%d, ",i);
9. }
10. return 0;
11. }
In the above code, we have created a type of enum named as months which consists
of all the names of months. We have assigned a '1' value, and all the other months
will be given a value as the previous one plus one. Inside the main() method, we have
defined a for loop in which we initialize the 'i' variable by jan, and this loop will
iterate till December.

Output

Why do we use enum?


The enum is used when we want our variable to have only a set of values. For
example, we create a direction variable. As we know that four directions exist (North,
South, East, West), so this direction variable will have four possible values. But the
variable can hold only one value at a time. If we try to provide some different value
to this variable, then it will throw the compilation error.

The enum is also used in a switch case statement in which we pass the enum variable
in a switch parenthesis. It ensures that the value of the case block should be defined
in an enum.

Let's see how we can use an enum in a switch case statement.

1. #include <stdio.h>
2. enum days{sunday=1, monday, tuesday, wednesday, thursday, friday, saturday};
3. int main()
4. {
5. enum days d;
6. d=monday;
7. switch(d)
8. {
9. case sunday:
10. printf("Today is sunday");
11. break;
12. case monday:
13. printf("Today is monday");
14. break;
15. case tuesday:
16. printf("Today is tuesday");
17. break;
18. case wednesday:
19. printf("Today is wednesday");
20. break;
21. case thursday:
22. printf("Today is thursday");
23. break;
24. case friday:
25. printf("Today is friday");
26. break;
27. case saturday:
28. printf("Today is saturday");
29. break;
30. }
31.
32. return 0;
33. }

Output

Some important points related to enum

o The enum names available in an enum type can have the same value. Let's look at the
example.

1. #include <stdio.h>
2.
3. int main(void) {
4. enum fruits{mango = 1, strawberry=0, apple=1};
5. printf("The value of mango is %d", mango);
6. printf("\nThe value of apple is %d", apple);
7. return 0;
8. }

Output

o If we do not provide any value to the enum names, then the compiler will
automatically assign the default values to the enum names starting from 0.
o We can also provide the values to the enum name in any order, and the unassigned
names will get the default value as the previous one plus one.
o The values assigned to the enum names must be integral constant, i.e., it should not
be of other types such string, float, etc.
o All the enum names must be unique in their scope, i.e., if we define two enum having
same scope, then these two enums should have different enum names otherwise
compiler will throw an error.

Let's understand this scenario through an example.

1. #include <stdio.h>
2. enum status{success, fail};
3. enum boolen{fail,pass};
4. int main(void) {
5.
6. printf("The value of success is %d", success);
7. return 0;
8. }

Output
o In enumeration, we can define an enumerated data type without the name also.

1. #include <stdio.h>
2. enum {success, fail} status;
3. int main(void) {
4. status=success;
5. printf("The value of status is %d", status);
6. return 0;
7. }

Output

Enum vs. Macro in C

o Macro can also be used to define the name constants, but in case of an enum, all the
name constants can be grouped together in a single statement.
For example,

# define pass 0;
# define success 1;

The above two statements can be written in a single statement by using the enum
type.
enum status{pass, success};

o The enum type follows the scope rules while macro does not follow the scope rules.
o In Enum, if we do not assign the values to the enum names, then the compiler will
automatically assign the default value to the enum names. But, in the case of macro,
the values need to be explicitly assigned.
o The type of enum in C is an integer, but the type of macro can be of any type.

You might also like