instagram youtube
Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
logo
Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Enum Information Kind in C: What it’s and Use It

- Team

Sabtu, 8 Juni 2024 - 11:29

facebook twitter whatsapp telegram line copy

URL berhasil dicopy

facebook icon twitter icon whatsapp icon telegram icon line icon copy

URL berhasil dicopy


Enumeration or Enum in C is a unique more or less information sort explained via the person. It is composed of continuous integrals or integers which can be given names via a person. Using enum in C to call the integer values makes all the program simple to be informed, perceive, and take care of via the similar and even other programmer.

Desire a Most sensible Device Construction Task? Get started Right here!

Complete Stack Developer – MERN StackDiscover Program

Want a Top Software Development Job? Start Here!

Syntax to Outline Enum in C

An enum is explained via the usage of the ‘enum’ key phrase in C, and using a comma separates the constants inside of. The elemental syntax of defining an enum is:

enum enum_name

int myExample = ROTATE ;

Within the above syntax, the default price of int_const1 is 0, int_const2 is 1, int_const3 is two, and so forth. Then again, you’ll be able to additionally exchange those default values whilst pointing out the enum. Beneath is an instance of an enum named vehicles and the way you’ll be able to exchange the default values.

enum vehicles

int myExample = ROTATE ;

Right here, the default values for the constants are:

BMW=0, Ferrari=1, Jeep=2, and Mercedes-Benz=3. Then again, to switch the default values, you’ll be able to outline the enum as follows:

enum vehicles SAVE;

printf(“%d”, myExample);

go back 0;

;

Enumerated Kind Declaration to Create a Variable

Very similar to pre-defined information varieties like int and char, you’ll be able to additionally claim a variable for enum and different user-defined information varieties. Right here’s learn how to create a variable for enum.

enum situation (true, false); //pointing out the enum

enum situation e; //making a variable of sort situation

Think we’ve got declared an enum sort named situation; we will create a variable for that information sort as discussed above. We will additionally converge each the statements and write them as:

enum situation (true, false) e;

For the above remark, the default price for true will probably be 1, and that for false will probably be 0.

Create and Put into effect Enum in C Program

Now that we understand how to outline an enum and create variables for it, let’s have a look at some examples to know the way to enforce enum in C methods.

Additionally Learn: Maximum Vital Options of C Language You Should be Conscious Of

Instance 1: Printing the Values of Weekdays

#come with <stdio.h>  

    enum days SAVE;

printf(“%d”, myExample);

go back 0;

;  

int primary(){

    // printing the values of weekdays

    for(int i=Sunday;i<=Saturday;i++){

        printf(“%d, “,i);

    }

    go back 0;

}

Output:

Enum_in_C_1

Within the above code, we declared an enum named days consisting of the title of the weekdays ranging from Sunday. We then initialized the price of Sunday to be 1. This may occasionally assign the price for the opposite days as the former price plus 1. To iterate throughout the enum and print the values of on a daily basis, we’ve got created a for loop and initialized the price for i as Sunday.

Instance 2: Assigning and Fetching Customized Values of Enum Parts

#come with<stdio.h>

enum bins{

    cont1 = 5,

    cont2 = 7,

    cont3 = 3,

    cont4 = 8

};

int primary(){ 

    // Initializing a variable to carry enums

    enum bins cur_cont = cont2;

printf(“Price of cont2 is = %d n”, cur_cont);

cur_cont = cont3;

printf(“Price of cont3 is = %d n”, cur_cont);

cur_cont = cont1;

printf(“Price of hearts is = %d n”, cur_cont);

go back 0;

}

Output:

Enum_in_C_2.

We have now declared an enum named bins with 4 other bins as the weather within the above code. We have now then given customized values to the weather and initialized the variable for the enum a couple of instances to print the related output.

How To Use Anum in C?

We use enums for constants, i.e., when we would like a variable to have just a particular set of values. For example, for weekdays enum, there may also be most effective seven values as there are most effective seven days in per week. Then again, a variable can retailer just one price at a time. We will use enums in C for a couple of functions; one of the makes use of of enums are:

  • To retailer consistent values (e.g., weekdays, months, instructions, colours in a rainbow)
  • For the usage of flags in C
  • Whilst the usage of switch-case statements in C

Instance of The use of Enum in Transfer Case Commentary

