Я пытаюсь написать некоторый код микроконтроллера, используя примеры Texas Instruments, и он везде использует макросы (вероятно, для уменьшения размера кода), некоторые из которых окружены st (). Прочитав комментарии, я все еще не понимаю, почему это необходимо или когда мне следует это использовать:
/*
* This macro is for use by other macros to form a fully valid C statement.
* Without this, the if/else conditionals could show unexpected behavior.
*
* For example, use...
* #define SET_REGS() st( ioreg1 = 0; ioreg2 = 0; )
* instead of ...
* #define SET_REGS() { ioreg1 = 0; ioreg2 = 0; }
* or
* #define SET_REGS() ioreg1 = 0; ioreg2 = 0;
* The last macro would not behave as expected in the if/else construct.
* The second to last macro will cause a compiler error in certain uses
* of if/else construct
*
* It is not necessary, or recommended, to use this macro where there is
* already a valid C statement. For example, the following is redundant...
* #define CALL_FUNC() st( func(); )
* This should simply be...
* #define CALL_FUNC() func()
*
* (The while condition below evaluates false without generating a
* constant-controlling-loop type of warning on most compilers.)
*/
#define st(x) do { x } while (__LINE__ == -1)
Можете ли вы привести несколько примеров того, что не получилось бы, если бы там не было st? Есть ли вред в добавлении st, где это не нужно?
Что может означать st
? Когда второй пример с { something }
приводит к ошибкам компилятора? Потому что это также используется в некоторых примерах кода.