美文网首页技术向IT面试c
C语言面试题收集

C语言面试题收集

作者: 程序熊大 | 来源:发表于2014-07-04 10:01 被阅读1625次

参考:

  1. C语言十二道有趣的面试题(1)
  2. C语言十二道有趣的面试题(2)
  3. exit()和_exit()的区别
  4. C语言泛型链表的实现、Linux内核中的链表实现
  5. strcpy、strncpy、strcpy_s的比较
  6. C语言函数参数处理顺序
  7. C语言高级程序设计

问题摘录:

  • free函数的操作参数必须是申请同一片内存空间时malloc返回的起始地址,否则出错。问题和实例代码如下:下面的程序会在用户输入'freeze'的时候出问题,而'zebra'则不会,为什么?

<pre>#include <stdio.h>

include <stdlib.h>

include <string.h>

int main(int argc, char *argv[])
{
char ptr = (char)malloc(10);

if(NULL == ptr) 
{ 
    printf("\n Malloc failed \n"); 
    return -1; 
} 
else if(argc == 1) 
{ 
    printf("\n Usage  \n"); 
} 
else 
{ 
    memset(ptr, 0, 10); 

    // strncpy(dest,source,num):dest指向目标缓存、source指向源缓存、
    // num为要拷贝的字节数,如果num超过dest的空间,则报错
    strncpy(ptr, argv[1], 9);

    while(*ptr != 'z') 
    { 
        if(*ptr == ' ') 
            break; 
        else 
            ptr++; 
    } 

    if(*ptr == 'z') 
    { 
        printf("\n String contains 'z'\n"); 
        // Do some more processing 
    } 

   free(ptr); 
} 

return 0; 

} </pre>

  • _exit()、exit()、atexit()函数的区别:
    _exit():直接进入内核,属于POSIX标准
    exit(): ANSI C标准。先做一些清理工作,例如关闭打开的文件,将缓存区的数据写回到文件中,最后再调用_exit()。
    atexit():C语言可以利用atexit(func)注册函数func,作为在main函数结束之后的一些操作,其中func为void类型。
    在下面这段代码中,func并没有被调用,就是因为这里用的是_exit()。
    <pre>#include<stdio.h>

void func(void)
{
printf("\n Cleanup function called \n");
return;
}

int main(void)
{
int i = 0;

atexit(func); 

for(;i<0xffffff;i++); 

_exit(0); 

}</pre>

  • 在《C语言十二题part1》中,第二题的答案,想法挺好,但是实际运行的时候,并没有他说的那种效果。代码如下:当输入超过10个字符的时候,编译器会检查出错误~

<pre>#include <stdio.h>

include <stdlib.h>

include <string.h>

int main(int argc, char *argv[])
{
int flag = 0;
char passwd[10];

// memset(dest,c,sizeof(dest)):将dest指向的缓存全部置为c
memset(passwd,0,sizeof(passwd)); 

//char *p = (char*)malloc(10);
//strcpy(passwd, argv[1]); 
scanf("%s",passwd);

if(0 == strcmp("LinuxGeek", passwd)) 
{ 
    flag = 1; 
} 

if(flag) 
{ 
    printf("\n Password cracked \n"); 
} 
else 
{ 
    printf("\n Incorrect passwd \n"); 

} 
return 0; 

} </pre>

  • *和++的优先级一样, 前置++和后置++的不同

<pre>#include<stdio.h>

int main(void)
{
char ptr = "Linux";
printf("\n [%c] \n",
ptr++);
//printf("\n [%c] \n",++ptr);
printf("\n [%c] \n",
ptr);

return 0; 

} </pre>

  • 常量字符串不能修改,C风格字符串的知识点

<pre>#include<stdio.h>

int main(void)
{
char *ptr = "Linux";
*ptr = 'T'; // 错误,此处ptr指向的是常量字符串,它的值是常量字符串的首字符的地址,即L的地址,而这句话试图修改L为T,发生错误

printf("\n [%s] \n", ptr); 

return 0; 

} </pre>

  • 能够修改自己名字的进程,哈哈,有点意思,修改argv[0]即可。

<pre>#include<stdio.h>

int main(int argc, char *argv[])
{
int i = 0;
char buff[100];

memset(buff,0,sizeof(buff));  // 初始化buff

strncpy(buff, argv[0], sizeof(buff));  //将原来的进程名拷贝到buff中
memset(argv[0],0,strlen(buff));  // 将argv[0]置为0

strncpy(argv[0], "NewName", 7);  //将新名字拷贝进argv[0]中

// Simulate a wait. Check the process 
// name at this point. 
for(;i<0xffffffff;i++); 

return 0; 

} </pre>

  • 变量的生存周期和函数的值传递性质

<pre>#include<stdio.h>

int* inc(int val)
{
int a = val;
a++;
return &a;
//严重漏洞,不可返回本地变量a的地址,因为a的生命周期仅仅是在inc这个函数中
}

int main(void)
{
int a = 10;
int *val = inc(a);
printf("\n Incremented value is equal to [%d] \n", *val);

return 0; 

} </pre>

  • printf()函数的参数是从右往左处理,因为参数一般为从右往左压栈的,这是因为C语言为了支持可变参数而确立的一个特性!
    <pre>#include<stdio.h>

int main(void)
{
int a = 10, b = 20, c = 30;
printf("\n %d..%d..%d \n", a+b+c, (b = b2), (c = c2));

return 0; 

}</pre>

输出结果如下:

<pre>110..40..60 </pre>

相关文章

  • C语言面试题收集

    参考: C语言十二道有趣的面试题(1) C语言十二道有趣的面试题(2) exit()和_exit()的区别 C语言...

  • 【C语言】C语言错误收集

    1.0 error: #70: incomplete type is not allowed 问题描述: 在调试P...

  • iOS常见面试题总结

    1.objective-c常见面试题:1、**OC **语言的基本特点OC 语言是 C 语言的一个超集,只是在 C...

  • C语言面试54题

    C语言面试54题 ​ 大家好,这期呢,我们谈一下c语言的面试题。 第1题,c语言有哪些核心的特征? 可移植性很强。...

  • C++语言的15个晦涩特性

    这个列表收集了C++ 语言的一些晦涩(Obscure)特性,是我经年累月研究这门语言的各个方面收集起来的。C++非...

  • C++语言的一些晦涩特性!(一)

    这个列表收集了C++ 语言的一些晦涩(Obscure)特性,是一些前辈经年累月研究这门语言的各个方面收集起来的。C...

  • iOS面试题及答案

    C语言相关面试题 1.static有什么用途? 答案:在C语言中,static主要定义全局静态变量,定义局部静态变...

  • iOS面试题大全(附带答案)

    C语言相关面试题 1.static有什么用途? 答案:在C语言中,static主要定义全局静态变量,定义局部静态变...

  • 嵌入式面试题目

    C语言面试题目一、选择题1.考查类型转换main(){struct node{int a;int b;int c;...

  • sizeof与字节对齐

    参考 【面试题】sizeof引发的血案编译器与字节对齐c 语言字节对齐问题详解C/C++内存对齐内存存取粒度C和C...

网友评论

  • district10:应该是不错的题,但是 Markdown 吧代码里的 指针都毁了。。。不修改吗 :flushed:

本文标题:C语言面试题收集

本文链接:https://www.haomeiwen.com/subject/jnmjtttx.html