Click to See Complete Forum and Search --> : #enum & #pragma in C


BoSCHoW
March 1st, 2008, 11:45 AM
Hello,

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

Thanks and
Best Regards,
BoSCHoW.

Lindley
March 1st, 2008, 12:16 PM
#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)

BoSCHoW
March 1st, 2008, 01:05 PM
So if I do something like this,

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 :

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.

potatoCode
March 1st, 2008, 02:37 PM
Hello bosChow,

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

Lindley
March 1st, 2008, 08:09 PM
So if I do something like this,

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 :

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

typedef int eMBErrorCode;
const eMBErrorCode MB_ENOERR = 0;
const eMBErrorCode MB_ENOREG = 1;
etc

BoSCHoW
March 1st, 2008, 09:10 PM
Ok, and when i declare a function like this:

eMBErrorCode
eMBInit( eMBMode eMode, UCHAR ucSlaveAddress,
UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity );


I actually did something like this?


const int
eMBInit( eMBMode eMode, UCHAR ucSlaveAddress,
UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity );


Thanks and
Best Regards,
BoSCHoW.

stephendoyle75
March 2nd, 2008, 02:56 PM
Ok, and when i declare a function like this:

eMBErrorCode
eMBInit( eMBMode eMode, UCHAR ucSlaveAddress,
UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity );


I actually did something like this?


const int
eMBInit( eMBMode eMode, UCHAR ucSlaveAddress,
UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity );




Not exactly! More like:

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.