学习中 C 相关一些试题和答案

解析用蓝色字体

1. 指针在任何情况下都可进行>, <, >=, <=, = =运算。(错误  )                          指针是无符号数,当它与负数比较时,可能会得到相反的结果

2. switch(c) 语句中c可以是int ,long,char ,float ,unsigned int 类型。    (F)                  错,不能用实形

3.#define print(x)   printf("the no, "#x",is ")。      (T)                             

在主函数中出现print(x) 就代替这一语句printf(”the no, ”#x”,is ”)  也就是凡是出现print(x) 的地方都用printf(”the no, ”#x”,is ”)这一语句代替    printf(”the no, ”#x”,is ”)这一语句的意思是输出the no, ”#x”,is

4. void getmemory(char **p, int num)
{ *p=(char *) malloc(num);}
void
test(void)
{ char
*str=NULL;
getmemory(&str,100);
strcpy(str,"hello");
printf(str);
}
运行test函数有什么结果?10分
答案:输出hello,但是发生内存泄漏。  new,malloc后没有delete,free

5、设int arr[]={6,7,8,9,10};
int
*ptr=arr;
*(ptr++)+=123;
printf("%d,%d",*ptr,*(++ptr));

答案:8,8。这道题目的意义不大,因为在不同的编译器里printf的参数的方向是不一样的,在vc6.0下是从有到左,这里先*(++ptr)
后*pt,于是结果为8,8

6、不使用库函数,编写函数int strcmp(char *source, char *dest) 相等返回0,不等返回-1;

int strcmp(char *source, char *dest)
{
 while( *source!=''&& *dest!=''&&(*source==*dest))
 {
  source ++;
  dest++;
 }
 return (*source )-(*dest)? -1:0;
}

原文地址:https://www.cnblogs.com/babysunnie/p/3203954.html