Enumeration using enum type, displaying an integer for the respective name

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server Standard Edition

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info:

To do: Enumeration using enum type, displaying an integer of the error code for the respective error string

To show: How to use typedef and enum in C example to give new name and enumerating the error codes

 

// typedef and enum

#include <stdio.h>

#include <stdlib.h>

 

// typedef and enum definition

typedef enum {

FailOpenDisk = 1,

PathNotFound,

FolderCannotBeFound,

FileCannotBeFound,

FailOpenFile,

FileCannotBeRead,

DataCorrupted

} ErrorCode;

 

int main(void)

{

ErrorCode MyErrorCode;

 

printf("Check the size of ErrorCode: %d bytes\n", sizeof(ErrorCode));

 

for(MyErrorCode=FailOpenDisk; MyErrorCode<=DataCorrupted; MyErrorCode++)

printf(" %d", MyErrorCode);

 

printf("\n");

return 0;

}

 

Output example:

 

Check the size of ErrorCode: 4 bytes

1 2 3 4 5 6 7

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index