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) ^
。