1.计算
#include<stdio.h>
int main()
{
int a,b,c,d;
a = 2;
b = a++;
c = ++a;
d =10 * a++;
printf("b:%d,c:%d,d:%d",b,c,d);
return 0;
}
答:b=2,c=4,d=40;
2.计算
a.~0 = -1
b.!0 = 1
c.2&4 = 0
d.2&&4 = 1
e.2|4 = 6
f.2||4 = 1
g.5<<3 = 40
#include<stdio.h>
struct bem
{
int limbs;
char type[30];
};
int main()
{
struct bem *pb;
struct bem deb = {6,"arcturan"};
pb = &deb;
printf("%s\n",deb.type);
printf("%s\n",pb->type);
printf("%s\n",deb.type+2);
printf("%s\n",pb->type+2);
return 0;
}
4.下面的代码里,假设在32位系统下,各sizeof计算的结果分别是多少???
char a [100];
sizeof (a)的值是 100
sizeof (a [50])的值是 1
sizeof (&a)的值是 4
sizeof (&a [0])的值是 4
5.
int a [10] = {0,1,2, 3, 4, 5, 6, 7, 8,9};
int *p = a;
*(p+7) = 7
6、
int a; //(全局未初始化区 )
int b = 10; //(全局初始化区)
char *p1;
ma i n ()
int c; //(栈)
char s[] = "abc"; //(栈)
char *p2; //(栈)
char *p3 = "123456”; //123456在常量区
static int c =0; //0在常量区
pl = (char *)mal loc(10); //分配得来得10字节的区域就在堆区。
以上程序编译后,简要说明上面各个变量的保存空间。
7.定义一个宏,输入两个参数,返回较大的参数.
#define max(a,b) (((a)>(b))?(a):(b))
8.预处理#define 定义 SECONDS_PER_YEAR 计算一年多少秒
#include <stdio.h>
#define UL (unsigned long)
#define SECONDS_PER_YEAR UL( 60 * 60 * 24 * 365 )
int main(void)
{
printf( "%lu\n", SECONDS_PER_YEAR );
return 0;
}
9.已知一个数组array,用一个宏定义数组个数:
#define num (sizeof(array)/sizeof(array[0]))
10.声明一个指向函数的指针。该函数的返回值是一个char指针,参数为一个char指针和一个char值
char * (*pfun) (char *, char);
11.在 aaa. c:int g_rec_count = 10;bbb. c中如果需要引用g_rec_count,需要怎样声明?
答:extern int g_rec_count ;
12.给定一个整型变量a,写两段代码,第一个设置a的bit 5,第二个清除a的bit 5。在以上两个操 作中,要保持其它位不变,把下面函数补充完整.
#define BIT5 (0x01 << 5)
static int a;
void set_bit3(void) {
a |= BIT5;
}
void clear_bit3(void) {
a &= ~BIT5;
}
13、有一个函数,功能是在/home目录下创建一个文件log. txt,可以保存一个字符串类型的msg,返回值 是int类型,把函数写完整.
int msg_save(char *msg)
(
FILE* fp;
char fname [] = "/home/1 og. txt”;
fp = fopen(fname,"W+");
fprintf(fp,"%s",msg);
return 0;
)
14、请解释一下“内存泄漏",内存泄漏形成的原因和会产生什么后果?
15.Interfaces that manage hardware resources have Open and Close functions. The Open function usual ly takes an index that enumerates the hardware resource as a Settings structure for initial configuration。