由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 这个结果是啥,为什么呢?
相关主题
弱问c++里有没有NULL这个keyword?what kind of language this is and how to change it? (转载)
深情的呼唤师傅们!C++做题做不出来啦!关于程序必须支持win and linux, 可不可以用class而不是#ifdef WIN32 ?
C++ template preprocessorhow to apply OOD to a code for both win and linux platform ?
问个C++问题,高手帮帮忙问低级问题
最基本的C语言编程问题请教谁来解释一下这个是compiler问题吗?
又一个初级问题: C++中多如牛毛的#define格式question for C++ constant
One question in C programmingC++ 的 问题
How to tell gcc stop compiling.Question on using ## in #define
相关话题的讨论汇总
话题: null话题: a1话题: a2话题: t1话题: define
进入Programming版参与讨论
1 (共1页)
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++中多如牛毛的#define格式what kind of language this is and how to change it? (转载)
One question in C programming关于程序必须支持win and linux, 可不可以用class而不是#ifdef WIN32 ?
How to tell gcc stop compiling.how to apply OOD to a code for both win and linux platform ?
进入Programming版参与讨论
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
1 (共1页)
进入Programming版参与讨论
相关主题
Question on using ## in #define最基本的C语言编程问题请教
一个简单的小问题又一个初级问题: C++中多如牛毛的#define格式
32/64编程怎么做才好呢One question in C programming
one question about operator deleteHow to tell gcc stop compiling.
弱问c++里有没有NULL这个keyword?what kind of language this is and how to change it? (转载)
深情的呼唤师傅们!C++做题做不出来啦!关于程序必须支持win and linux, 可不可以用class而不是#ifdef WIN32 ?
C++ template preprocessorhow to apply OOD to a code for both win and linux platform ?
问个C++问题,高手帮帮忙问低级问题
相关话题的讨论汇总
话题: null话题: a1话题: a2话题: t1话题: define