技术向IT面试c

C语言面试题收集

2014-07-04  本文已影响1625人  程序熊大

参考:

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

问题摘录:

<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>

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>

<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>

<pre>#include<stdio.h>

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

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

return 0; 

} </pre>

<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>

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>

上一篇下一篇

猜你喜欢

热点阅读