C语言复习问题总结

发布在 C语言

C语言复习问题总结


1.字段结构的成员的地址

改错题:

1
2
3
4
struct _half_byte {
unsigned short hb0: 4, hb1: 4, hb2: 4, hb3: 4;
} m;
scanf("%hu %hu %hu %hu", &m.hb0, &m.hb1, &m.hb2, &m.hb3);

这里需要注意到字段结构的成员是没有地址的,因此scanf("%hu %hu %hu %hu", &m.hb0, &m.hb1, &m.hb2, &m.hb3);是错误的,正确的代码可以类似:

1
2
3
unsigned short tmp[4];
scanf(“%hu %hu %hu %hu”, &tmp[0] , &tmp[1] , &tmp[2] , &tmp[3]);
m.hb0 = tmp[0], m.hb1 = tmp[1] , m.hb2 = tmp[2] , m.hb3 = tmp[3];

2.&&和||的编译器优化

如果开启了编译器优化,那么若&&的第一个条件为假或者||的第一个条件为真,那么编译器将不会去看&&或||的第二个条件,因此:

Declare int x = 0, y = 1; , which values of the following expressions are nonzero?

A. x++ + y--

B. y-x||x++, x

C. x*y&&y--, y

D. y-- ? y++ : y

这道题目的B和C为例,B中y-x已经为1,所以编译器将忽略x++,所以x仍然为0,所以B错误。而C中x*y为0所以编译器将忽略y–,所以y为1,C正确。

3.注意标识符和关键字的区别

例如:main是一个合法的标识符,而非关键字。

再例如:Case是一个合法的标识符,而非关键字。(注意C语言区分大小写)

4.词法分析

How many tokens are there in the statement of *p+++=12.L;?

A. 5

B. 6

C. 7

D. 10

记号共有5类:标识符、关键字、常量(含字符串常量)、运算符、标点符号。

*p+++=12.L;可以被分解为 * p ++ += 12.L ;共计6个记号,所以,答案为B。

5.转义序列

根据文档:

Escape sequences are used to represent certain special characters within string literals and character constants.

在C中可以由 \(反斜杠)+ 字符来进行转义。

6.类型转换

long x, y; x = -6L; y = 5UL; Which expression is equal to 1?

A. x<y && -6L<5UL

B. x<y && -6L>5UL

C. x>y && -6L<5UL

D. x>y && -6L>5UL

x,y的类型为long,因此5UL将会被转换为5L,所以x<y。而在进行比较运算的时候-6L会被提升至Unsigned Long,此时-6L>5UL,所以答案选B。

7.后缀自增自减运算符的计算延迟

后缀式++/–在遇到&& || ?: , 运算符后、或者完整表达式结束后自增/自减。

8.注意作用域、链接和存储时期

作用域描述程序中可以访问一个标识符的一个或多个区域;链接包括外部、内部和空链接,具有空链接的变量被代码块或函数原型所私有,外部链接的变量可以在多文件程序的任何地方使用,内部链接的变量可以在一个文件的任何地方使用;静态存储时期的变量将在程序执行期间一直存在,并且只能初始化一次。

具体可以参考C Primer Plus第十二章《存储类、链接和内存管理》或是课本。以下列出5个存储类的组合(来自C Primer Plus):
















































存储类 时期 作用域 链接 声明方式
自动 自动 代码块 代码块内
寄存器 自动 代码块 代码块内,使用关键字register
具有外部链接的静态 静态 文件 外部 所有函数之外
具有内部链接的静态 静态 文件 内部 所有函数之外,使用关键字static
空链接的静态 静态 代码块 代码块内,使用关键字static

In the following descriptions about the using of static, which ones are correct?

A. The static declaration can be used to variables and functions.

B. The external static declaration limits the scope of that object.

C. Internal static variables are local to a particular function just as auto variables are.

D. Internal static variables provide permanent storage and are initialized only once.

A正确,B external static具有内部链接,因此被限制了只能被当前文件引用,正确,C内部定义的静态变量具有代码块作用域,正确,D正确。

同时在另外一个文件中引用具有外部链接的变量时需要使用关键字 extern,因此,下面一道题选D。

Suppose that file_a.c and file_b.c can be compiled independently. and they share the following global variables

extern int x; char ch;

which are declared in file_a.c. The allowed global variables declared in file_b.c are

A、extern int x; char ch;

B、extern int x; extern char ch;

C、int x; char ch;

D、int x; extern char ch;

9.改错题注意悬挂指针

例如:

1
2
3
4
char *a[5];
int i = 0;
for (i=0; i<5; i++)
scanf("%s", a[i]);

10.Switch中的break

若switch中的case不加break,将会将该case后的所有语句全部执行完。

11.字段成员的内存分配

字段成员的内存分配是由低位到高位的。例如下面的题目,答案为C:

Suppose declared:

