Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何从函数外部访问变量_C - Fatal编程技术网

C 如何从函数外部访问变量

C 如何从函数外部访问变量,c,C,我在这里写一些C代码。我有一个简单的错误,这是相当混乱。 我正在浏览这个援助网站:。它说,在Main中声明的变量可以在函数之外的Main中使用。因此,我这里有一段代码:**经过简化和修改,看起来不那么复杂 void get_user_input(int grades[], int n) //My function { int i; for (i = 0; i < n; i++)//Loop to collet grades. { printf("Please entera grade b

我在这里写一些C代码。我有一个简单的错误,这是相当混乱。 我正在浏览这个援助网站:。它说,在Main中声明的变量可以在函数之外的Main中使用。因此,我这里有一段代码:**经过简化和修改,看起来不那么复杂

void get_user_input(int grades[], int n) //My function
{ 
int i;
for (i = 0; i < n; i++)//Loop to collet grades.
{
printf("Please entera grade between 0 and 100 for student # %i: ", i+1);
 scanf("%d", &grades[i]);

if (grades[i] >= 93)
{
 grades_scale[0]++;
 total_count++;
}
else if (grades[i]<= 92 && grades[i] >= 90)
{
 grades_scale[1]++;
 total_count++;
}
在我的Main中,我有评分量表总计数声明并初始化为全局变量对吗?然而,当我编译和运行我的程序时,它在我的函数中向我发送了一个错误,指出未声明总计数和等级规模。如何在我的主功能中、在我的功能中访问变量Ex:总计数和等级刻度。

main()是它自己的函数。要成为全局变量,它应该在任何函数之外初始化


除非将参数传递给包含在main()中声明的变量的函数,否则它将无法识别这些变量

这是一个完整的例子

注意

我认为这很值得一提,即使对于初学者来说,全局变量也有其用途,但通常被认为是不好的做法。我不会费心解释为什么,使用谷歌和stackoverflow的魔力

#include <stdio.h> 

/* global variables are here 
 * they don't need to be passed to the function */ 
int total_count = 0; 
int grades_scale[11] = {0};

/* your function */ 
void get_user_input(int grades[], int n)
{ 
    int i;
    for (i = 0; i < n; i++)//Loop to collet grades.
    {
        printf("Please entera grade between 0 and 100 for student # %i: ", i+1);
        scanf("%d", &grades[i]);

        if (grades[i] >= 93)
        {
            grades_scale[0]++;
            total_count++;
        }
        else if (grades[i]<= 92 && grades[i] >= 90)
        {
            grades_scale[1]++;
            total_count++;
        }
    }
}

int main()
{
    int n = 0; 
    printf("How many grades students are in the class?  ");
    scanf("%d", &n);

    int grades[n] = {0}; 
    get_user_input(grades,n);
}
#包括
/*全局变量在这里
*它们不需要传递给函数*/
整数总计数=0;
int grades_scale[11]={0};
/*您的函数*/
无效获取用户输入(整数分数[],整数n)
{ 
int i;
对于(i=0;i=93)
{
等级/刻度[0]++;
总计数++;
}
否则,如果(等级[i]=90)
{
等级(1级)++;;
总计数++;
}
}
}
int main()
{
int n=0;
printf(“班上有多少年级的学生?”);
scanf(“%d”和“&n”);
整数等级[n]={0};
获取用户输入(等级,n);
}

n必须实现什么?无论如何您的变量不是全局变量。它们是本地的
main
。哎呀,我不得不删除那个部分。我正要出错。如果用户输入一些奇怪或不兼容的数字,则重新启动循环。请稍等,我会把它处理掉。尤金说,全局变量放在所有函数之外,包括main。就像尤金说的,你的变量是main的局部变量。我建议读一读谢谢,一旦我读到了,它就给我带来了更多的启示。我现在可以用了。:)我该怎么办?你的意思是在main have:void get_user_input(int grades[],int n)?您可以选择全局变量路径,只需在程序开始时声明变量,然后再定义任何函数。谢谢,它似乎可以工作。但是,另一个错误由此产生。像往常一样,这似乎是可以做到的。谢谢你,朋友。
int n
应该初始化
int n=0
哦,是的,我没有清理他的代码,只是把全局声明放在应该放的地方!现在你站起来:)
#include <stdio.h> 

/* global variables are here 
 * they don't need to be passed to the function */ 
int total_count = 0; 
int grades_scale[11] = {0};

/* your function */ 
void get_user_input(int grades[], int n)
{ 
    int i;
    for (i = 0; i < n; i++)//Loop to collet grades.
    {
        printf("Please entera grade between 0 and 100 for student # %i: ", i+1);
        scanf("%d", &grades[i]);

        if (grades[i] >= 93)
        {
            grades_scale[0]++;
            total_count++;
        }
        else if (grades[i]<= 92 && grades[i] >= 90)
        {
            grades_scale[1]++;
            total_count++;
        }
    }
}

int main()
{
    int n = 0; 
    printf("How many grades students are in the class?  ");
    scanf("%d", &n);

    int grades[n] = {0}; 
    get_user_input(grades,n);
}