鱼C论坛

 找回密码
 立即注册
查看: 6949|回复: 119

[技术交流] 这 6 道简单的 C 语言题,能看出你的基本功如何!敢挑战下??

  [复制链接]
发表于 2021-10-18 08:34:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 鱼C-小师妹 于 2021-10-27 11:16 编辑

在线讲解:




                               
登录/注册后可看大图


关注小师妹的童鞋们都应该知道,本仙女是“手写”代码狂人~



其实除了手写代码,本人还爱好刷题,哈哈哈哈哈哈哈,越来越秃,不对,越来越强!

论坛上的百题集(传送门)和课后作业(传送门),利扣,各大厂面试题等等资源,小师妹做了很多,也整理了很多资料,想要的扣1,超过 100 条,到时视频下方激活传送门!

接下来小师妹就从我的刷题小本本中,找到 6 道很基础,很面试,很坑人的东东~

有言在先:

  • 全部不会,赶紧回炉重造小甲鱼老师的 C 语言教程
  • 全会并答对,恭喜你,水平很棒!
  • 1-2道不会,看答案就会懂
  • 3-4道不会,看答案,不懂得赶紧去看对应的教程

好啦,能看到的这里的童鞋,就不要走啦!

不要打开编译器,全靠自己脑算或者手算出结果哦~

关门!放题!

27d7e2f0e51e46e6aa77a62c1c5d2bbc.gif

第 1 关:printf() 的参数

下面的代码输出结果:

  1. #include <stdio.h>
  2. int main()
  3. {
  4.         int a = 1, b = 2, c = 3; printf("\n %d - %d - %d \n", a+b+c, (b = b*2), (c = c*2));
  5.    return 0;
  6. }
复制代码

答案:
游客,如果您要查看本帖隐藏内容请回复


这一关就是考验你是否知道 C 语言里函数的参数默认是从右往左处理的,输出时是从左往右。


第 2 关:main() 的返回类型

下面这段代码可以顺利编译吗:

  1. #include<stdio.h>

  2. void main(void)
  3. {
  4.     char *ptr = (char*)malloc(10);

  5.     if(NULL == ptr)
  6.     {
  7.         printf("\n Malloc failed \n");
  8.         return;
  9.     }
  10.     else
  11.     {
  12.         // Do some processing
  13.         free(ptr);
  14.     }
  15.     return;
  16. }
复制代码

答案:

游客,如果您要查看本帖隐藏内容请回复



第 3 关:gets()

找出下面代码中的问题:

  1. #include<stdio.h>
  2. int main(void)
  3. {
  4.     char buff[10];
  5.     memset(buff,0,sizeof(buff));
  6.     gets(buff);
  7.     printf("\n The buffer entered is [%s]\n",buff);
  8.     return 0;
  9. }
复制代码

答案:

代码中的问题就在于gets() 函数的使用。

这个函数从 stdin 接收一个字符串而不检查它所复制的缓存的容积,这可能会导致缓存溢出。

这里推荐使用标准函数 fgets() 代替。


第 4 关:free()

下面的程序会在用户输入 freeze 的时候出问题,而 zfish 则不会,为什么?

  1. #include<stdio.h>
  2. int main(int argc, char *argv[])
  3. {
  4.     char *ptr = (char*)malloc(10);
  5.     if(NULL == ptr)
  6.     {
  7.         printf("\n Malloc failed \n");
  8.         return -1;
  9.     }
  10.     else if(argc == 1)
  11.     {
  12.         printf("\n Usage  \n");
  13.     }
  14.     else
  15.     {
  16.         memset(ptr, 0, 10);
  17.         strncpy(ptr, argv[1], 9);
  18.         while(*ptr != 'z')
  19.         {
  20.             if(*ptr == '')
  21.                 break;
  22.             else
  23.                 ptr++;
  24.         }
  25.         if(*ptr == 'z')
  26.         {
  27.             printf("\n String contains 'z'\n");
  28.             // Do some more processing
  29.         }
  30.        free(ptr);
  31.     }
  32.     return 0;
  33. }
复制代码

答案:

游客,如果您要查看本帖隐藏内容请回复



第 5 关:* 和 ++ 操作