On this instance, we can create an enum with the entire 4 instructions, North, East, West, and South because the constants. We can then use the swap case statements to change between the course parts and print the output in response to the price of the variable for the enum instructions.

#come with <stdio.h>  

enum instructions{North=1, East, West, South};  

int primary(){

    enum instructions d;

    d=West;

    swap(d){  

        case North:

        printf(“We’re headed against North.”);

        damage;

        case East:

        printf(“We’re headed against East.”);

        damage;

        case West:

        printf(“We’re headed against West.”);

        damage;

        case South:

        printf(“We’re headed against South”);

        damage;

    }

    go back 0;

}

Output:

Enum_in_C_3.

Desire a Most sensible Device Construction Task? Get started Right here!

Complete Stack Developer – MERN StackDiscover Program

Want a Top Software Development Job? Start Here!

Instance of The use of Enum in C for Flags

We will use enum in C for flags via holding the values of integral constants an influence of two. This may occasionally permit us to make a choice and mix two or extra flags with out overlapping with the assistance of the Bitwise OR (|) operator. Let’s imagine the instance beneath the place we set 3 flags: Crop, Rotate, and Save to paintings with a picture.

Instance:

#come with <stdio.h>

enum designFlags{

    CROP = 1,

ROTATE = 2,

SAVE = 4

};

int primary() SAVE;

printf(“%d”, myExample);

go back 0;

Output:

Enum_in_C_4.

If we do the calculations for the above code, it’s:

   00000010 (ROTATE = 2)

| 00000100 (SAVE = 4)

 ___________

   00000110 (Output = 6)

As you’ll be able to see, our calculation and the output given via this system are the similar. This concludes that we will use enum in C for flags. Additionally, we will upload our customized flags.

Attention-grabbing Issues About Initialization of Enum in C

There are a couple of details in regards to the enum value noting, reminiscent of:

1. A couple of enum names or parts could have the similar price. Right here’s an instance of 2 enum parts having a identical price.

Instance:

#come with <stdio.h>

enum Automobiles{Jeep = 1, BMW = 0, Mercedes_Benz = 0};

int primary(){

    printf(“%d, %d, %d”, Jeep, BMW, Mercedes_Benz);

    go back 0;

}

Output:

Enum_in_C_5

2. If we don’t assign customized values to enum parts, the compiler will assign them default values ranging from 0. For example, the compiler will assign values to the months within the instance beneath, with January being 0.

Instance:

#come with <stdio.h>

enum Months{January, February, March, April, Would possibly, June, July, August, September, October, November, December};

int primary(){

enum Months m = Would possibly;

    printf(“The Price of Would possibly in Months is %d”, m);

go back 0;

}

Output:

Enum_in_C_6

Desire a Most sensible Device Construction Task? Get started Right here!

Complete Stack Developer – MERN StackDiscover Program

Want a Top Software Development Job? Start Here!

3. We will supply values to any parts of enum in any order. The entire unassigned parts gets the price as earlier + 1. The next program demonstrates the similar.

Instance:

#come with <stdio.h>

enum weekdays {Sunday, Monday = 2, Tuesday, Wednesday = 6, Thursday, Friday = 9, Saturday = 12};

int primary()

{

printf(“%d %d %d %d %d %d %d”, Sunday, Monday, Tuesday,

Wednesday, Thursday, Friday, Saturday);

go back 0;

}

Output:

Enum_in_C_7

4. The entire values assigned to the weather of enum will have to be an integral consistent. For example, they must be throughout the vary of minimal and most conceivable integers.

5. The entire enum parts or constants must have a singular scope. It implies that a component can’t be part of two other enums in the similar program as it’s going to fail all over compilation. Right here’s an instance:

Instance:

#come with <stdio.h>

enum Automobiles{Mahindra, Jeep, BMW};

enum Luxury_Cars{BMW, Ferrari, Mercedes_Benz};

int primary(){

    go back 0;

}

Output:

Enum_in_C_8.

Enum in C vs. Macro

Very similar to enum, you’ll be able to additionally use the macro in C to outline named constants. Then again, using enum in C is most popular because it has many benefits over the macro. Two of the important thing advantages are:

  • Enums follows the ‘scope’ laws as mentioned previous (within the final level of the former segment).
  • The compiler can robotically assign values to enum parts. Therefore, the declaration turns into more uncomplicated.

