简体   繁体   English

有人可以帮我解决 C 问题吗? 当我尝试运行它不返回任何东西

[英]Could someone help me with that C problem? When I try to run it doesn't return anything

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

double cube(double num) {
    return num * num * num;
}


int main(){

    Answer(cube(3.0));

    return 0;
}

void Answer(double cube()) {

    printf("Answer: %.1f\n", cube());

}

If you try to run it, doesn't work, at least on my computer.如果您尝试运行它,至少在我的计算机上不起作用。 I tried to change some things like adding some more parentheses, but I can't find the solution.我试图改变一些东西,比如添加更多的括号,但我找不到解决方案。

I don't think your code compiles.我不认为你的代码编译。 Here's some quick fixes:这里有一些快速修复:

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

void Answer(double); // forward declaration, you could also define Answer before main

double cube(double num) {
    return num * num * num;
}


int main(){

    Answer(cube(3.0));

    return 0;
}

void Answer(double value) {

    printf("Answer: %.1f\n", value);

}

Or simpler, move the Answer() function above main() below cube() .或者更简单,将Answer() function 移到main()上方的cube() ) 下方。

C files are compiled in a Single-Pass . C 文件在Single-Pass中编译。 This means that before a function can be used, the compiler must know about it.这意味着在使用 function 之前,编译器必须知道它。 (it has to already know what it is before it reaches the point where it is used) (它必须在到达使用点之前已经知道它是什么)

With Answer() below main() (and no forward declaration), when the compiler reaches main() and sees Answer() it has no idea what it is yet.main() Answer() ) (并且没有前向声明),当编译器到达main()并看到Answer()时,它还不知道它是什么。 So one fix is to provide a prototype , a forward-declaration, promising the compiler that yes, this function will be defined later in the source file (or elsewhere).所以一个解决方法是提供一个原型,一个前向声明,向编译器保证是的,这个 function 将在源文件(或其他地方)中定义

Your other option (recommended for short functions) is to simply order the definition for the function in an order so each is defined before it is used.您的另一个选项(推荐用于短函数)是简单地按顺序订购 function 的定义,以便在使用之前定义每个。 This will naturally end up with all functions defined above main() , but there is nothing wrong with that.这自然会以上面定义的所有函数结束main() ,但这并没有错。

As the size and number of the functions you are using grows -- then it can be beneficial (convenient) to provide forward-declarations to help organize the code in a logical way that it can be split between a source and header file with minimal effort.随着您使用的函数的大小和数量的增长——那么提供前向声明以帮助以一种逻辑方式组织代码是有益的(方便的),它可以在源文件和 header 文件之间以最小的努力进行拆分. In large source files organizing the forward declarations up top in the order they appear below main() also provides road-map to where the functions are defined (which may help you navigate to where you need to go, or provide a quick copy/paste into the search dialog)在大型源文件中,按照它们出现在main()下方的顺序组织前向声明,还提供了定义函数的路线图(这可以帮助您导航到需要 go 的位置,或提供快速复制/粘贴进入搜索对话框)

@selbie has already provide your answer for using a prototype or forward-declaration. @selbie 已经为您提供了使用原型或前向声明的答案。 A simple reordering would be:一个简单的重新排序将是:

#include <stdio.h>

double cube (double num)
{
    return num * num * num;
}

void Answer (double value)
{
    printf ("Answer: %.1f\n", value);
}

int main (void) {

    Answer (cube (3.0));

    return 0;
}

( note: avoid including unneeded header files in your code) 注意:避免在代码中包含不需要的 header 文件)

How you want to handle it is completely up to you, but these are the normal considerations that go into your decision.你想如何处理它完全取决于你,但这些是 go 进入你决定的正常考虑因素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试编写一个C程序来将文件中的整数存储到数组中,但是它不起作用。 有人能帮我吗? - I'm trying to write a C program to store integers from a file into an array, but it doesn't work. Can someone help me? 有人可以帮我清楚一些关于指针的混淆吗? 在C. - Could someone help me clear some confusions about pointers? in C 当 function 的返回类型不允许我这样做时,如何在 C 中返回错误代码? - How do I return an error code in C when the return type of the function doesn't allow me to do that? 有人可以帮我C吗 - Can someone help me with C 用C编写Shell,不返回任何内容 - Writing a shell in C, doesn't return anything 这是 Cs50 问题 set1 现金。 我不知道这段代码有什么问题有人可以帮助我 - This is Cs50 problem set1 cash. I don't know what's wrong with this code can someone help me 有人可以帮我在C中使用这些索引吗 - Can someone help me with these indices in c 有人可以帮我处理 C 中的结构吗? - Can someone help me with structs in C? 如果有人可以帮助我,那将是令人难以置信的我是一名学生并且在解决这个程序时遇到了错误 - if someone could help me it would be incredible i am a student and got the error while solving this program 有人可以帮我弄清楚为什么我收到错误 malloc(): corrupted top size - Could someone help me figure out why I am getting the error malloc(): corrupted top size
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM