C 抑制用户定义的函数输出?

C 抑制用户定义的函数输出?,c,C,短版向上,长版向下。 假设我有一个这样的程序: #include <stdio.h> #include <stlib.h> int funcA(); void funcB(); int main(){ funcA(); funcB(); return 0; } int funcA(){ int x=7; printf("this is the output of funcA()\n"); return x; } void

短版向上,长版向下。 假设我有一个这样的程序:

#include <stdio.h>
#include <stlib.h>

int funcA();
void funcB();

int main(){
    funcA();
    funcB();
return 0;
}

int funcA(){
    int x=7;
    printf("this is the output of funcA()\n");
    return x;
} 

void funcB(){
    int y,z;
    y=funcA();        //this is me trying to store x from funcA() in y from funcB()
    z=y*5;
    printf("the output of funcB() is %d",z);
}
我只想让它说一次funcA()的输出和一次funcB(),但当我尝试在funcB()中将funcA()的返回值用作变量时,它会再次显示funcA()的输出。我该怎么阻止这一切?提前感谢您,也感谢帮助我更好地设置格式的用户

长部分: 以下是我的节目中让我感到悲伤的部分:

float pcRRV(){                                  //calculate power consumption with rated resistor values
    int i;
    float pr=0,ptemp;                       //pr=power with rated values, ptemp=temporary power variable for for() loop
    for(i=0;i<=RMAX;i++){
            R=RRV[i];
            ptemp=(V*V)/R;                  //power= V^2/R
            pr=ptemp+pr;                    //add all power values (could also have added all resistors then calculated total power, either would require for() loop)
    }
    printf("Given the rated resistor values, the power consumed by the resistors, in series with a %dV source, is %fW.\n",V,pr);
    puts("");
    return pr;                              //need power value for part D
}
float pcARV(){                                  //calculate power consumption with actual resistor values
    int i;
    float pa=0,ptemp;                       //same formate as previous function
    for(i=0;i<=RMAX;i++){
            A=ARV[i];
            ptemp=(V*V)/A;
            pa=ptemp+pa;
    }
    printf("Given the actual resistor values, the power consumed by the resistors, in series with a %dV source, is %fW.\n",V,pa);
    puts("");
    return pa;
}

float powerpercentdif(){                        //calculate percent difference between two power consumption values
    float a,b;
    float powerdif,powerave,percentdif;
    a=pcRRV();                              //use return value of pcRRV() as variable
    b=pcARV();                              //use return value of pcARV() as variable
    powerdif=(a-b);
    powerave=(a+b)/2;
    percentdif=(powerdif/powerave)*100;
    printf("The percent difference between the power consumption given the rated values and given the actual values of the resistors is %f%%.\n",percentdif);
}
但实际情况是:

Given the rated resistor values, the power consumed by the resistors, in series with a 10V source, is 66.240997W.

Given the actual resistor values, the power consumed by the resistors, in series with a 10V source, is 64.273056W.

Given the rated resistor values, the power consumed by the resistors, in series with a 10V source, is 66.240997W.

Given the actual resistor values, the power consumed by the resistors, in series with a 10V source, is 64.273056W.

The percent difference between the power consumption given the rated values and given the actual values of the resistors is 3.015677%.
现在我不希望前两个函数的输出显示两次。最简单的方法是不调用main中的前两个函数,而是在第三个函数调用它们时显示它们的输出。但是,由于这是针对一个类的,其中一个要求是我编写的3个函数都必须在主函数中调用才能显示它们的输出,因此我必须调用所有3个函数,但我不希望输出两次。最后,我想知道当我将第一个和第二个函数分配给第三个函数中的变量时,如何抑制它们的输出,因为我只想在该函数中使用它们的返回值,而不是让它们显示完整的输出。提前谢谢你的帮助,当我得到一个超级简单的回答时,我会感到宽慰/愤怒/沮丧/高兴。(我已经做了一段时间)谢谢你

你不能。您可以做的是使函数不打印结果。您可以将打印部件移动到另一个函数,或者直接将其放入
main
(或任何调用这些函数的函数)中(如果需要)

#include <stdio.h>
#include <stdlib.h>

int funcA();
void print_funcA();
int funcB();
void print_funcB();

int main(){
    print_funcA();
    print_funcB();
    return 0;
}

int funcA(){
    int x=7;
    return x;
}

void print_funcA() {
    printf("the output of funcA() is: %d\n", funcA());
}

int funcB(){
    int y,z;
    y=funcA();        //this is me trying to store x from funcA() in y from funcB()
    z=y*5;
    return z;
}

void print_funcB() {
    printf("the output of funcB() is: %d\n", funcB());
}
#包括
#包括
int funcA();
void print_funcA();
int funcB();
无效打印_funcB();
int main(){
print_funcA();
print_funcB();
返回0;
}
int funcA(){
int x=7;
返回x;
}
无效打印_funcA(){
printf(“funcA()的输出为:%d\n”,funcA());
}
int funcB(){
int y,z;
y=funcA();//这是我试图将funcA()中的x存储在funcB()中的y中
z=y*5;
返回z;
}
无效打印_funcB(){
printf(“funcB()的输出为:%d\n”,funcB());
}

请阅读并回答您的问题。您指的是从
main()
调用函数和输出问题,但您提供的代码根本不包括
main()
中的代码,因此我们可以看到您在那里执行的操作。我们不可能调试或排除看不到的代码。对不起,我现在就让它更易于阅读!有一个计算的函数,还有一个打印的函数。@n.m.我只是重新定义了它,这样我就有了一个计算的函数和一个打印的函数,但是每当我试图给它打印的函数赋值时,它就会输出。@F先生,如果不让它打印东西,就无法调用打印东西的函数。这就是为什么你应该有一个只计算要打印的内容的函数,还有一个不同的函数,它调用第一个函数来计算要打印的数字,然后打印出来。太好了。非常感谢你。刚做了这个改动,效果很好。谢谢你和n.m.的帮助。@F先生,顺便说一句,当我说“你不能”时,我撒谎了。更准确的说法是“你不应该”。这只会让您以后感到困惑,但可以向函数传递一个参数,告诉它是打印还是使用全局变量。这似乎是一种策略,任何主题都可以简单地介绍基本用法,但要深入研究更复杂的用法。但对于我的应用程序来说,它确实工作得很好。我只是试图避免很多全局变量。再次感谢。
Given the rated resistor values, the power consumed by the resistors, in series with a 10V source, is 66.240997W.

Given the actual resistor values, the power consumed by the resistors, in series with a 10V source, is 64.273056W.

Given the rated resistor values, the power consumed by the resistors, in series with a 10V source, is 66.240997W.

Given the actual resistor values, the power consumed by the resistors, in series with a 10V source, is 64.273056W.

The percent difference between the power consumption given the rated values and given the actual values of the resistors is 3.015677%.
#include <stdio.h>
#include <stdlib.h>

int funcA();
void print_funcA();
int funcB();
void print_funcB();

int main(){
    print_funcA();
    print_funcB();
    return 0;
}

int funcA(){
    int x=7;
    return x;
}

void print_funcA() {
    printf("the output of funcA() is: %d\n", funcA());
}

int funcB(){
    int y,z;
    y=funcA();        //this is me trying to store x from funcA() in y from funcB()
    z=y*5;
    return z;
}

void print_funcB() {
    printf("the output of funcB() is: %d\n", funcB());
}