为C程序员准备的0x10个最佳问题
日期:2006-10-23 11:59:17 点击:64 作者: 来源:Paid King
Answer With Detailed Explanation
_____________________________________________________________ 数据挖掘实验室
Answer 1. 数据挖掘实验室
The answer is (b)
volatile variable isn"t affected by the optimization. Its value after the longjump is the last value variable assumed.
b last value is 5 hence 5 is printed.
setjmp : Sets up for nonlocal goto /* setjmp.h*/
Stores context information such as register values so that the lomgjmp function can return control to the statement following the one calling setjmp.Returns 0 when it is initially called.
Lonjjmp: longjmp Performs nonlocal goto /* setjmp.h*/
Transfers control to the statement where the call to setjmp (which initialized buf) was made. Execution continues at this point as if longjmp cannot return the value 0.A nonvolatile automatic variable might be changed by a call to longjmp.When you use setjmp and longjmp, the only automatic variables guaranteed to remain valid are those declared volatile. 数据挖掘研究院
Note: Test program without volatile qualifier (result may very)
Answer 2.
The answer is (a)
The members of structures have address in increasing order of their declaration. If a pointer to a structure is cast to the type of a pointer to its first member, the result refers to the first member.
Answer 3. 数据挖掘实验室
The answer is (a)
Non recursive version of the program 数据挖掘研究院
int what ( int x , int n) 数据挖掘实验室
{
int val;
int product;
product =1;
val =x; 数据挖掘研究院
while(n>0) 数据挖掘研究院
{ 数据挖掘研究院
if (n%2 == 1)
product = product*val;
n = n/2;
val = val* val; 数据挖掘研究院
} 数据挖掘实验室
} 数据挖掘研究院
/* Code raise a number (x) to a large power (n) using binary doubling strategy */
Algorithm description
(while n>0)
{
if next most significant binary digit of n( power) is one 数据挖掘研究院
then multiply accumulated product by current val , 数据挖掘研究院
reduce n(power) sequence by a factor of two using integer division .
get next val by multiply current value of itself 数据挖掘研究院
} 数据挖掘研究院
Answer 4.
The answer is (c)
type of a is array of int
type of &a is pointer to array of int

