为C程序员准备的0x10个最佳问题

为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