程序员面试宝典第二版笔记(一)

1:i++;

 What will be the output of the following C code ? 

#include <stdio.h>

int main() {
int b = 3;
int arr[] = {6, 7, 8, 9, 10};
int *ptr = arr;
*(ptr++) += 123;
printf(
"%d, %d\n", *ptr, *(++ptr));
}


2:编程风格 

We have two pieces of code, which one do you prefer, and tell why ?

if ('A' == a) {
a
++;
}
B
if (a == 'A') {
a
++;
}

Answer:

1:C中printf计算参数时从右往左压栈的,即先计算*(++ptr)再计算*ptr,故为8 8

         


3:类型转换:

3.1

下面程序的输出结果是多少?

#include <iostream>
using namespace std;

int main() {
unsigned
char a = 0xA5;
unsigned
char b = ~a >> 4;
printf(
"%d\n", b);
return 0;
}

Answer:

2:A好,因为第一种写法,"=="误写为"=",由于不允许对常量赋值,编译器会报错。


3.2

下面程序的输出结果是多少?

#include <iostream>
using namespace std;

int main() {
float a = 1.0f;
cout
<< (int)a << endl; //a强制为整型
cout << &a << endl; //输出a的地址
cout << (int&)a << endl; //不加转换的取出a在内存中的值,将其转成整型输出。
cout << boolalpha << ( (int)a == (int&)a ) << endl;
//输出什么?
float b = 0.0f;
cout
<< (int)b << endl;
cout
<< &b << endl;
cout
<< (int&)b << endl;
cout << boolalpha << ( (int)b == (int&)b ) << endl;
   //输出什么?
return 0;
}

Answer:

3.1:245

  (1) unsigned char b = ~a >> 4;在计算时,编译器先把a和4的值转换为int(即所谓整数提升)后再进行计算,当计算结果

    出来后,再把结果转换为unsigned char赋值给b。

    (2) >> 优先级高于~

3.3

下面程序的输出结果是多少?

#include <stdio.h>

int main() {
unsigned
int a = 0xfffffff7;
unsigned
char i = (unsigned char)a;
char *b = (char*)&a;
printf(
"%08x, %08x\n", i, *b);
}

Answer:

3.2:false  true

/*
(int&)a,不经过转换, 直接得到a在内存单元的值,并将其转换成整数输出。
(int)a ,a在内存中的值转换成int类型
float类型在内存中存储的形式是 ,符号位 指数 尾数
由754标准:
阶码采用增码(该数补码的反符号),尾数采用原码
所以1.0f 在内存中的形式为
0011 1111 1000 0000 0000 0000 0000 0000
即0x3f800000,输出时为它的整数形式
int和float对1.0f的二进制表示不同,故输出false。
0在内存中的浮点表示和整型表示相同,输出true。
*/
   

4:与非或问题

下面程序的结果是多少?

#include <iostream>
using namespace std;
int main() {
int count = 0;
int m = 9999;
while (m) {
count
++;
m
= m & (m - 1);
}
cout
<< count << endl;
}

Answer

 3.3:000000f7, fffffff7

  unsigned int变量赋值给unsigned char时,发生字节截断(3位和高于3位的被丢弃,留下低两位).


5: a,b交换

There are two int variables:a and b, don't use "if", "? :", "switch" or other judgement statements, find out 

  the biggest one of the two numbers.

Answer:

4: 8

  m的二进制表示中1出现的个数,即为count的值
  每进行一次 m & (m - 1) 运算,去除最右边的一个1

     

6:C和C++的关系

Answer 5:

(1)int max = (a + b + abs(a - b)) / 2;

(2)

 

#include <cstdio>
using namespace std;

int main () {
int a, b;
while (scanf("%d %d", &a, &b) == 2) {
int c = a - b;
char *str[2] = {"a大", "b大"};
c
= unsigned(c) >> (sizeof(int) * 8 - 1);
/*
看了良久,得如下结论
int类型的c范围在【-2^31, 2^31 - 1】
if (c < 0) {
unsigned(c) 的值为 c + 2^32;
} else {
unsigned(c) 的值即为c的值;
}
unsigned占32位,所有c右移31位,不是0,就是1
if (a >= b) {
then c >= 0
unsigned(c) = c;属于【0,2^31 - 1】即右移后为0
} else {
then c < 0
c属于【-2^31, 0)
unsigned(c) = c + 2^32次 >= 2^31次,即右移后为1
}
*/
printf(
"%s\n", str[c]);
}
return 0;
}

6.1:在C++程序中调用被C编译器编译后的函数,为什么要加extern "C"?

6.2:头文件中的ifndef/define/endif是干什么用的

Answer:

6.1

C++支持重载函数,C语言不支持重载函数。函数被C++编译后在库函数中的名字与C语言的不同。

例:假如某函数原型为void foo(int x, int y).C编译后再库中的名字为_foo,

而C++编译器则会产生像_foo_int_int之类的名字。。。

6.2:防止该头文件被重复引用

作者: ¥忘%风 发表于 2011-03-21 22:25 原文链接

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架