CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2007
    Posts
    90

    #enum & #pragma in C

    Hello,

    I just wanted to know what does the enum and pragma do, and when are this commands used.

    Thanks and
    Best Regards,
    BoSCHoW.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: #enum & #pragma in C

    #pragma is a way of setting compiler settings from within code. It is often compiler-specific, so don't rely on it too much without #ifdefs.

    There is no such thing as a compiler directive #enum, as far as I know; the language keyword enum is simply a way of referring to integers in a more readable manner when, for example, you're using them to represent states or options.

    if (option == 1)

    is less obvious than

    if (option == READ)

  3. #3
    Join Date
    Feb 2007
    Posts
    90

    Re: #enum & #pragma in C

    So if I do something like this,
    Code:
    typedef enum
    {
        MB_ENOERR,                  /*!< no error. */
        MB_ENOREG,                  /*!< illegal register address. */
        MB_EINVAL,                  /*!< illegal argument. */
        MB_EPORTERR,                /*!< porting layer error. */
        MB_ENORES,                  /*!< insufficient resources. */
        MB_EIO,                     /*!< I/O error. */
        MB_EILLSTATE,               /*!< protocol stack in illegal state. */
        MB_ETIMEDOUT                /*!< timeout error occurred. */
    } eMBErrorCode
    I actually did somethign like this :
    Code:
    typedef struct
    {
        int MB_ENOERR,                  /*!< no error. */
        int MB_ENOREG,                  /*!< illegal register address. */
        int MB_EINVAL,                  /*!< illegal argument. */
        int MB_EPORTERR,                /*!< porting layer error. */
        int MB_ENORES,                  /*!< insufficient resources. */
        int MB_EIO,                     /*!< I/O error. */
        int MB_EILLSTATE,               /*!< protocol stack in illegal state. */
        int MB_ETIMEDOUT                /*!< timeout error occurred. */
    } eMBErrorCode
    Thanks and
    Best Regards,
    BoSCHoW.

  4. #4
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: #enum & #pragma in C

    Hello bosChow,

    enum values are constant, so it's more like "const int" not just "int".

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: #enum & #pragma in C

    Quote Originally Posted by BoSCHoW
    So if I do something like this,
    Code:
    typedef enum
    {
        MB_ENOERR,                  /*!< no error. */
        MB_ENOREG,                  /*!< illegal register address. */
        MB_EINVAL,                  /*!< illegal argument. */
        MB_EPORTERR,                /*!< porting layer error. */
        MB_ENORES,                  /*!< insufficient resources. */
        MB_EIO,                     /*!< I/O error. */
        MB_EILLSTATE,               /*!< protocol stack in illegal state. */
        MB_ETIMEDOUT                /*!< timeout error occurred. */
    } eMBErrorCode
    I actually did somethign like this :
    Code:
    typedef struct
    {
        int MB_ENOERR,                  /*!< no error. */
        int MB_ENOREG,                  /*!< illegal register address. */
        int MB_EINVAL,                  /*!< illegal argument. */
        int MB_EPORTERR,                /*!< porting layer error. */
        int MB_ENORES,                  /*!< insufficient resources. */
        int MB_EIO,                     /*!< I/O error. */
        int MB_EILLSTATE,               /*!< protocol stack in illegal state. */
        int MB_ETIMEDOUT                /*!< timeout error occurred. */
    } eMBErrorCode
    Thanks and
    Best Regards,
    BoSCHoW.
    More like
    Code:
    typedef int eMBErrorCode;
    const eMBErrorCode MB_ENOERR = 0;
    const eMBErrorCode MB_ENOREG = 1;
    etc

  6. #6
    Join Date
    Feb 2007
    Posts
    90

    Re: #enum & #pragma in C

    Ok, and when i declare a function like this:
    Code:
    eMBErrorCode
    eMBInit( eMBMode eMode, UCHAR ucSlaveAddress,
                  UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity );
    I actually did something like this?

    Code:
    const int 
    eMBInit( eMBMode eMode, UCHAR ucSlaveAddress,
                  UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity );
    Thanks and
    Best Regards,
    BoSCHoW.

  7. #7
    Join Date
    Apr 2007
    Location
    Ireland
    Posts
    81

    Re: #enum & #pragma in C

    Quote Originally Posted by BoSCHoW
    Ok, and when i declare a function like this:
    Code:
    eMBErrorCode
    eMBInit( eMBMode eMode, UCHAR ucSlaveAddress,
                  UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity );
    I actually did something like this?

    Code:
    const int 
    eMBInit( eMBMode eMode, UCHAR ucSlaveAddress,
                  UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity );
    Not exactly! More like:
    Code:
    int 
    eMBInit( eMBMode eMode, UCHAR ucSlaveAddress,
                  UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity );
    If you have a "const int" return type, the const is quite meaningless here as the return value will be returned using "pass by value" semantics. This means that the value will be copied out to the calling function and so the destination variable does not need to be constant.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured