|
-
March 1st, 2008, 12:45 PM
#1
#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.
-
March 1st, 2008, 01:16 PM
#2
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)
-
March 1st, 2008, 02:05 PM
#3
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.
-
March 1st, 2008, 03:37 PM
#4
Re: #enum & #pragma in C
Hello bosChow,
enum values are constant, so it's more like "const int" not just "int".
-
March 1st, 2008, 09:09 PM
#5
Re: #enum & #pragma in C
 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
-
March 1st, 2008, 10:10 PM
#6
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.
-
March 2nd, 2008, 03:56 PM
#7
Re: #enum & #pragma in C
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|