Hello,
I just wanted to know what does the enum and pragma do, and when are this commands used.
Thanks and
Best Regards,
BoSCHoW.
Printable View
Hello,
I just wanted to know what does the enum and pragma do, and when are this commands used.
Thanks and
Best Regards,
BoSCHoW.
#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)
So if I do something like this,
I actually did somethign 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
Thanks andCode: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
Best Regards,
BoSCHoW.
Hello bosChow,
enum values are constant, so it's more like "const int" not just "int".
:)
More likeQuote:
Originally Posted by BoSCHoW
Code:typedef int eMBErrorCode;
const eMBErrorCode MB_ENOERR = 0;
const eMBErrorCode MB_ENOREG = 1;
etc
Ok, and when i declare a function like this:
I actually did something like this?Code:eMBErrorCode
eMBInit( eMBMode eMode, UCHAR ucSlaveAddress,
UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity );
Thanks andCode:const int
eMBInit( eMBMode eMode, UCHAR ucSlaveAddress,
UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity );
Best Regards,
BoSCHoW.
Not exactly! More like:Quote:
Originally Posted by BoSCHoW
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.Code:int
eMBInit( eMBMode eMode, UCHAR ucSlaveAddress,
UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity );