C语言 在 C 中,从 main 调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16617858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In C, calling a function from main
提问by Wobblester
In C, I tried to call a function printSum from main. But the main function isn't calling printSum, its just printing out "Hi!" which is a print statement from main. I am not sure why printSum is not being called. Thanks.
在 C 中,我尝试从 main 调用函数 printSum。但是主函数没有调用printSum,它只是打印出“嗨!” 这是来自 main 的打印语句。我不确定为什么没有调用 printSum。谢谢。
Code:
代码:
int main(void){
void printSum(void);
printf("Hi!\n");
return 0;
}
void printSum (void){
printf("Please give two integers\n");
int x,y;
scanf("%d %d", &x,&y);
printf("%d + %d is %d\n",x,y,x+y);
}
Sam
山姆
回答by Dory Zidon
you defined it again.. Just remove the void from the funciton. include a header a forward declartion so it will recognize it..
您再次定义了它。只需从函数中删除空隙即可。包含一个标题和一个前向声明,以便它会识别它..
void printSum (void); <-------------------
int main(void){
printSum(); <-------------------------
printf("Hi!\n");
return 0;
}
void printSum (void)
{
printf("Please give two integers\n");
int x,y;
scanf("%d %d", &x,&y);
printf("%d + %d is %d\n",x,y,x+y);
}
回答by Nathan Hiemenz
It looks like you're just starting out with C. Hopefully you have some experience with other languages, as C has a steep learning curve. Anyway, it's important to note a few things about C. The first is what void printSum(void);really means. Breaking it down:
看起来您刚刚开始使用 C。希望您对其他语言有一些经验,因为 C 的学习曲线很陡峭。不管怎样,重要的是要注意一些关于 C 的事情。第一个是void printSum(void);真正的意思。分解它:
voidprintSum(void);
无效打印和(无效);
This declares a return signature. In other words, what the function gives back to you. In C, the word voidbasically means "no variable". Remember that specifically. Why? Because C has another similar word, NULL. NULLmeans "no value." This is another way to look at it.
这声明了一个返回签名。换句话说,函数给你什么。在 C 中,这个词void基本上意味着“没有变量”。记住这一点。为什么?因为 C 有另一个相似的词,NULL。NULL意思是“没有价值”。这是另一种看待它的方式。
Some valid variables: int, float, bool, voidSome valid values: 1, 'c', 2.0f, NULL
一些有效变量:int, float, bool,void一些有效值:1, 'c', 2.0f,NULL
In reality, NULLis actually just the number 0. Literally. NULL == 0will return true.
实际上,NULL实际上只是数字0。字面上地。NULL == 0将返回真。
Moving on...
继续...
void printSum(void);
无效打印和(无效);
This defines the name of the item.
这定义了项目的名称。
void printSum (void );
无效打印总和(无效);
The parentheses mean this is a function.
括号表示这是一个函数。
void printSum(void);
无效打印总和(无效);
This represents a variable being passed into the system. So this couldhave been int, float, etc.
这表示一个变量被传递到系统中。所以这可能是 int、float 等。
void printSum(void) ;
void printSum(void) ;
The semi-colon represents the end of a statement. This is a bit trickier of a concept to explain, but just think of it as finishing a sentence.
分号代表语句的结束。解释这个概念有点棘手,但只需将其视为完成一个句子即可。
Now, the important bit here is the very first void. If you tell C what kind of thing a function returns, it assumes you are talking about the function, not actually calling it. Omitting the first void makes C attempt to runthe function instead of defineor declareit.
现在,这里的重要一点是第一个void. 如果你告诉 C 一个函数返回什么样的东西,它假设你在谈论这个函数,而不是实际调用它。省略第一个 void 会使 C 尝试运行该函数而不是定义或声明它。
The difference between defining a function and declaring it is interesting and might be best saved for when you're a bit more accustomed to C.
定义函数和声明函数之间的区别很有趣,最好在您更习惯 C 时保存。
回答by Carl Norum
Your program doesn't call printSum, it just declares it. Change this line:
您的程序不会调用printSum,它只是声明它。改变这一行:
void printSum(void);
to
到
printSum();
If your functions are in the same order in your source file as you put them here, you'll also need to forward declare or move the implementation of printSumabove mainto be correct.
如果你的函数是在源文件相同的顺序,你把它们放在这里,你还需要向前声明或移动执行printSum上面main是正确的。
You should probably look into getting a beginner C book.
您可能应该考虑获得一本初学者 C 书。
回答by Lee Daniel Crocker
void printSum(void);
Does not actually call the function, it merely declares that it exists. You need to do that so the compiler will know how to call it even though it hasn't been defined yet, so leave that line in. But to actually call the function, you need:
实际上并不调用该函数,它只是声明它存在。您需要这样做,以便编译器即使尚未定义它也知道如何调用它,因此请保留该行。但要实际调用该函数,您需要:
printSum();
回答by Stewart
The following:-
下列:-
void printSum(void);
is not a function call. It is a function declaration.
不是函数调用。它是一个函数声明。
printSum();
is a function call. You should also forward declare the function before main.
是一个函数调用。您还应该在 main 之前转发声明该函数。
回答by Dan F
Because instead of actually calling the function, you are just declaring it. To call the function you just need the function name and parameter list, which in this case is empty:
因为不是实际调用函数,而是声明它。要调用该函数,您只需要函数名称和参数列表,在本例中为空:
printSum();

