c语言中函数体中的变量声明不能使用和形参相同的变量名
001、
[root@PC1 test]# ls test.c [root@PC1 test]# cat test.c #include <stdio.h> int max(int a, int b) // 创建一个名为max的函数 { int k = 100; if(a > b) { return a; } else { return b; } } int main(void) { int x, y; puts("please input two integers."); printf("x: "); scanf("%d", &x); printf("y: "); scanf("%d", &y); printf("the large is %d\n", max(x,y)); return 0; } [root@PC1 test]# gcc test.c -o kkk [root@PC1 test]# ls kkk test.c [root@PC1 test]# ./kkk please input two integers. x: 87 y: 34 the large is 87
。
002、
[root@PC1 test]# ls test.c [root@PC1 test]# cat test.c ## 测试程序 #include <stdio.h> int max(int a, int b) // 创建一个函数max { int a = 100; //声明一个和形参同名的变量 if(a > b) { return a; } else { return b; } } int main(void) { int x, y; puts("please input two integers."); printf("x: "); scanf("%d", &x); printf("y: "); scanf("%d", &y); printf("the large is %d\n", max(x,y)); return 0; } [root@PC1 test]# gcc test.c -o kkk ## 程序无法正常编译 test.c: In function ‘max’: test.c:5:6: error: ‘a’ redeclared as different kind of symbol int a = 100; ^ test.c:3:13: note: previous definition of ‘a’ was here int max(int a, int b) ^
。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2023-11-07 centos7 中安装perl的JSON模块
2023-11-07 Configuration failed because libxml-2.0 was not found. Try installing:
2023-11-07 Configuration failed to find one of freetype2 libpng libtiff-4 libjpeg.
2023-11-07 Configuration failed because libcurl was not found. Try installing:
2023-11-07 ERROR: dependencies ‘openssl’, ‘curl’ are not available for package ‘credentials’
2023-11-07 fatal error: pango/pangocairo.h: No such file or directory
2023-11-07 linux(redhat系列 rpm软件管理)中确认是否安装指定的安装包