gpt4 book ai didi

c - 如何在 int main() C 编程中调用您创建的函数?

转载 作者:行者123 更新时间:2023-11-30 20:34:41 24 4
gpt4 key购买 nike

我试图从 main 函数调用一个函数,但是当我编译并运行它时,它给了我一个随机数。我想要求用户在 yrBorn 函数中输入一个数字,然后将其返回到 main 函数,最后将其显示在终端中。

int yrBorn(int);
#include<stdio.h>
int yrBorn (int yearBorn)
{
printf("Enter the year you were born in: ");
scanf("%d", &yearBorn);
return yearBorn;
}

int main ()
{
printf("%d",yrBorn);
}

最佳答案

你必须像这样理解你的代码:

int yrBorn(int);  // function prototype say it must accept a int argument
#include<stdio.h>
int yrBorn (int yearBorn) //it must accept a argument of int type because of this definition
{
printf("Enter the year you were born in: ");
scanf("%d", &yearBorn);
return yearBorn;
}

int main ()
{
printf("%d",yrBorn); // You are passing function pointer here not a function call use like yrBorn(1994)
printf("%d",yrBorn(1994)); //it will work :)
}

关于c - 如何在 int main() C 编程中调用您创建的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41842425/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com