Thread: typedef enum statement confusion.

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    21

    typedef enum statement confusion.

    Hey all, thanks for the help. This should be fairly easy for someone who understands this syntax well. I am modifying some code for work, but ran into a little confusion with the statement below, cannot find what i need online. I understand typedef and enum alone, but the syntax here is throwing me off. Doesn't enum need a declaration of a variable name after it, before the list? And does typedef before enum cause each on to be an individual variable type? Either case does not make much sense. I just two statements of code with this syntax usage, but it appears much more so it is vital that I know what is going on. Thanks much.

    Code:
    typedef enum {
      CLK_PRESCALER_HSIDIV1   = (u8)0x00, /*!< High speed internal clock prescaler: 1 */
      CLK_PRESCALER_HSIDIV2   = (u8)0x08, /*!< High speed internal clock prescaler: 2 */
      CLK_PRESCALER_HSIDIV4   = (u8)0x10, /*!< High speed internal clock prescaler: 4 */
      CLK_PRESCALER_HSIDIV8   = (u8)0x18, /*!< High speed internal clock prescaler: 8 */
      CLK_PRESCALER_CPUDIV1   = (u8)0x80, /*!< CPU clock division factors 1 */
      CLK_PRESCALER_CPUDIV2   = (u8)0x81, /*!< CPU clock division factors 2 */
      CLK_PRESCALER_CPUDIV4   = (u8)0x82, /*!< CPU clock division factors 4 */
      CLK_PRESCALER_CPUDIV8   = (u8)0x83, /*!< CPU clock division factors 8 */
      CLK_PRESCALER_CPUDIV16  = (u8)0x84, /*!< CPU clock division factors 16 */
      CLK_PRESCALER_CPUDIV32  = (u8)0x85, /*!< CPU clock division factors 32 */
      CLK_PRESCALER_CPUDIV64  = (u8)0x86, /*!< CPU clock division factors 64 */
      CLK_PRESCALER_CPUDIV128 = (u8)0x87  /*!< CPU clock division factors 128 */
    } CLK_Prescaler_TypeDef;
    
    /**
       * @brief  SWIM Clock divider.
       */
    typedef enum {
      CLK_SWIMDIVIDER_2 = (u8)0x00, /*!< SWIM clock is divided by 2 */
      CLK_SWIMDIVIDER_OTHER = (u8)0x01 /*!< SWIM clock is not divided by 2 */
    }CLK_SWIMDivider_TypeDef;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Daniel.Blesener
    Doesn't enum need a declaration of a variable name after it, before the list?
    I think you mean the enumeration name rather than a variable name. The answer is no: the enumeration is anonymous, but...

    Quote Originally Posted by Daniel.Blesener
    does typedef before enum cause each on to be an individual variable type?
    ... the typedef gives the enumeration a name, e.g., CLK_Prescaler_TypeDef. So yes, it is a type name.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Daniel.Blesener View Post
    Hey all, thanks for the help. This should be fairly easy for someone who understands this syntax well. I am modifying some code for work, but ran into a little confusion with the statement below, cannot find what i need online. I understand typedef and enum alone, but the syntax here is throwing me off. Doesn't enum need a declaration of a variable name after it, before the list?
    That's technically referred to as a tag and is optional.
    Quote Originally Posted by Daniel.Blesener View Post
    And does typedef before enum cause each on to be an individual variable type?
    Yep!

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    Thanks for the quick replies! I still do not understand what
    Code:
    CLK_Prescaler_TypeDef;
    means though? Does it need to be there? All the statements like this have something at the end similar.
    Why does the statement have
    Code:
    CLK_Prescaler_TypeDef;
    written outside of the parenthesis? Thanks!

    Last edited by Daniel.Blesener; 06-19-2012 at 12:18 PM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Daniel.Blesener View Post
    Thanks for the quick replies! I still do not understand what
    Code:
    CLK_Prescaler_TypeDef;
    means though?
    It's just a name for the new type you have created. Consider this:
    Code:
    typedef enum {small, medium, large} size;  /* now size is a new name for "enum {small, medium, large}" */
    size dimension;  /* makes dimension a variable of type size aka enum {small, medium, large} */
    Last edited by itCbitC; 06-19-2012 at 01:59 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you print a typedef enum?
    By -EquinoX- in forum C Programming
    Replies: 1
    Last Post: 10-19-2008, 08:50 PM
  2. typedef'd enum specifying type
    By trillianjedi in forum C Programming
    Replies: 4
    Last Post: 05-27-2008, 01:31 PM
  3. Confusion with typedef
    By capvirgo in forum C Programming
    Replies: 4
    Last Post: 02-19-2008, 07:21 PM
  4. need help with typedef enum
    By Lince in forum C Programming
    Replies: 4
    Last Post: 12-06-2006, 01:14 PM
  5. typedef enum question
    By .shifty in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2003, 04:43 AM