C函数看不到全局变量 [英] C function not seeing global variables

查看:185
本文介绍了C函数看不到全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么normals_since函数看不到全局变量跳跃? 我不敢相信main中声明的变量在整个程序中不可用,是什么封装或隐藏或意味着什么?是否可以通过诸如main.z或_ main _z之类的秘密方式进行访问? 我的gcc错误>>

why can't my normals_since function see my global variable leaps?? I can't believe that variables declared in main aren't available in the whole program is that what encapsulation or hiding or something means? is it accessible in some secret way like main.z or _main_z ?? my gcc error>>

yrs_since.c: In function ‘normals_since’:
yrs_since.c:40:9: error: ‘leaps’ undeclared (first use in this function)
yrs_since.c:40:9: note: each undeclared identifier is reported only once </p>
for each function it appears in

possible answer
looks like if I want all functions to see the vars, I have to move 
int z; //place holder
int leaps;
int normals;

在main之外,并在#defines之后在顶部声明它们

outside of main and declare them up at the top after the #defines

#include stdio.h>
#include stdlib.h>
#define START_GREG 1582
int yrs_since(int year); //type-declare the function
int leaps_since(int years);
int normals_since(int years);

int main(int argc, char* argv[]){
    int year = 1599; //local var
    int z; //place holder
    int leaps;
    int normals;
    z = yrs_since(year); //call the function
    leaps = leaps_since(z); //leap years move the doomsday fwd by 2 days
    normals= normals_since(z); //normal years it adjusts one day
    printf("blah blah %d,", z);//print the result
    printf("leap years since 1582:-->> %d  <<", leaps);
    printf("normal years since 1582:-->> %d  <<", normals);
    return EXIT_SUCCESS;
}
int yrs_since(year){
    int x;
    x=year-START_GREG;
    return x;
};
int leaps_since (years){
    return years/4;
};

int normals_since(years){
    int x;
    x=years-leaps;
    return x;
};

推荐答案

对,正如您所发现的,函数内的变量仅对该函数可见. main是一个函数,与其他任何函数一样,没有任何特殊方式.

Right, as you have found out, variables INSIDE a function are only visible to that function. main is a function just like any other function, it is not treated in any special way.

全局变量被声明为超出函数范围(但通常最好避免使用全局函数.

Global variables are declared OUTSIDE of functions (but it's generally good advice to avoid global functions, in general.

如果要避免使用全局变量,解决方案是使用变量将变量从main传递到函数中.

The solution, if you want to avoid global variables, is to pass the variable from main into the function using the variable.

例如:

int normals_since(int years, int leaps){
    int x;
    x=years-leaps;
    return x;
};

请注意,我在Years变量中添加了"int".尽管某些编译器仍允许使用旧式C,但绝对建议使用ANSI标准(在您的gcc命令行中添加-ansi -strict -Wall -std=c99,以警告您某些事情可能做错了",以及有关未遵循ANSI标准的错误)

Note that I added "int" to the years variable. Whilst old-style C is still allowed in some compilers, it's definitely recommended to use ANSI standard (add -ansi -strict -Wall -std=c99 to your gcc command line to give you warnings for "things you may have done wrong" and errors for not following ANSI standard)

这篇关于C函数看不到全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