下面的代码输出什么:

  1. #include<stdio.h>
  2. int main(void)
  3. {
  4.     char *ptr = "FishC";
  5.     printf("\n [%c] \n",*ptr++);
  6.     printf("\n [%c] \n",*ptr);

  7.     return 0;
  8. }
复制代码

答案:

游客,如果您要查看本帖隐藏内容请回复



第 6 关:内存泄露

下面的代码会导致内存泄漏吗:

  1. #include<stdio.h>
  2. void main(void)
  3. {
  4.     char *ptr = (char*)malloc(10);
  5.     if(NULL == ptr)
  6.     {
  7.         printf("\n Malloc failed \n");
  8.         return;
  9.     }
  10.     else
  11.     {
  12.         // Do some processing
  13.     }
  14.     return;
  15. }
复制代码

答案:

游客,如果您要查看本帖隐藏内容请回复


如果你想学习更多关于内存泄露的问题,请去看下面课程中的 P48 内存池教程。

这一次抽测,全部结束,留言告诉我们你做对了几道题呢?!

好啦,下课~




小甲鱼老师的课程:


评分

参与人数 2荣誉 +7 鱼币 +7 贡献 +6 收起 理由
不二如是 + 6 + 6 + 6 鱼C有你更精彩^_^
村里小黑 + 1 + 1 鱼C有你更精彩^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-18 08:55:20 From FishC Mobile | 显示全部楼层
考生1号小莫(doge
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-18 09:17:15 | 显示全部楼层
Xiao_Mo 发表于 2021-10-18 08:55
考生1号小莫(doge

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-18 10:38:18 | 显示全部楼层
本帖最后由 jhq999 于 2021-10-18 10:42 编辑

1、)6,4,6
2、)//
3、)
4、)指针加完就不是原来的指针地址了
5、)F、 i
6、)free
果然败在各种顺序上了,和不仔细上。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-18 10:56:19 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-18 11:12:52 | 显示全部楼层
6 4 6
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-18 11:25:46 | 显示全部楼层
11 - 4 - 6
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-18 11:43:18 | 显示全部楼层
第2关代码有问题呀,正常代码都被注释掉了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-18 12:26:32 | 显示全部楼层
  1. $ vim main.c
  2. $ cat main.c
  3. #include <stdio.h>
  4. int main()
  5. {
  6.         int a = 1, b = 2, c = 3; printf("\n %d - %d - %d \n", a+b+c, (b = b*2), (c = c*2));
  7.    return 0;
  8. }
  9. $ clang -g -Wall -o main main.c
  10. main.c:4:73: warning: unsequenced modification and access to 'b' [-Wunsequenced]
  11.         int a = 1, b = 2, c = 3; printf("\n %d - %d - %d \n", a+b+c, (b = b*2), (c = c*2));
  12.                                                                 ~       ^
  13. main.c:4:84: warning: unsequenced modification and access to 'c' [-Wunsequenced]
  14.         int a = 1, b = 2, c = 3; printf("\n %d - %d - %d \n", a+b+c, (b = b*2), (c = c*2));
  15.                                                                   ~                ^
  16. 2 warnings generated.
  17. $ ./main

  18. 6 - 4 - 6
  19. $
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-18 12:28:23 | 显示全部楼层
  1. $ cat main.c
  2. #include <stdio.h>
  3. void main()
  4. {
  5.         char *ptr = (char*)malloc(10);
  6.         if(NULL == ptr){printf("\n Malloc failed \n");return;}
  7.         else{// Do some processingfree(ptr); }
  8.    return 0;
  9. }
  10. $ clang -g -Wall -o main main.c
  11. main.c:2:1: warning: return type of 'main' is not 'int' [-Wmain-return-type]
  12. void main()
  13. ^
  14. main.c:2:1: note: change return type to 'int'
  15. void main()
  16. ^~~~
  17. int
  18. main.c:4:28: warning: implicitly declaring library function 'malloc' with type 'void *(unsigned long)' [-Wimplicit-function-declaration]
  19.         char *ptr = (char*)malloc(10);
  20.                            ^
  21. main.c:4:28: note: include the header <stdlib.h> or explicitly provide a declaration for 'malloc'
  22. main.c:7:4: error: void function 'main' should not return a value [-Wreturn-type]
  23.    return 0;
  24.    ^      ~
  25. main.c:8:2: error: expected '}'
  26. }
  27. ^
  28. main.c:3:1: note: to match this '{'
  29. {
  30. ^
  31. 2 warnings and 2 errors generated.
  32. $
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-18 12:37:23 | 显示全部楼层


我记得书上说不同的编译器不同的版本可能对参数的求值顺序不一样,
所以用 gcc 10.3.0和 vs 2022 pre试了,确实是跟答案一样,clang倒是没试,结果看见你试的,还真不一样
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-18 12:40:48 | 显示全部楼层
hrpzcf 发表于 2021-10-18 12:37
我记得书上说不同的编译器不同的版本可能对参数的求值顺序不一样,
所以用 gcc 10.3.0和 vs 2022 pre ...

我也记得是这样的
入栈顺序是从右到左,求值顺序未定义
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-18 12:46:11 | 显示全部楼层
人造人 发表于 2021-10-18 12:40
我也记得是这样的
入栈顺序是从右到左,求值顺序未定义


我也去试了一下,这是怎么回事,难道跟系统有关
  1. PS F:\Repos\ccode> clang code.c -o code.exe   
  2. code.c:5:48: warning: unsequenced modification and access to 'b' [-Wunsequenced]
  3.     printf("\n %d - %d - %d \n", a + b + c, (b = b * 2), (c = c * 2));
  4.                                      ~         ^
  5. code.c:5:61: warning: unsequenced modification and access to 'c' [-Wunsequenced]
  6.     printf("\n %d - %d - %d \n", a + b + c, (b = b * 2), (c = c * 2));
  7.                                          ~                  ^
  8. 2 warnings generated.
  9. PS F:\Repos\ccode> ./code

  10. 11 - 4 - 6
  11. PS F:\Repos\ccode>
复制代码

  1. PS C:\Users\hrp\Desktop> clang --version
  2. clang version 13.0.0
  3. Target: x86_64-pc-windows-msvc
  4. Thread model: posix
  5. InstalledDir: C:\LLVM\bin
  6. PS C:\Users\hrp\Desktop>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-18 12:47:37 | 显示全部楼层
hrpzcf 发表于 2021-10-18 12:46
我也去试了一下,这是怎么回事,难道跟系统有关
  1. $ clang --version
  2. clang version 8.0.1 (tags/RELEASE_801/final)
  3. Target: x86_64-unknown-windows-cygnus
  4. Thread model: posix
  5. InstalledDir: /usr/bin
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-18 12:49:35 | 显示全部楼层
hrpzcf 发表于 2021-10-18 12:46
我也去试了一下,这是怎么回事,难道跟系统有关

我的另一个版本也是
  1. $ ./main

  2. 6 - 4 - 6
  3. $ clang --version
  4. clang version 12.0.1
  5. Target: x86_64-pc-linux-gnu
  6. Thread model: posix
  7. InstalledDir: /usr/bin
  8. $
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-18 12:50:57 | 显示全部楼层
hrpzcf 发表于 2021-10-18 12:46
我也去试了一下,这是怎么回事,难道跟系统有关

你有没有装虚拟机,在虚拟机中试试
或者用 cygwin 试试
我用的这两个
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-18 12:59:42 | 显示全部楼层
人造人 发表于 2021-10-18 12:50
你有没有装虚拟机,在虚拟机中试试
或者用 cygwin 试试
我用的这两个


应该是跟Target有关
  1. hrp@local:/mnt/c/Users/hrp$ cd /mnt/f/Repos/ccode
  2. hrp@local:/mnt/f/Repos/ccode$ clang code.c -o code
  3. code.c:5:48: warning: unsequenced modification and access to 'b' [-Wunsequenced]
  4.     printf("\n %d - %d - %d \n", a + b + c, (b = b * 2), (c = c * 2));
  5.                                      ~         ^
  6. code.c:5:61: warning: unsequenced modification and access to 'c' [-Wunsequenced]
  7.     printf("\n %d - %d - %d \n", a + b + c, (b = b * 2), (c = c * 2));
  8.                                          ~                  ^
  9. 2 warnings generated.
  10. hrp@local:/mnt/f/Repos/ccode$ ./code

  11. 6 - 4 - 6
  12. hrp@local:/mnt/f/Repos/ccode$ clang --version
  13. clang version 10.0.0-4ubuntu1
  14. Target: x86_64-pc-linux-gnu
  15. Thread model: posix
  16. InstalledDir: /usr/bin
  17. hrp@local:/mnt/f/Repos/ccode$
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-18 13:00:42 | 显示全部楼层
hrpzcf 发表于 2021-10-18 12:59
应该是跟Target有关

也许是吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-18 13:36:53 | 显示全部楼层
  1. $ cat main.c
  2. #include<stdio.h>
  3. int main(int argc, char *argv[])
  4. {
  5.         char *ptr = (char*)malloc(10);
  6.     if(NULL == ptr)
  7.     {
  8.         printf("\n Malloc failed \n");
  9.         return -1;
  10.     }
  11.     else if(argc == 1)
  12.     {
  13.         printf("\n Usage  \n");
  14.     }
  15.     else
  16.     {
  17.         memset(ptr, 0, 10);
  18.         strncpy(ptr, argv[1], 9);
  19.         while(*ptr != 'z')
  20.         {
  21.             if(*ptr == '')
  22.                 break;
  23.             else
  24.                 ptr++;
  25.         }
  26.         if(*ptr == 'z')
  27.         {
  28.             printf("\n String contains 'z'\n");
  29.             // Do some more processing
  30.         }
  31.        free(ptr);
  32.     }
  33.     return 0;
  34. }
  35. $ gcc -g -Wall -o main main.c
  36. main.c: In function ‘main’:
  37. main.c:4:28: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration]
  38.     4 |         char *ptr = (char*)malloc(10);
  39.       |                            ^~~~~~
  40. main.c:2:1: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’
  41.     1 | #include<stdio.h>
  42.   +++ |+#include <stdlib.h>
  43.     2 | int main(int argc, char *argv[])
  44. main.c:4:28: warning: incompatible implicit declaration of built-in function ‘malloc’ [-Wbuiltin-declaration-mismatch]
  45.     4 |         char *ptr = (char*)malloc(10);
  46.       |                            ^~~~~~
  47. main.c:4:28: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’
  48. main.c:16:9: warning: implicit declaration of function ‘memset’ [-Wimplicit-function-declaration]
  49.    16 |         memset(ptr, 0, 10);
  50.       |         ^~~~~~
  51. main.c:2:1: note: include ‘<string.h>’ or provide a declaration of ‘memset’
  52.     1 | #include<stdio.h>
  53.   +++ |+#include <string.h>
  54.     2 | int main(int argc, char *argv[])
  55. main.c:16:9: warning: incompatible implicit declaration of built-in function ‘memset’ [-Wbuiltin-declaration-mismatch]
  56.    16 |         memset(ptr, 0, 10);
  57.       |         ^~~~~~
  58. main.c:16:9: note: include ‘<string.h>’ or provide a declaration of ‘memset’
  59. main.c:17:9: warning: implicit declaration of function ‘strncpy’ [-Wimplicit-function-declaration]
  60.    17 |         strncpy(ptr, argv[1], 9);
  61.       |         ^~~~~~~
  62. main.c:17:9: note: include ‘<string.h>’ or provide a declaration of ‘strncpy’
  63. main.c:17:9: warning: incompatible implicit declaration of built-in function ‘strncpy’ [-Wbuiltin-declaration-mismatch]
  64. main.c:17:9: note: include ‘<string.h>’ or provide a declaration of ‘strncpy’


  65. main.c:20:24: error: empty character constant
  66.    20 |             if(*ptr == '')
  67.       |                        ^~


  68. main.c:30:8: warning: implicit declaration of function ‘free’ [-Wimplicit-function-declaration]
  69.    30 |        free(ptr);
  70.       |        ^~~~
  71. main.c:30:8: note: include ‘<stdlib.h>’ or provide a declaration of ‘free’
  72. main.c:30:8: warning: incompatible implicit declaration of built-in function ‘free’ [-Wbuiltin-declaration-mismatch]
  73. main.c:30:8: note: include ‘<stdlib.h>’ or provide a declaration of ‘free’
  74. $
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-18 13:43:41 | 显示全部楼层
因为 ++ 和 * 的优先权一样。

++ 优先级高于 *
++ 1 级
* 2 级

1.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 22:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表