这 6 道简单的 C 语言题,能看出你的基本功如何!敢挑战下??
本帖最后由 鱼C-小师妹 于 2021-10-27 11:16 编辑在线讲解:
https://www.bilibili.com/video/BV12v411u7Q5?spm_id_from=444.41.0.0
static/image/hrline/3.gif
关注小师妹的童鞋们都应该知道,本仙女是“手写”代码狂人~
https://www.bilibili.com/video/BV1HT4y1K7DY?spm_id_from=333.999.0.0
其实除了手写代码,本人还爱好刷题,哈哈哈哈哈哈哈,越来越秃,不对,越来越强!
论坛上的百题集(传送门)和课后作业(传送门),利扣,各大厂面试题等等资源,小师妹做了很多,也整理了很多资料,想要的扣1,超过 100 条,到时视频下方激活传送门!
接下来小师妹就从我的刷题小本本中,找到 6 道很基础,很面试,很坑人的东东~
有言在先:
[*]全部不会,赶紧回炉重造小甲鱼老师的 C 语言教程
[*]全会并答对,恭喜你,水平很棒!
[*]1-2道不会,看答案就会懂
[*]3-4道不会,看答案,不懂得赶紧去看对应的教程
好啦,能看到的这里的童鞋,就不要走啦!
不要打开编译器,全靠自己脑算或者手算出结果哦~
关门!放题!
第 1 关:printf() 的参数
下面的代码输出结果:
#include <stdio.h>
int main()
{
int a = 1, b = 2, c = 3; printf("\n %d - %d - %d \n", a+b+c, (b = b*2), (c = c*2));
return 0;
}
答案:
**** Hidden Message *****
这一关就是考验你是否知道 C 语言里函数的参数默认是从右往左处理的,输出时是从左往右。
第 2 关:main() 的返回类型
下面这段代码可以顺利编译吗:
#include<stdio.h>
void main(void)
{
char *ptr = (char*)malloc(10);
if(NULL == ptr)
{
printf("\n Malloc failed \n");
return;
}
else
{
// Do some processing
free(ptr);
}
return;
}
答案:
**** Hidden Message *****
第 3 关:gets()
找出下面代码中的问题:
#include<stdio.h>
int main(void)
{
char buff;
memset(buff,0,sizeof(buff));
gets(buff);
printf("\n The buffer entered is [%s]\n",buff);
return 0;
}
答案:
代码中的问题就在于gets() 函数的使用。
这个函数从 stdin 接收一个字符串而不检查它所复制的缓存的容积,这可能会导致缓存溢出。
这里推荐使用标准函数 fgets() 代替。
第 4 关:free()
下面的程序会在用户输入 freeze 的时候出问题,而 zfish 则不会,为什么?
#include<stdio.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(ptr, argv, 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;
}
答案:
**** Hidden Message *****
第 5 关:* 和 ++ 操作
下面的代码输出什么:
#include<stdio.h>
int main(void)
{
char *ptr = "FishC";
printf("\n [%c] \n",*ptr++);
printf("\n [%c] \n",*ptr);
return 0;
}
答案:
**** Hidden Message *****
第 6 关:内存泄露
下面的代码会导致内存泄漏吗:
#include<stdio.h>
void main(void)
{
char *ptr = (char*)malloc(10);
if(NULL == ptr)
{
printf("\n Malloc failed \n");
return;
}
else
{
// Do some processing
}
return;
}
答案:
**** Hidden Message *****
如果你想学习更多关于内存泄露的问题,请去看下面课程中的 P48 内存池教程。
这一次抽测,全部结束,留言告诉我们你做对了几道题呢?!
好啦,下课~
小甲鱼老师的课程:
https://www.bilibili.com/video/BV17s411N78s?spm_id_from=333.999.0.0
考生1号小莫(doge Xiao_Mo 发表于 2021-10-18 08:55
考生1号小莫(doge
{:10_275:} 本帖最后由 jhq999 于 2021-10-18 10:42 编辑
1、)6,4,6
2、)//
3、)
4、)指针加完就不是原来的指针地址了
5、)F、 i
6、)free
果然败在各种顺序上了,和不仔细上。 {:10_257:} 6 4 6 11 - 4 - 6 第2关代码有问题呀,正常代码都被注释掉了{:10_247:} $ vim main.c
$ cat main.c
#include <stdio.h>
int main()
{
int a = 1, b = 2, c = 3; printf("\n %d - %d - %d \n", a+b+c, (b = b*2), (c = c*2));
return 0;
}
$ clang -g -Wall -o main main.c
main.c:4:73: warning: unsequenced modification and access to 'b' [-Wunsequenced]
int a = 1, b = 2, c = 3; printf("\n %d - %d - %d \n", a+b+c, (b = b*2), (c = c*2));
~ ^
main.c:4:84: warning: unsequenced modification and access to 'c' [-Wunsequenced]
int a = 1, b = 2, c = 3; printf("\n %d - %d - %d \n", a+b+c, (b = b*2), (c = c*2));
~ ^
2 warnings generated.
$ ./main
6 - 4 - 6
$
$ cat main.c
#include <stdio.h>
void main()
{
char *ptr = (char*)malloc(10);
if(NULL == ptr){printf("\n Malloc failed \n");return;}
else{// Do some processingfree(ptr); }
return 0;
}
$ clang -g -Wall -o main main.c
main.c:2:1: warning: return type of 'main' is not 'int' [-Wmain-return-type]
void main()
^
main.c:2:1: note: change return type to 'int'
void main()
^~~~
int
main.c:4:28: warning: implicitly declaring library function 'malloc' with type 'void *(unsigned long)' [-Wimplicit-function-declaration]
char *ptr = (char*)malloc(10);
^
main.c:4:28: note: include the header <stdlib.h> or explicitly provide a declaration for 'malloc'
main.c:7:4: error: void function 'main' should not return a value [-Wreturn-type]
return 0;
^ ~
main.c:8:2: error: expected '}'
}
^
main.c:3:1: note: to match this '{'
{
^
2 warnings and 2 errors generated.
$
人造人 发表于 2021-10-18 12:26
我记得书上说不同的编译器不同的版本可能对参数的求值顺序不一样,
所以用 gcc 10.3.0和 vs 2022 pre试了,确实是跟答案一样,clang倒是没试,结果看见你试的,还真不一样{:10_247:} hrpzcf 发表于 2021-10-18 12:37
我记得书上说不同的编译器不同的版本可能对参数的求值顺序不一样,
所以用 gcc 10.3.0和 vs 2022 pre ...
我也记得是这样的
入栈顺序是从右到左,求值顺序未定义
人造人 发表于 2021-10-18 12:40
我也记得是这样的
入栈顺序是从右到左,求值顺序未定义
我也去试了一下,这是怎么回事,难道跟系统有关{:10_257:}
PS F:\Repos\ccode> clang code.c -o code.exe
code.c:5:48: warning: unsequenced modification and access to 'b' [-Wunsequenced]
printf("\n %d - %d - %d \n", a + b + c, (b = b * 2), (c = c * 2));
~ ^
code.c:5:61: warning: unsequenced modification and access to 'c' [-Wunsequenced]
printf("\n %d - %d - %d \n", a + b + c, (b = b * 2), (c = c * 2));
~ ^
2 warnings generated.
PS F:\Repos\ccode> ./code
11 - 4 - 6
PS F:\Repos\ccode>
PS C:\Users\hrp\Desktop> clang --version
clang version 13.0.0
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\LLVM\bin
PS C:\Users\hrp\Desktop> hrpzcf 发表于 2021-10-18 12:46
我也去试了一下,这是怎么回事,难道跟系统有关
$ clang --version
clang version 8.0.1 (tags/RELEASE_801/final)
Target: x86_64-unknown-windows-cygnus
Thread model: posix
InstalledDir: /usr/bin
hrpzcf 发表于 2021-10-18 12:46
我也去试了一下,这是怎么回事,难道跟系统有关
我的另一个版本也是
$ ./main
6 - 4 - 6
$ clang --version
clang version 12.0.1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
$
hrpzcf 发表于 2021-10-18 12:46
我也去试了一下,这是怎么回事,难道跟系统有关
你有没有装虚拟机,在虚拟机中试试
或者用 cygwin 试试
我用的这两个 人造人 发表于 2021-10-18 12:50
你有没有装虚拟机,在虚拟机中试试
或者用 cygwin 试试
我用的这两个
应该是跟Target有关
hrp@local:/mnt/c/Users/hrp$ cd /mnt/f/Repos/ccode
hrp@local:/mnt/f/Repos/ccode$ clang code.c -o code
code.c:5:48: warning: unsequenced modification and access to 'b' [-Wunsequenced]
printf("\n %d - %d - %d \n", a + b + c, (b = b * 2), (c = c * 2));
~ ^
code.c:5:61: warning: unsequenced modification and access to 'c' [-Wunsequenced]
printf("\n %d - %d - %d \n", a + b + c, (b = b * 2), (c = c * 2));
~ ^
2 warnings generated.
hrp@local:/mnt/f/Repos/ccode$ ./code
6 - 4 - 6
hrp@local:/mnt/f/Repos/ccode$ clang --version
clang version 10.0.0-4ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
hrp@local:/mnt/f/Repos/ccode$ hrpzcf 发表于 2021-10-18 12:59
应该是跟Target有关
也许是吧
$ cat main.c
#include<stdio.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(ptr, argv, 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;
}
$ gcc -g -Wall -o main main.c
main.c: In function ‘main’:
main.c:4:28: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration]
4 | char *ptr = (char*)malloc(10);
| ^~~~~~
main.c:2:1: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’
1 | #include<stdio.h>
+++ |+#include <stdlib.h>
2 | int main(int argc, char *argv[])
main.c:4:28: warning: incompatible implicit declaration of built-in function ‘malloc’ [-Wbuiltin-declaration-mismatch]
4 | char *ptr = (char*)malloc(10);
| ^~~~~~
main.c:4:28: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’
main.c:16:9: warning: implicit declaration of function ‘memset’ [-Wimplicit-function-declaration]
16 | memset(ptr, 0, 10);
| ^~~~~~
main.c:2:1: note: include ‘<string.h>’ or provide a declaration of ‘memset’
1 | #include<stdio.h>
+++ |+#include <string.h>
2 | int main(int argc, char *argv[])
main.c:16:9: warning: incompatible implicit declaration of built-in function ‘memset’ [-Wbuiltin-declaration-mismatch]
16 | memset(ptr, 0, 10);
| ^~~~~~
main.c:16:9: note: include ‘<string.h>’ or provide a declaration of ‘memset’
main.c:17:9: warning: implicit declaration of function ‘strncpy’ [-Wimplicit-function-declaration]
17 | strncpy(ptr, argv, 9);
| ^~~~~~~
main.c:17:9: note: include ‘<string.h>’ or provide a declaration of ‘strncpy’
main.c:17:9: warning: incompatible implicit declaration of built-in function ‘strncpy’ [-Wbuiltin-declaration-mismatch]
main.c:17:9: note: include ‘<string.h>’ or provide a declaration of ‘strncpy’
main.c:20:24: error: empty character constant
20 | if(*ptr == '')
| ^~
main.c:30:8: warning: implicit declaration of function ‘free’ [-Wimplicit-function-declaration]
30 | free(ptr);
| ^~~~
main.c:30:8: note: include ‘<stdlib.h>’ or provide a declaration of ‘free’
main.c:30:8: warning: incompatible implicit declaration of built-in function ‘free’ [-Wbuiltin-declaration-mismatch]
main.c:30:8: note: include ‘<stdlib.h>’ or provide a declaration of ‘free’
$
因为 ++ 和 * 的优先权一样。
++ 优先级高于 *
++ 1 级
* 2 级