1
2
3
4
5
6
7
struct direction {
unsigned short int east:4,south:4,west:4,north:4; //east为最低位4,north为最高位1
};
union ud{
unsigned short int all;
struct direction d;
} a={0x1234};

the output of printf("%d\n",a.d.south); is

A、1

B、2

C、3

D、4

12.细心审题

C语言考试审题还是很重要的,稍微一不小心看错了一个字母,有可能题目就错了,务必仔细审题。

评论和共享

iOS学习笔记(二)

发布在 iOS开发

因为一些原因,用了整整一周的时间来重构项目,因为期末考试的原因,决定暑假继续这个项目,在复习之前最后总结一下这一段开发过程中遇到的一些问题。

定位问题


参考博客:后台定位上传的代码实践|里脊串的开发随笔

需求是每隔一定时间向服务器上传一次地理位置,而不管用户或系统是否杀死了APP。这里参考了里脊串博客里的写法,使用BackgroundMode中的Location updates即可实现。但是与这篇博客中的APP的需求不同,并不需要考虑速度距离等因素,只要考虑时间即可,因此可以通过判断两次location中的时间戳间隔即可。

1
2
3
4
5
#define kUpdateTimeInteval 60.0 //上传间隔
@interface LocationManager()
@property (nonatomic, strong) CLLocation * lastLocation; //最新一次的地理位置
@property (nonatomic) double timeInteval; //上一次上传地理位置时的时间戳
@end

然后在locationManager的委托方法中加入如下代码:

1
2
3
4
5
6
7
8
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
_lastLocation = locations[0];
if (_lastLocation.timestamp.timeIntervalSince1970 - _timeInteval > kUpdateTimeInteval) {
NSLog(@"update location");
[self updateLocation]; //上传地理位置
_timeInteval = _lastLocation.timestamp.timeIntervalSince1970;
}
}

需要注意的是,在进入后台模式或者是APP被杀的情况下,如果设备的位置没有发生变化,那么locationManager的 didUpdateLocations方法将不会被调用,因此尽管间隔的时间已经超过了设定的时间,地理位置也不会被上传。

不显示动画


由于APP中存在用户注册和登录的需求,因此需要加入验证码认证,两次验证码之间应有一分钟的间隔,此时,UI应该每隔1s刷新一次剩余时间以提醒用户,因此我使用了一个每隔1s执行一次的NSTimer去刷新UI,却发现每次刷新的时候按钮上的Title都会有渐入渐出的动画效果。后来发现这是UIButtonTypeRoundedRect的自带动画效果,为了不让这个渐变动画干扰了UI的刷新,加入如下代码:

1
2
3
4
[UIView performWithoutAnimation:^{
//在这里设置UIButton的属性如Text等
[_button.layer layoutIfNeeded];
}];

自定义UIBarButtonItem


参考了StackOverFlow

需求是自定义一个同时带图片和文字的UIBarButtonItem,查阅文档和搜索后发现UIBarButtonItem有以下的方法:

- (instancetype)initWithCustomView:(UIView *)customView;

因此我们可以先实例化一个UIButton,给这个UIButton添加Image并且设置Title,再用这个UIButton去实例化一个UIBarButtonItem即可,代码如下:

1
2
3
4
5
6
7
8
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"返回" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:17.0];
[button setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
button.imageEdgeInsets = UIEdgeInsetsMake(0, -3, 0, 0); //使用这个属性控制UIImageView和UILabel之间的距离
[button sizeToFit]; //设定button的frame
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * leftButton = [[UIBarButtonItem alloc]initWithCustomView:button];

但是这样又发现一个新的问题,我们无法控制UIBarButtonItem的边距。官方文档UINavigationItem中对于leftBarButtonItems和rightBarButtonItems这两个属性中有如下描述:

leftBarButtonItems are placed in the navigation bar left to right with the first item in the list at the left outside edge and left aligned.

rightBarButtonItems are placed right to left with the first item in the list at the right outside edge and right aligned.

因此我们可以再实例化一个UIBarButtonItem并设置它的width,完整代码如下:

1
2
3
4
5
6
7
8
9
10
11
UIBarButtonItem * negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -16.0; //可根据实际需要调整
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"返回" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:17.0];
[button setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
button.imageEdgeInsets = UIEdgeInsetsMake(0, -3, 0, 0);
[button sizeToFit];
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * leftButton = [[UIBarButtonItem alloc]initWithCustomView:button];
self.navigationItem.leftBarButtonItems = @[negativeSpacer, leftbutton];

去除UINavigationBar的阴影


我采用的是如下的方法:

[nav.navigationBar setShadowImage:[[UIImage alloc]init]];

评论和共享

  • 第 1 页 共 1 页
作者的图片

码龙黑曜

iOS开发者/计算机科学/兽人控


华中科技大学 本科在读


Wuhan, China