Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1124914
  • 博文数量: 241
  • 博客积分: 4385
  • 博客等级: 上校
  • 技术积分: 2383
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-07 23:13
文章分类

全部博文(241)

文章存档

2013年(1)

2012年(8)

2011年(62)

2010年(109)

2009年(61)

分类: C/C++

2009-09-16 13:45:44

第一章   程序设计基本概念

1i+++j等价于(i++)+j

2x=x+1x+=1x++,哪个效率高?为什么?

答:

x=x+1最低

(1)读取右x的地址

(2)x+1

(3)读取左x的地址

(4)将右值传给左边的x(编译器并不认为左右x的地址相同)

x+=1其次

(1)读取右x的地址

(2) x+1

(3) 将右值传给x(因为x的地址已经读出)

x++最优

(1)读取右x的地址

(2) x自增1

3:下面程序的结果是什么?

char foo(void)

{

       unsigned int a = 6;

       int b = -20;

       char c;

       (a +b > 6)       ?(c = 1):(c = 0);

       return c;

}

答:int->unsigned int->long->double

因此a+b不是-14,而是一个unsigned int类型的大数4294967382,所以c等于1

4:找出int a, b这两个数当中较大的,且不用if、?:、switch等判断语句

答:int max = ((a+b)+abs(a-b))/2,其中:abs()函数在头文件中

5:如何将ab的值进行交换,并且不使用任何中间变量?

答:

(1)

a=a+b;

b=a-b;

a=a-b

(2)

a=a^b;

b=a^b;

a=a^b;

6C编译时程序定义了_STDC_C++编译时程序定义了_cplusplus

7main函数执行完毕后,是否可能会再执行一段代码?

答:可以利用atexit()函数,atexit()函数注册程序正常终止时要被调用的函数。

代码:

#include

#include

int atexit(void (*function)(void));

using namespace std;

 

void fn(void);

 

int main()

{

       atexit(fn);

       cout<<"First!"<

       return 0;

}

 

void fn(void)

{

       cout<<"Second!"<

}

打印结果:

First!

Second!

阅读(929) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~