几套经典的C语言试题A
A1.
有结构AA,中间有cc域,比较这两种对cc引用的不同,并说明pp和qq各应如何定义:
pp->cc,
qq.cc
答:不同:当用箭头操作符时,它的左边应是一个结构指针;当用点操作符时,它的左边应是一个结构变量。
struct AA *pp;
struct AA qq;
A2.
比较 union 和 struct 的不同。
答:ARM 32bit platform
struct AA
{
int I;
short j;
long long k;
};
union AA
{
int I;
short j;
long long k;
};//结构体变量的成员占有不同的存储空间
//共用体变量的成员占有相同的起始存储空间,任一时刻,只有一个成员可见
A3.
编译和链接有什么不同?(如外部符号的处理)
答:
编译生成的是目标文件(object*.o);
编译过程中对于外部符号不做任何解释和处理。外部符号对应的就是“符号”
链接生成的是可执行程序
链接将会解释和处理外部符号。外部符号对应的是地址
A4.
请举例说明二维字符数组与一维字符指针数组的差异
答:
char strArray1[][10] = {“hello”, “world”};
char *strArray2[] = {“hello”, “world”};
二维字符数组的字符串占用固定的空间 .data
一维字符指针数组的字符串占用不固定的空间 .ro_data
A5.
头文件中经常看到类似于下面的定义,请问这些预处理指令的作用是?
#ifndef
__LCD_DRIVER__
#define
__LCD_DRIVER__
#undef
O_RDONLY
#define
O_RDONLY
0x0001
//
write your header here
__FILE__
#endif
答:
A6.
已知strcpy函数的函数原型是:char *strcpy(char *strDest, const char *strSrc)。其中,strDest是目的字符串,strSrc是源字符串。
1)不调用C++/C的字符串库函数,请编写函数strcpy
答:
typedef
enum {false = 0, true = 1} BOOL;
strcpy();
char *strcpy(char *strDest, const char *strSrc)
{
int i;
if(!(strDest && strSrc))
return;
ASSERT(strDest && strSrc);
while(strDest[i++] = *strSrc++);
return strDest;
}
2)strcpy能把strSrc的内容复制到strDest,为什么还要char *类型的返回值?
答:
为了实现链式表达式
int len = strlen(strcpy(strDest, strSrc));
A7.
下面程序中有一处语法错误和一处逻辑错误,请分别指出(直接在程序中标出)。
// 程序功能:搜索字符串中的非数字字符,并将非数字字符打印出来
#include <stdio.h>
#define SIZE 10;
// define 后面没有分号
bool IsNumber(char ch)
{
return( (ch <= '9') && (ch >= '0') );
}
int main(void)
{
char c[SIZE] = "12345y789";
int i;
bool Result;
for(i = 0; i < SIZE; i++)
{
Result = IsNumber(c);
if(Result == false)
{
printf("%c is not a number string/n", c);
}
}
return 0;
}
A8.
简述在C++程序中调用被C编译器编译后的函数,为什么要加extern “C”声明?
答:
A9.
已知一个函数原型:int foo(struct type_t1 a, struct type_t2 *b),请定义一个指向该原型函数的指针类型,并用这个新定义的类型声明一个元素个数为SIZE的指针数组。
答:
struct AA
{
int I;
} AA;
int a;
int *p = &a;
*p = 0;
void *f = func;
void (*f)(void);
void func(void)
{
}
typedf int INT;
INT a;
typedef int FOO_FUNCTION(struct type_t1 a, struct type_t2 *b);
(FOO_FUNCTION *)0xa0f00000;
int (*p)(struct type_t1 a, struct type_t2 *b)
foo *Array[SIZE];
int (*Array[SIZE])(struct type_t1 a, struct type_t2 *b);
A10.判断题(对的写T,错的写F并说明原因)
1)
有数组定义int a[2][2]={{1},{2,3}};则a[0][1]的值为0。(T)
2)int (*ptr) (),则ptr是一维数组的名字。(F)
ptr是一个函数指针
int function(void);
3)指针在任何情况下都可进行>, <, >=, <=, = =运算。(F 指针比较大小没有任何意义,但是可以比较==与!= )
4)switch(c) 语句中c可以是int, long, char, float, unsigned int 类型。(F)
5)#define print(x) printf("the no, "#x",is ") (T)
#宏
print(test);
printf(“the no, “ “test” “,is “);
“string1string2”
#define STRING_HELP_CONTENT “About:/nasdfasdf/nadf”
“About:/n” “asdfasdf/n”
-----------------------------------------------------------------------------------------
几套经典的C语言试题B
B1.
用预处理指令#define声明一个常数,用以表明1年中有多少秒(忽略闰年问题)
#define SECOND_PER_YEAR (365UL * 24UL * 60UL * 60UL)
B2.
写一个“标准”宏MIN,这个宏输入两个参数并返回较小的一个
#define MIN(a, b) (a) <= (b) ? (a) : (b)
temp = MIN(a + b, c + d);
temp = (a + b) <= (c + d) ? (a + b) : (c + d);
B3.
用变量a给出下面的定义
1)
一个整型数
int a;
2)
一个指向整型数的指针
int *a;
3)
一个指向指针的的指针,它指向的指针是指向一个整型数
typedef int *POINTER;
POINTER *a;
int **a;
4)
一个有10个整型数的数组
int a[10];
5)
一个有10个指针的数组,该指针是指向一个整型数的
int *a[10];
6)
一个指向有10个整型数数组的指针
typedef int POINTER[10];
POINTER *a;
7)
一个指向函数的指针,该函数有一个整型参数并返回一个整型数
typedef int POINTER(int);
POINTER *a;
8)
一个有10个指针的数组,该指针指向一个函数,该函数有一个整型参数并返回一个整型
typedef int POINTER(int);
POINTER *a[10]
B4.
关键字static的作用是什么
static用来修饰一个局部的变量的时候,
生命域是全局的
作用域是局部的
static用来修饰一个模块内的(某一个C的源程序文件)全局变量的时候
生命域不变
作用域减小,只在本模块内有效
static用来修饰一个函数的时候
作用域减小,只在本模块内有效
B5.
程序判断。
typedef char *POINTER;
void setmemory(POINTER *p, int num)
{
*p = (char *)malloc(num);
}
void test(void)
{
char *str=NULL;
setmemory(&str,100);
strcpy(str,”hello”);
printf(str);
}
运行test函数有什么结果?
答:
可以打印出来“hello”,但是会造成内存泄露
void setmemory(POINTER p, int num)
{
p = (char *)malloc(num);
}
void test(void)
{
char *str=NULL;
setmemory(str,100);
strcpy(str,”hello”);
printf(str);
}
不但不能打印“hello”,而且会造成内存泄露
B6.
设int arr[]={6,7,8,9,10};
int *ptr=arr;
*(ptr++) += 123;
// *(ptr)+=123;
// ptr++;
// arr={129, 7, 8, 9};
// ptr = &arr[1];
printf("%d,%d",*(++ptr),*(++ptr));
// ptr = &arr[2];
运行printf()函数的输出是什么?
答:8,8
B7.
不使用库函数,编写函数int strcmp(char *source, char *dest),相等返回0,不等返回-1。
答:
int strcmp(char *source, char *dest)
{
if(source && dest)
{
// compare
while((*source != ‘/0’) && (*source == *dest))
{
source++;
dest++;
}
return *source == *dest ? 0 : -1;
}
else
{
return source == dest ? 0 : -1;
}
}
B8.
有关关键字const,回答以下问题。
1)
关键字const是什么含义?
const表示“只读”。
2)
说明下面的声明的含义:
A.
const int a;
// a是一个常数
int const a;
// a是一个常数
B.
const int *a;
// a是一个指向整型常数的指针
int * const a;
// a是一个指向整型变量的常指针
int const * a const;
// a是一个指向整型常数的常指针
C.
char *strcpy(char *strDest, const char *strSrc);
// 参数在函数内部不会被修改
const int strcmp(char *source, char *dest);
// 函数的返回值不能被修改
const int a = strcmp(xx, yy);
if(strcmp(xx,yy) != 0)
B9.
说明关键字volatile有什么含意,并给出例子。
volatile表示被修饰的符号是易变的。告诉编译器不要随便优化我的代码!!
1)
一个硬件寄存器
2)
中断中用到的变量
3)
线程之间共享变量
volatile int a = 10;
while((a & 0x01) == 0);
#define P_UART_STATUS ((const volatile unsigned int *)0x88000000);
// volatile表示硬件会修改这个寄存器的内容
// const表示该寄存器只读,写无意义
B10.
嵌入式系统总是要用户对变量或寄存器进行位操作。给定一个整型变量a,写两段代码,第一个设置a的bit 3,第二个清除a 的bit 3。在以上两个操作中,要保持其它位不变。
#define BIT(n) (0x01 << (n))
#define BIT1 (0x01 << 1)
#define BIT2 (0x01 << 2)
#define BIT3 (0x01 << 3)
#define BIT4 (0x01 << 4)
#define BIT5 (0x01 << 5)
#define BIT6 (0x01 << 6)
#define BIT7 (0x01 << 7)
int a;
void SetBit3()
{
a |= BIT3;
// a |= 0x04
}
void ClearBit3()
{
a &= ~BIT3;
// a &= 0xfb
}