There are many useful C #define function, as an embedded software engineer, one need to know about some handy #define function. Below is list of some of the #defines which are being used
1. clear bit
#define clear_bit(val, bit) (val &= (~(1<<.bit))) usage: clear_bit(port0,0); clear zero bit of input is unsigned int
2. set bit
#define set_bit(val, bit) (val |= (1<<.bit)) usage: set_bit(port0,0); set zero bit of input is unsigned int
3. Error handling
#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
__LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
Usage:
if opening a file is failed like
if ((fd = open ("/dev/mem", O_RDWR | O_SYNC)) == -1)
FATAL;
1. clear bit
#define clear_bit(val, bit) (val &= (~(1<<.bit))) usage: clear_bit(port0,0); clear zero bit of input is unsigned int
2. set bit
#define set_bit(val, bit) (val |= (1<<.bit)) usage: set_bit(port0,0); set zero bit of input is unsigned int
3. Error handling
#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
__LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
Usage:
if opening a file is failed like
if ((fd = open ("/dev/mem", O_RDWR | O_SYNC)) == -1)
FATAL;
Comments