C***H 发帖数: 508 | 1 某人今天问起,C语言中,如下定义:
#define A1 NULL
#define A2 NULL
#if (A1==NULL)
#undef A1
#define A1 t1
#elif (A2==NULL)
#undef A2
#define A2 t2
#endif
#if (A1==NULL)
#undef A1
#define A1 t1
#elif (A2==NULL)
#undef A2
#define A2 t2
#endif
编译完后,A1,A2分别是啥?
以为很直接,但实际上一试,发现结果和想象的不一样,没搞明白为什么,特来此一问
... |
t****t 发帖数: 6806 | 2 because t1 and NULL are both not defined.
【在 C***H 的大作中提到】 : 某人今天问起,C语言中,如下定义: : #define A1 NULL : #define A2 NULL : #if (A1==NULL) : #undef A1 : #define A1 t1 : #elif (A2==NULL) : #undef A2 : #define A2 t2 : #endif
|
C***H 发帖数: 508 | 3 我汗...没看懂你想说啥,这是编译,不是执行
【在 t****t 的大作中提到】 : because t1 and NULL are both not defined.
|
l*********s 发帖数: 5409 | 4 not t1 t2? What is the answer? |
C***H 发帖数: 508 | 5 A1 is t1
A2 is NULL
don't know why yet
【在 l*********s 的大作中提到】 : not t1 t2? What is the answer?
|
l*********s 发帖数: 5409 | 6 Two blocks are redundant?
【在 C***H 的大作中提到】 : A1 is t1 : A2 is NULL : don't know why yet
|
C***H 发帖数: 508 | 7 guess not, changed to the following is the same:
#define A1 NULL
#define A2 NULL
#if (A1==NULL)
#undef A1
#define A1 t1
#endif
#if (A1==NULL)
#undef A1
#define A1 t1
#elif (A2==NULL)
#undef A2
#define A2 t2
#endif
【在 l*********s 的大作中提到】 : Two blocks are redundant?
|
H***a 发帖数: 735 | 8 thrust说得对, 这是 t1, t2, NULL 都没有define过导致的.
C Standard 6.10.1说了preprocessor的条件判断必须是整型常数表达式, 如果是其他
类型表达式, 视之前是否defined过来设值: defined -> 1 ; undefined ->0
所以你第二次判断的时候 t1==NULL 解释成 0==0, 你的结果就不难理解了.
如果你在最开头加上
#define NULL 0
#define t1 1
#define t2 2
就没有问题, 对比一下就清楚了. |
l*********s 发帖数: 5409 | 9 I am curious. If t1 and t2 have not been defined, how did lz figure out A1
is T1, not NULL?
In addition, NULL is not predefined in C?
【在 H***a 的大作中提到】 : thrust说得对, 这是 t1, t2, NULL 都没有define过导致的. : C Standard 6.10.1说了preprocessor的条件判断必须是整型常数表达式, 如果是其他 : 类型表达式, 视之前是否defined过来设值: defined -> 1 ; undefined ->0 : 所以你第二次判断的时候 t1==NULL 解释成 0==0, 你的结果就不难理解了. : 如果你在最开头加上 : #define NULL 0 : #define t1 1 : #define t2 2 : 就没有问题, 对比一下就清楚了.
|
C***H 发帖数: 508 | 10 Great, this is exactly what I want to know, thanks a lot!
【在 H***a 的大作中提到】 : thrust说得对, 这是 t1, t2, NULL 都没有define过导致的. : C Standard 6.10.1说了preprocessor的条件判断必须是整型常数表达式, 如果是其他 : 类型表达式, 视之前是否defined过来设值: defined -> 1 ; undefined ->0 : 所以你第二次判断的时候 t1==NULL 解释成 0==0, 你的结果就不难理解了. : 如果你在最开头加上 : #define NULL 0 : #define t1 1 : #define t2 2 : 就没有问题, 对比一下就清楚了.
|
|
|
C***H 发帖数: 508 | 11 A1 will be replaced by t1, but t1==NULL for C preprocessor according to
thrust and Hyena
【在 l*********s 的大作中提到】 : I am curious. If t1 and t2 have not been defined, how did lz figure out A1 : is T1, not NULL? : In addition, NULL is not predefined in C?
|
l*********s 发帖数: 5409 | 12 I mean, how did you find out A1 is replaced by t1, if t1, t2, NULL are
undefined? Say, if T1 is defined as "hello", you can print out A1 and draw
the conclusion. But since all are undefined, you must be able to check the
symbol substitution by compilers directly, right?
【在 C***H 的大作中提到】 : A1 will be replaced by t1, but t1==NULL for C preprocessor according to : thrust and Hyena
|
t****t 发帖数: 6806 | 13 i think LZ means this section is pre-processed alone. not within a bigger
program -- of course that will be different.
【在 l*********s 的大作中提到】 : I mean, how did you find out A1 is replaced by t1, if t1, t2, NULL are : undefined? Say, if T1 is defined as "hello", you can print out A1 and draw : the conclusion. But since all are undefined, you must be able to check the : symbol substitution by compilers directly, right?
|
l*********s 发帖数: 5409 | 14 Let's say it is a standalone piece of code, how to find out what is being
substituted to what? Is there some option of compilers that returns this
kind of information?
【在 t****t 的大作中提到】 : i think LZ means this section is pre-processed alone. not within a bigger : program -- of course that will be different.
|
a**e 发帖数: 5794 | 15 看看你程序里的逻辑关系,走不到下面这行。
#define A2 t2
【在 C***H 的大作中提到】 : A1 is t1 : A2 is NULL : don't know why yet
|
H***a 发帖数: 735 | 16 gcc -E
【在 l*********s 的大作中提到】 : Let's say it is a standalone piece of code, how to find out what is being : substituted to what? Is there some option of compilers that returns this : kind of information?
|
H***a 发帖数: 735 | 17 It's just substitution.
Grabbed online "the preprocessor macro NULL is #defined (by or <
stddef.h>) with the value 0,
possibly cast to (void *)"
Here we still in preprocessor phase, neither nor is
included
【在 l*********s 的大作中提到】 : I am curious. If t1 and t2 have not been defined, how did lz figure out A1 : is T1, not NULL? : In addition, NULL is not predefined in C?
|
l*********s 发帖数: 5409 | 18 Thank you, Cong!
【在 H***a 的大作中提到】 : gcc -E
|