y****e 发帖数: 23939 | 1 Which of the following statements describe the results of executing the
code snippet below in C++?
int var = 1;
void main() {
int i = i;
}
A. The i within main will have an undefined value.
B. The compiler will allow this statement, but the linker will not be able
to resolve the declaration of i.
C. The i within main will have a value of 1.
D. The compiler will not allow this statement.
E. The result is compiler-dependent.
正确答案应该是什么?为什么?
觉得A和E都对。 |
l*********s 发帖数: 5409 | |
f***c 发帖数: 338 | 3 学习中,坐等高手来讲课。
我在linux上试了,编译通过,加上cout<
哪个对呢? |
S**I 发帖数: 15689 | 4 A is correct for sure; E is somewhat correct (strictly speaking, compiler
doesn't need to handle this at all, so it is not correct to say "compiler-
dependent").
able
【在 y****e 的大作中提到】 : Which of the following statements describe the results of executing the : code snippet below in C++? : int var = 1; : void main() { : int i = i; : } : A. The i within main will have an undefined value. : B. The compiler will allow this statement, but the linker will not be able : to resolve the declaration of i. : C. The i within main will have a value of 1.
|
b*******s 发帖数: 5216 | 5 一个小疑问,main()在c/c++里面,一般要返回一个int的吧
able
【在 y****e 的大作中提到】 : Which of the following statements describe the results of executing the : code snippet below in C++? : int var = 1; : void main() { : int i = i; : } : A. The i within main will have an undefined value. : B. The compiler will allow this statement, but the linker will not be able : to resolve the declaration of i. : C. The i within main will have a value of 1.
|
S**I 发帖数: 15689 | 6 可以不写,默认返回0。
【在 b*******s 的大作中提到】 : 一个小疑问,main()在c/c++里面,一般要返回一个int的吧 : : able
|
b*******s 发帖数: 5216 | 7 但是可以用这种原型?
void main() ?
【在 S**I 的大作中提到】 : 可以不写,默认返回0。
|
b*******s 发帖数: 5216 | 8 觉得是d
void main()应该不能通过编译
able
【在 y****e 的大作中提到】 : Which of the following statements describe the results of executing the : code snippet below in C++? : int var = 1; : void main() { : int i = i; : } : A. The i within main will have an undefined value. : B. The compiler will allow this statement, but the linker will not be able : to resolve the declaration of i. : C. The i within main will have a value of 1.
|
S**I 发帖数: 15689 | 9 按标准来说,是应该通不过编译;不过某些编译器为了保持和legacy code的兼容,会
允许编译通过。
【在 b*******s 的大作中提到】 : 觉得是d : void main()应该不能通过编译 : : able
|
f***c 发帖数: 338 | 10 update, 昨天code测试直接用了int main()。
用gcc version 4.4.5 (Debian 4.4.5-8)编译不通过,“test.cpp:7: error: ‘::
main’ must return ‘int’”
所以答案是D。 |
|
|
m*******l 发帖数: 12782 | 11 wrong
【在 f***c 的大作中提到】 : update, 昨天code测试直接用了int main()。 : 用gcc version 4.4.5 (Debian 4.4.5-8)编译不通过,“test.cpp:7: error: ‘:: : main’ must return ‘int’” : 所以答案是D。
|
f***c 发帖数: 338 | 12 太简单了,请解释。谢谢。
但是我用g++编译确实没有通过。
【在 m*******l 的大作中提到】 : wrong
|
m*******l 发帖数: 12782 | 13 try 4.6.3 and compile again
【在 f***c 的大作中提到】 : 太简单了,请解释。谢谢。 : 但是我用g++编译确实没有通过。
|
n*******e 发帖数: 62 | 14
=== 错误应该是在这里!i没定义就用了,所以编译通不过。
able
【在 y****e 的大作中提到】 : Which of the following statements describe the results of executing the : code snippet below in C++? : int var = 1; : void main() { : int i = i; : } : A. The i within main will have an undefined value. : B. The compiler will allow this statement, but the linker will not be able : to resolve the declaration of i. : C. The i within main will have a value of 1.
|
S**I 发帖数: 15689 | 15 你的回复跟你的ID倒是很吻合;楼上的回复你都不看吗?
【在 n*******e 的大作中提到】 : : === 错误应该是在这里!i没定义就用了,所以编译通不过。 : able
|
S**I 发帖数: 15689 | 16 GCC只是若干编译器中的一种。
【在 f***c 的大作中提到】 : 太简单了,请解释。谢谢。 : 但是我用g++编译确实没有通过。
|
L*1 发帖数: 11537 | 17 这是真正CS应该知道的。吗工不管有没有CS学位,都不该关心这类事情,因为有的常识
的吗工都不会写这样的code。 |
l*********s 发帖数: 5409 | 18 yeah, the code compiles in CC, but this is so ridiculous...
【在 S**I 的大作中提到】 : GCC只是若干编译器中的一种。
|
m****7 发帖数: 69 | 19 result就是写这个代码的码农如果是有意的应该被开除了去 |
m*******l 发帖数: 12782 | 20 这到题其实考察好几个基本概念.你们真是的
【在 m****7 的大作中提到】 : result就是写这个代码的码农如果是有意的应该被开除了去
|
|
|
y****e 发帖数: 23939 | 21 大侠展开说说吧,正确答案应该是A和E吗?
【在 m*******l 的大作中提到】 : 这到题其实考察好几个基本概念.你们真是的
|
k**********g 发帖数: 989 | 22
正确答案是,如果你选A/E,interviewer会选D。如果你选D,interviewer会选A/E。
interviewer就是要你和他辩论。
【在 y****e 的大作中提到】 : 大侠展开说说吧,正确答案应该是A和E吗?
|
c*****n 发帖数: 75 | 23 这个问题的确考了一些平时自己想当然而没有深究的东西。
1) int i = i;
这个相当于
int i; //define and allocate storage on stack.
i = i; // assignment operator.
所以 i 是 uninitialized. 选 A)
2) void main()
研究了一下。 对main()的prototype, C/C++ 不同的年代的标准有不同的要求
C-90: 应该return int, 但允许compiler自由加入对其他return type的support.
也就是说, void main(), float main()都可能被你的compiler 支持。
C-99: 同 C-90
C++: 只允许 int main();
gcc 选择了支持 void main().
g++ 没得选, 不能支持 void main().
由以下结果也可看出结论:
gcc -std=c99 // ok;
gcc -pedantic -std=c99 // warning: return type of ‘main’ is not ‘int’ [-
Wmain]
g++ // error: ‘::main’ must return ‘int’
所以D 应为:The compiler **may or may not** allow this statement
E: 不知道这里的 result 指的是什么
=================================================================
int var = 1;
void main() {
int i = i;
}
A. The i within main will have an undefined value.
B. The compiler will allow this statement, but the linker will not be able
to resolve the declaration of i.
C. The i within main will have a value of 1.
D. The compiler will not allow this statement.
E. The result is compiler-dependent. |
t****t 发帖数: 6806 | 24 第一个就错了, "int i=i;" 和"int i; i=i;"在C++里是两个不同的东西. 第一个形式
是undefined (unless another "i" can be found in other scope), 因为
"... an initializer in the definition of a variable can consist of arbitrary
expressions involving literals and previously declared variables and
functions, regardless of the variable's storage duration." [8.5, clause 2]
注意, initializer expression must only involve "*previously* declared"
variables and functions...
当然, 我支持不应该考这些, 但是如果你要研究的话, 没有理由得到一个错误的结论.
【在 c*****n 的大作中提到】 : 这个问题的确考了一些平时自己想当然而没有深究的东西。 : 1) int i = i; : 这个相当于 : int i; //define and allocate storage on stack. : i = i; // assignment operator. : 所以 i 是 uninitialized. 选 A) : 2) void main() : 研究了一下。 对main()的prototype, C/C++ 不同的年代的标准有不同的要求 : C-90: 应该return int, 但允许compiler自由加入对其他return type的support. : 也就是说, void main(), float main()都可能被你的compiler 支持。
|
t****t 发帖数: 6806 | 25 对于main()的定义, C 和C++实际上是没差别的. C++和C一样允许freestanding
environment, 这时你也可以不定义main(). 但是在hosted environment, C和C++都要
求main()必须返回int.
【在 c*****n 的大作中提到】 : 这个问题的确考了一些平时自己想当然而没有深究的东西。 : 1) int i = i; : 这个相当于 : int i; //define and allocate storage on stack. : i = i; // assignment operator. : 所以 i 是 uninitialized. 选 A) : 2) void main() : 研究了一下。 对main()的prototype, C/C++ 不同的年代的标准有不同的要求 : C-90: 应该return int, 但允许compiler自由加入对其他return type的support. : 也就是说, void main(), float main()都可能被你的compiler 支持。
|
k****5 发帖数: 546 | 26 大牛指点一下,啥是freestanding environment? hosted environment?
【在 t****t 的大作中提到】 : 对于main()的定义, C 和C++实际上是没差别的. C++和C一样允许freestanding : environment, 这时你也可以不定义main(). 但是在hosted environment, C和C++都要 : 求main()必须返回int.
|
S**I 发帖数: 15689 | 27 "A hosted environment is one that provides the standard libraries. The
standard permits both hosted and freestanding environments. and goes to some
length to differentiate between them. Who would want to go without
libraries? Well, anybody writing 'stand alone' programs. Operating systems,
embedded systems like machine controllers and firmware for instrumentation
are all examples of the case where a hosted environment might be
inappropriate. Programs written for a hosted environment have to be aware of
the fact that the names of all the library functions are reserved for use
by the implementation. There is no such restriction on the programmer
working in a freestanding environment, although it isn't a good idea to go
using names that are used in the standard library, simply because it will
mislead readers of the program."
【在 k****5 的大作中提到】 : 大牛指点一下,啥是freestanding environment? hosted environment?
|
b*******s 发帖数: 5216 | 28 你让编译器生成下汇编代码你就知道怎么回事了
你说的明显不对
【在 n*******e 的大作中提到】 : : === 错误应该是在这里!i没定义就用了,所以编译通不过。 : able
|
t****t 发帖数: 6806 | 29 这个楼里, 只有新警察同学说的才是最正确的, 大家还都要嘲笑他.
【在 b*******s 的大作中提到】 : 你让编译器生成下汇编代码你就知道怎么回事了 : 你说的明显不对
|
n******t 发帖数: 4406 | 30 undefined behavior和编译通不过还是不一样的把。
当然int i =i 是吃饱了撑的没错。
【在 t****t 的大作中提到】 : 这个楼里, 只有新警察同学说的才是最正确的, 大家还都要嘲笑他.
|
|
|
m*******l 发帖数: 12782 | 31 void main根本就是不和标准的东西
【在 t****t 的大作中提到】 : 这个楼里, 只有新警察同学说的才是最正确的, 大家还都要嘲笑他.
|
t****t 发帖数: 6806 | 32 void main() can be allowed in freestanding environment. but int i=i is simpl
y ill-formed in any environment.
think about it.
【在 m*******l 的大作中提到】 : void main根本就是不和标准的东西
|
t****t 发帖数: 6806 | 33 是, 我其实说错了, int i=i;应该是ill-formed, 不是undefined.
【在 n******t 的大作中提到】 : undefined behavior和编译通不过还是不一样的把。 : 当然int i =i 是吃饱了撑的没错。
|
m*******l 发帖数: 12782 | 34 刚才午睡前翻书看到BS说这个问题,你说错了
arbitrary
【在 t****t 的大作中提到】 : 第一个就错了, "int i=i;" 和"int i; i=i;"在C++里是两个不同的东西. 第一个形式 : 是undefined (unless another "i" can be found in other scope), 因为 : "... an initializer in the definition of a variable can consist of arbitrary : expressions involving literals and previously declared variables and : functions, regardless of the variable's storage duration." [8.5, clause 2] : 注意, initializer expression must only involve "*previously* declared" : variables and functions... : 当然, 我支持不应该考这些, 但是如果你要研究的话, 没有理由得到一个错误的结论.
|
t****t 发帖数: 6806 | 35 嗯, 我又看了一遍, 是我说错了, declaration point starts immediately after dec
larator and before initializer. 所以i=i的确是indeterminated value. 谢谢.
【在 m*******l 的大作中提到】 : 刚才午睡前翻书看到BS说这个问题,你说错了 : : arbitrary
|
k**********g 发帖数: 989 | 36
dec
这问题果然是杀牛宝刀
【在 t****t 的大作中提到】 : 嗯, 我又看了一遍, 是我说错了, declaration point starts immediately after dec : larator and before initializer. 所以i=i的确是indeterminated value. 谢谢.
|