鱼C论坛

 找回密码
 立即注册
查看: 1799|回复: 4

[已解决]函数声明的作用域

[复制链接]
发表于 2022-11-15 22:09:59 | 显示全部楼层 |阅读模式

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

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

x
在main函数中声明的max函数,在test()函数中可不可以不声明直接调用,还是说这个看编译器?
#include <stdio.h>
int test();
void main()
{
        float max(float x, float y);
    float a,b;
    scanf("%f,%f",&a,&b);
    printf("%f",max(a,b));
        test();
}
int test()
{
        float a=1,b=2;
        float c;
        c=max(a,b);
        printf("Max is %f\n",c);
        return 0;
}
float max(float x, float y)
{   float z;
    z=x>y?x:y;
        return z;
}
最佳答案
2022-11-15 22:30:54
本帖最后由 jackz007 于 2022-11-15 22:32 编辑

          确实,VC都可以,但是 gcc 不灵
        【tdm-gcc 5.1.0】:
D:\[00.Exerciese.2022]\C>g++ -o x x.c
x.c:3:11: error: '::main' must return 'int'
 void main()
           ^
x.c: In function 'int test()':
x.c:15:18: error: 'max' was not declared in this scope
         c=max(a,b);
                  ^

D:\[00.Exerciese.2022]\C>
        【vc6.0】:
D:\[00.Exerciese.2022]\C>cl x.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

x.c
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/out:x.exe
x.obj

D:\[00.Exerciese.2022]\C>
        【vc9.0】:
D:\[00.Exerciese.2022]\C>cl x.c
用于 80x86 的 Microsoft (R) 32 位 C/C++ 优化编译器 15.00.30729.01 版
版权所有(C) Microsoft Corporation。保留所有权利。

x.c
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:x.exe
x.obj

D:\[00.Exerciese.2022]\C>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-11-15 22:15:53 | 显示全部楼层
感觉那个max只在main函数的作用域内吧,那个max得声明在函数外部,才能被发现调用
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-15 22:30:54 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2022-11-15 22:32 编辑

          确实,VC都可以,但是 gcc 不灵
        【tdm-gcc 5.1.0】:
D:\[00.Exerciese.2022]\C>g++ -o x x.c
x.c:3:11: error: '::main' must return 'int'
 void main()
           ^
x.c: In function 'int test()':
x.c:15:18: error: 'max' was not declared in this scope
         c=max(a,b);
                  ^

D:\[00.Exerciese.2022]\C>
        【vc6.0】:
D:\[00.Exerciese.2022]\C>cl x.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

x.c
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/out:x.exe
x.obj

D:\[00.Exerciese.2022]\C>
        【vc9.0】:
D:\[00.Exerciese.2022]\C>cl x.c
用于 80x86 的 Microsoft (R) 32 位 C/C++ 优化编译器 15.00.30729.01 版
版权所有(C) Microsoft Corporation。保留所有权利。

x.c
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:x.exe
x.obj

D:\[00.Exerciese.2022]\C>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-16 16:43:31 | 显示全部楼层
这是想在一个什么样的场景里这么用呢, 既然想方便调用,声明放在文件作用域不是很合适么?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-16 22:00:03 | 显示全部楼层
两手空空儿 发表于 2022-11-16 16:43
这是想在一个什么样的场景里这么用呢, 既然想方便调用,声明放在文件作用域不是很合适么?

突然想到的一个脑抽问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-12 14:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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