C函数原型

时间:2014-02-03 06:42:48

标签: c function

我对C编程很陌生,我有一个问题,为什么我给出的示例代码运行方式。我正在学习函数原型。有人可以告诉我这个编译顺序吗?

//TwoFunctions - All code split into two user-defined functions
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

//function prototypes
//Calculates and displays the total and avergae of two numbers

void CalcAvg(double tot);
double CalcTotal();
int main()
{
    double totl;
    totl = CalcTotal();
    CalcAvg(totl);
        printf("Your total is %.2f\n", totl);

    return 0;
}

CalcTotal()
    {
    double val, 
        num, 
        total;
    printf("Please enter a number: ");
    scanf(" %lf", &num);
    printf("Please enter another number: ");
    scanf(" %lf", &val);
    total = val + num;
    return total;
    }  


void CalcAvg(double tot)
    {
    double avg;
    avg = tot/2;
    //printf("Your total is %.2f\n", tot);
    printf("The average of the two numbers is %.2f\n", avg);
    return;
    }

如果它有任何意义,大部分我理解并且可以编写这样的程序,但是我对所涉及的步骤,调用以及编译器编译程序的顺序有点不清楚。有人可以对此有所了解吗?非常感谢!

4 个答案:

答案 0 :(得分:3)

建议的更改:

/*
 * Calculates and displays the total and avergae of two numbers
 */
#include <stdio.h>

//function prototypes
double CalcAvg(double tot);
double CalcTotal();

int main (int argc, char *argv[])
{
    double tot = CalcTotal();
    printf("Your total is %.2f\n", totl);
    printf("The average of the two numbers is %.2f\n", CalcAvg(totl));    
    return 0;
}

double
CalcTotal()
{
    double val,  num,  total;

    printf("Please enter a number: ");
    scanf(" %lf", &num);
    printf("Please enter another number: ");
    scanf(" %lf", &val);
    total = val + num;
    return total;
}  

double
CalcAvg(double tot)
{
    return tot / 2.0;
}

答案 1 :(得分:2)

函数声明和函数原型有其不同之处。声明只是意味着在使用之前将函数(名称)引入编译器,类似于变量;只是指定了函数返回类型。您可以在原型中指定与函数关联的每种类型,即参数类型和返回类型。

假设你有一个add,它会添加两个int并返回int。这是一个声明

int add();

虽然这是原型

int add(int, int);

在C89中根本没有必要声明。您可以只使用(调用)add(3, 5),编译器应该从函数调用中推断出参数类型,因为不知道返回类型int。从C99起,使用前的声明是强制性的。 Still declaring a prototype isn't necessary。因此宣言将是

请注意,参数类型不是声明的一部分。 C99不允许将返回类型隐式假定为int,它必须在声明中指定。功能原型仍然不是强制性的。即使在今天,C11也不会期待原型,只是宣言。

函数原型在ANSI C(1989)中引入,其中函数的声明可以指定参数和返回类型。虽然从来没有强制要求所有功能。仅对于可变函数,自C89起必须使用原型声明函数。


至于你的特定程序,你宣布了两个函数(一个是原型,一个没有[见Kerrek SB's comment]),并在main中使用它们;在main的定义之后,你已经定义了之前声明的两个函数。

答案 2 :(得分:0)

您的问题需要讨论很多不同的主题。

1)如何进行编译? Check This Link For compilation Process

2)如何调用函数? Function Call

阅读这两个链接,如果你仍然怀疑你想要什么更具体。

答案 3 :(得分:0)

请按照@FoggyDay的建议进行修改。

任何程序的执行都是从main开始的。所以编译器将首先找出main并从那里开始执行。

    int main()
    {
        double totl;
        totl = CalcTotal(); /*As a function call to CalcTotal(); function is made here, the
                            execution is main is suspended and compiler tries to find 
                            CalcTotal()'s definition and runs the whole function and return
                            value is store in "totl" variable. (Here the return value of
                            CalcTotal() is double as defined by function's return value.)*/

        /*As soon as CalcTotal() function returns, the compiler again comes back to main()
        where it suspended the execution and resumes the execution. saves return value of
        CalcTotal() in "totl" and jumps to next statement.*/

        CalcAvg(totl); /*Again in this statement, a function call to CalcAvg(totl) function
                       is made here so main()'s execution will be suspended and execution
                       jumps to CalcAvg(totl) function. The compiler continues to execute
                       this function until it returns*/

        /*When CalcAvg(totl) returns, the execution of main() is again reumed from here*/

        printf("Your total is %.2f\n", totl); //This line prints totl 

        return 0; //main returns and this defines end point of the program.
}

通常,程序的执行从main开始并逐步进行,直到在main内部调用函数。一旦调用任何函数,执行就会被重定向到该函数,直到该函数返回。

相关问题