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)
             ^
复制代码

 。

 

posted @   小鲨鱼2018  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!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软件管理)中确认是否安装指定的安装包
点击右上角即可分享
微信分享提示