Conclusion

On this article, you’ve gotten realized the whole lot about enum in C and learn how to use it another way. If you wish to know extra about C programming basics, you’ll be able to enroll on our SkillUp platform. The platform gives unfastened classes on a couple of programming languages, together with C and C++. You’ll take the chance to comb up to your C programming abilities and put them to follow.

In nowadays’s aggressive international, full-stack builders are in upper call for than execs with mastery in a selected language. Therefore, it’s of extreme significance to be informed and grasp a couple of programming languages. That’s precisely what our Complete Stack Developer – MERN Stack is made for. The route supplies tutorials and carried out finding out to get accustomed to 30+ programming abilities and equipment. Go for the route to develop into the most productive and land a high-paying task within the device building box.

FAQs

1. What’s an enum information sort in C?

Enum, brief for enumeration, is a user-defined information sort in C programming. It allows builders to ascertain a selection of named constants, referred to as enumerators, every related with an integer price. Enumerations supply a method to assign symbolic names to integral constants, a great deal improving code clarity and maintainability.

2. How do you outline an enum in C?

To outline an enum in C, you use the enum key phrase adopted via the designation of the enumeration. Inside of curly braces, enumerators, separated via commas, are indexed. Those enumerators constitute the named constants related to the enum. Each and every enumerator might optionally have an assigned price, in most cases ranging from 0 and incrementing via 1 for next enumerators until explicitly specified another way.

3. How are enums utilized in C programming?

Enums are repeatedly used to constitute a suite of comparable constants with significant names. They considerably support code clarity via offering descriptive labels for integer values. Enums are steadily hired in switch-case statements, serve as parameters, and knowledge constructions to make stronger program readability and group.

4. Can enums have particular integer values assigned to their parts?

Certainly, enums can possess explicitly assigned integer values for his or her parts. Whilst the preliminary enumerator is assigned the price 0 via default, next enumerators increment via 1. However, builders retain the versatility to assign values to particular person enumerators at once, thus making an allowance for custom designed integer assignments.

5. What are some great benefits of the usage of enums over macros in C?

Enums be offering notable benefits over macros. They be sure that sort protection, limiting enumerated values to a predefined set and mitigating attainable mistakes. Additionally, enums make stronger code clarity and maintainability via offering symbolic representations of constants, contrasting with the textual substitution mechanism of macros.

supply: www.simplilearn.com

Berita Terkait

Most sensible Recommended Engineering Tactics | 2025
Unfastened Flow Vs General Flow
Be told How AI Automation Is Evolving in 2025
What Is a PHP Compiler & The best way to use it?
Best Leadership Books You Should Read in 2024
Best JavaScript Examples You Must Try in 2025
How to Choose the Right Free Course for the Best Value of Time Spent
What Is Product Design? Definition & Key Principles
Berita ini 12 kali dibaca

Berita Terkait

Selasa, 11 Februari 2025 - 22:32

Revo Uninstaller Pro 5.3.5

Selasa, 11 Februari 2025 - 22:21

Rhinoceros 8.15.25019.13001

Selasa, 11 Februari 2025 - 22:12

Robin YouTube Video Downloader Pro 6.11.10

Selasa, 11 Februari 2025 - 22:08

RoboDK 5.9.0.25039

Selasa, 11 Februari 2025 - 22:05

RoboTask 10.2.2

Selasa, 11 Februari 2025 - 21:18

Room Arranger 10.0.1.714 / 9.6.2.625

Selasa, 11 Februari 2025 - 17:14

Team11 v1.0.2 – Fantasy Cricket App

Selasa, 11 Februari 2025 - 16:20

Sandboxie 1.15.6 / Classic 5.70.6

Berita Terbaru

Headline

Revo Uninstaller Pro 5.3.5

Selasa, 11 Feb 2025 - 22:32

Headline

Rhinoceros 8.15.25019.13001

Selasa, 11 Feb 2025 - 22:21

Headline

Robin YouTube Video Downloader Pro 6.11.10

Selasa, 11 Feb 2025 - 22:12

Headline

RoboDK 5.9.0.25039

Selasa, 11 Feb 2025 - 22:08

Headline

RoboTask 10.2.2

Selasa, 11 Feb 2025 - 22:05