Thread: Enumerators

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    97

    Enumerators

    Hi everyone,

    I want to know if there is any practical difference (or advantage) between these two variable assignments regarding the enumerator.

    Code:
    enum names{
    Nick,
    George
    Hellen
    }name;
    
    name = George; // Variable Assignment #1
    
    int my_name = George; // Variable Assignment #2
    Thanks
    Nick

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It doesn't make much difference at all in C.

    They are just named constants of some integral type.
    So assigning one to another usually goes unremarked.
    Code:
    #include <stdio.h>
    
    enum colours {
      red,
      green,
      blue
    };
    
    enum fruit {
      apple,
      banana,
    };
    
    int main ( ) {
      enum fruit x = green;
      printf("%d\n",x);
    }

    C++ on the other hand makes each enum it's own distinct type, so the above code would fail.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    97
    Thanks Salem!! Really helpfull

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enumerators vs Magic no.s
    By manasij7479 in forum C++ Programming
    Replies: 11
    Last Post: 05-24-2011, 01:08 AM
  2. Enumerators as strings
    By Mario F. in forum C++ Programming
    Replies: 14
    Last Post: 07-19-2006, 01:26 PM
  3. How to use enumerators to store input
    By Rubiks14 in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2005, 09:43 PM
  4. darn enumerators...please help
    By Waldo2k2 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-08-2002, 07:11 PM
  5. needs lots of help with arrays,pointers, and enumerators
    By ssjnamek in forum C++ Programming
    Replies: 13
    Last Post: 11-30-2001, 05:18 PM

Tags for this Thread