z****e 发帖数: 2024 | 1 Which of the following statements correctly describe the code below in C++?
#define language 437 //Line 1
#if language < 400
#undef language //Line 2
#else //Line 3
#define language 850 //Line 4
#ifdef language //Line 5
printf("%d", language); //Line 6
#endif
#endif
A. An error or warning will occur on Line 6 because a macro cannot be used
as part of a preprocessor directive.
B. An error or warning will occur on Line 2 because #undef is not a valid
preprocessor directive.
C. An error or warning will occur on Line 4 because language has already
been defined.
D. If Line 1 is changed to #define language 300, Line 6 will print "850".
E. An error or warning will occur on Line 3 because #else can only be used
as the last conditional in the chain.
===================================================
我怎么觉得没一个对的呢?
C看着沾边,但是原因也不是"has already been defined.", 而是重定义时候的数值不
是437,如果是437,重定义没问题。但是这个“重定义”可能也就不算是“重定义”了。
有个大牛哥们说D,我怎么觉得执行不到,也打印不出呢?
至于A,放在main里边,完全没问题。
这题目肯定是有个答案, 而且是个多选题。
师傅们,弟子又迷途了,你们老人家都快出来渡我吧! |
z****e 发帖数: 2024 | 2 另外,到底什么是macro,什么是preprocessor directives |
b***i 发帖数: 3043 | 3 codepad.org
【在 z****e 的大作中提到】 : Which of the following statements correctly describe the code below in C++? : #define language 437 //Line 1 : #if language < 400 : #undef language //Line 2 : #else //Line 3 : #define language 850 //Line 4 : #ifdef language //Line 5 : printf("%d", language); //Line 6 : #endif : #endif
|
p***o 发帖数: 1252 | 4 g++/gcc都说是 C, 看起来也靠谱。
【在 z****e 的大作中提到】 : Which of the following statements correctly describe the code below in C++? : #define language 437 //Line 1 : #if language < 400 : #undef language //Line 2 : #else //Line 3 : #define language 850 //Line 4 : #ifdef language //Line 5 : printf("%d", language); //Line 6 : #endif : #endif
|
k**********g 发帖数: 989 | 5 C: 留意 "warning" 一字
基本上是考小心眼,很un-American |
l*********s 发帖数: 5409 | 6 Thanks !
【在 b***i 的大作中提到】 : codepad.org
|
z****e 发帖数: 2024 | 7 不错,如此, 非C莫属。
【在 k**********g 的大作中提到】 : C: 留意 "warning" 一字 : 基本上是考小心眼,很un-American
|
t****t 发帖数: 6806 | 8 actually it's an error, not warning.
[16.3 Macro replacement]
2 An identifier currently defined as an object-like macro may be redefined
by another #define preprocessing directive provided that the second
definition is an object-like macro definition and the two replacement lists
are identical, otherwise the program is ill-formed. Likewise, an identifier
currently defined as a function-like macro may be redefined by another #
define preprocessing directive provided that the second definition is a
function-like macro definition that has the same number and spelling of
parameters, and the two replacement lists are identical, otherwise the
program is ill-formed.
【在 k**********g 的大作中提到】 : C: 留意 "warning" 一字 : 基本上是考小心眼,很un-American
|
p***o 发帖数: 1252 | 9 Would ill-formed demand a compiling error?
lists
identifier
【在 t****t 的大作中提到】 : actually it's an error, not warning. : [16.3 Macro replacement] : 2 An identifier currently defined as an object-like macro may be redefined : by another #define preprocessing directive provided that the second : definition is an object-like macro definition and the two replacement lists : are identical, otherwise the program is ill-formed. Likewise, an identifier : currently defined as a function-like macro may be redefined by another # : define preprocessing directive provided that the second definition is a : function-like macro definition that has the same number and spelling of : parameters, and the two replacement lists are identical, otherwise the
|
t****t 发帖数: 6806 | 10 hmm....you are right, it's not required for error.
[1.4 Implementation compliance]
8 A conforming implementation may have extensions (including additional
library functions), provided they do not alter the behavior of any well-
formed program. Implementations are required to diagnose programs that use
such extensions that are ill-formed according to this International Standard
. Having done so, however, they can compile and execute such programs.
【在 p***o 的大作中提到】 : Would ill-formed demand a compiling error? : : lists : identifier
|