Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.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语言中函数和main之间的变量_C_Function_Variables - Fatal编程技术网

c语言中函数和main之间的变量

c语言中函数和main之间的变量,c,function,variables,C,Function,Variables,我正在编写在c代码顶部声明全局变量的代码。然后在主函数中,我使用random为这些变量设置随机值。然后,代码调用外部函数对这些值进行计算。然而,在这个函数中,所有变量都显示为零。有没有办法传递这些变量 伪代码但是如何设置代码 // Header int A, B; main() { A = (rand() % 14000); B = (rand() % 14000); // other things math_func Printf("%d %d", A, B);

我正在编写在c代码顶部声明全局变量的代码。然后在主函数中,我使用random为这些变量设置随机值。然后,代码调用外部函数对这些值进行计算。然而,在这个函数中,所有变量都显示为零。有没有办法传递这些变量

伪代码但是如何设置代码

// Header
int A, B;
main() {
    A = (rand() % 14000);
    B = (rand() % 14000);
    // other things
    math_func Printf("%d %d", A, B);
    Return
}

math_func() {
    A + B;
    A* B;
    A / B;
}
现在,A和B在数学函数中似乎为0。。。任何想法都很感激

math_func() {
    A+B;
    A*B;
    A/B;
}
这三种说法没有任何效力。 例如,您想用这段代码实现什么

A+B;
这个表达式保持不变。 是否要更改值?如果是这样,你应该使用A=A+B;或A+=B;。
与其他两个语句相同。使用+=、*=和/=运算符。

这纯粹是推测,但似乎您希望printf以某种方式打印数学函数中语句的结果

如果希望这些语句的结果在main中可见,那么必须将它们分配给一些变量,并在main中打印出这些变量


你能发布一个真正的可编译测试代码吗?你可能会发现这个问题有帮助的伪代码没有用处。不管是什么错误,都在实际的可编译代码中。向我们展示这一点。指出伪代码中的错误是毫无意义的,因为你可以声称你的真实代码没有这些问题,所以请发布真实代码,说明你正在观察的问题。你已经接受了答案,因此显然你已经解决了你的问题,不管是什么问题。但是这个问题对未来的读者来说是没有用的,除非你更新它来显示你的实际代码。我们仍然不确定你的问题是什么。
#include <stdio.h>
#include <stdlib.h>


int A, B;
int C, D, E;
void math_func();
main() {
    A = (rand() % 14000);
    B = (rand() % 14000);
    // other things
    printf("%d %d\n", A, B);
    math_func();
    printf("%d %d %d\n", C, D, E);
}

void math_func() {
    C = A + B;
    D = A* B;
    E = A / B;
}
void math_func() {
    printf("%d %d\n", A, B);
    C = A + B;
    D = A* B;
    E = A / B;
}