header - Preprocessor directives in C : macros using __LINE__ -


i find difficult understand working of macro defined of preprocessor directives.

the macro,

trxem_spi_begin() 

is defined of 2 preprocessor directives refereed 2 header files. firstly, wish state declaration of said macro.

#define trxem_spi_begin() st( trxem_port_out &= ~trxem_spi_sc_n_pin; nop();) 

as declaration of macro st () missing here, found defined in different header file , ti shown below.

#define st(x) { x } while (__line__ == -1) 

now after combining 2 macros, true definition of macro trxem_spi_begin() must be,

#define trxem_spi_begin() {  ( trxem_port_out &= ~trxem_spi_sc_n_pin; nop(); )  } while (__line__ == -1) 

this code written work inside microcontroler trxem_port_out, rxem_spi_sc_n_pin memory mapped registers , nop initiates instruction cycle nothing.

as per understanding, __line__ means line of code in c file __line__ lies. line can never equal -1. i.e. loopmust running only once provided __line__ can never placed in -1 place in .c file. put, -1 can never value of __line__.

therefore, believe do while() loop here unnecessary , same output have been achieved without using looping.

i not understand functioning of macro. appreciate if elaborate on it.

as per understanding, means line of code in c file __line__ lies. line can never equal -1. i.e. loopmust running once provided __line__ can never placed in -1 place in .c file. put, -1 can never return value __line__.

your understanding correct here. there make sure code runs once.

think of following scenario:

#define baz foo();bar(); 

now if do

if(some_cond) baz; 

this equivalent to:

if(some_cond) foo(); bar(); 

which possibly not want. change to:

#define baz {foo();bar();} 

this works fine if written if(some_cond) foo() else wow(); fail compilation if written if(some_cond) foo(); else wow();

so define baz as

/* no semicolon @ end */ #define baz {foo();bar();} while(condition_which_is_always_false) 

and can write natural code intuitive semicolon @ end.

in case, condition_which_is_always_false __line__ == -1


Comments