前瞻性声明如何运作?

前瞻性声明如何运作?,第1张

概述我理解在main之前声明factorial.但是,当阶乘公式出现之后,主要如何计算答案呢? #include <stdio.h>long long factorial(int);int main(){ int n; long long f; printf("Enter a number and I will return you its factorial:\n 我理解在main之前声明factorial.但是,当阶乘公式出现之后,主要如何计算答案呢?

#include <stdio.h>long long factorial(int);int main(){    int n;    long long f;    printf("Enter a number and I will return you its factorial:\n");    scanf_s("%d",&n);    if (n < 0)        printf("No negative numbers allowed!\n"); //prevent negative numbers    else    {        f = factorial(n);        printf("The factorial of %d is %ld\n",n,f);    }    return 0;}long long factorial(int n){    if (n == 0)        return 1;    else        return (n * factorial(n - 1));}
解决方法

But how can main calculate the answer when the factorial formula comes after it?

第一件事 – 主要不计算答案;它是你的阶乘功能,为你做到这一点.在编写程序时,我还需要了解3个步骤:

>您将代码写入文件.
>您编译文件并且编译器检查语法错误,在此阶段没有进行代码计算仅仅是词法分析.
>然后链接发生.如果收到链接器错误,则表示您的代码编译正常,但无法找到所需的某些函数或库.这发生在我们称之为链接阶段并将阻止生成可执行文件.许多编译器同时执行编译和链接阶段.

然后,当您实际运行代码时 – 那时代码的控制流在计算发生时(即在运行时)进入阶乘函数.使用Debugger查看此内容.

下图来自Compiling,Linking and Building C/C++ Applications

从Wiki:

In computer programming,a forward declaration is a declaration of an
IDentifIEr (denoting an entity such as a type,a variable,a constant,
or a function) for which the programmer has not yet given a complete
deFinition….
This is particularly useful for one-pass compilers and separate
compilation. Forward declaration is used in languages that require
declaration before use; it is necessary for mutual recursion in such
languages,as it is impossible to define such functions (or data
structures) without a forward reference in one deFinition: one of the
functions (respectively,data structures) must be defined first. It is
also useful to allow flexible code organization,for example if one
wishes to place the main body at the top,and called functions below
it.

所以基本上主要功能根本不需要知道因子是如何工作的.

总结

以上是内存溢出为你收集整理的前瞻性声明如何运作?全部内容,希望文章能够帮你解决前瞻性声明如何运作?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1216442.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存