2

I'm pretty new to C programming and I had a question as to why a sample code I was given runs the way it does. I'm learning about function prototypes. Can someone give me a run down on the order in which this compiles?

//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;
    }

If it makes any sense, for the most part I understand and can write a program like that, however I am a little unclear as to the the steps involved, the calls, and the order in which the compiler compiles the program. Can someone shed a little light on this for? Greatly appreciate it!

Fresh Saint
  • 139
  • 2
  • 12
Bob Ross
  • 61
  • 7
  • 4
    `double CalcTotal();` is a declaration, not a prototype. The prototype would be `double CalcTotal(void);`. – Kerrek SB Feb 03 '14 at 06:44
  • 1
    @KerrekSB : Correct me if I am wrong but `double CalcTotal();` means `double CalcTotal(...);` right!! – 0xF1 Feb 03 '14 at 06:45
  • @MadHatter: Grammatically, `CalcTotal(...)` is a prototype that says "anything is fine", whereas `CalcTotal()` is a declaration that says "I'm not going to say anything about this function"... I think. C is really weird. – Kerrek SB Feb 03 '14 at 06:49
  • @MadHatter: In C, you cannot use ellipsis without a prior named argument. In fact, `double CalcTotal();` means "an undefined argument list, but not a variable argument list that could be described with ellipsis". You must have a correct prototype in scope for any function that has ellipsis in the declaration (and definition). – Jonathan Leffler Feb 03 '14 at 07:20
  • It sounds almost silly to say it, but you do ask about the order in which the compiler compiles the program. The compiler processes the code from top to bottom. The preprocessor includes `` and anything that it in turn includes, and then the compiler proper reads the resulting text (called a translation unit or TU) and notes declarations, processes definitions, and eventually generates object code (possibly after optimizing it, and maybe using an assembler to generate the object code). Then a linker is used to take the object code and any libraries to create an executable. – Jonathan Leffler Feb 03 '14 at 07:24

4 Answers4

3

Suggested changes:

/*
 * 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;
}
FoggyDay
  • 11,962
  • 4
  • 34
  • 48
2

Function declaration and function prototypes have their differences. A declaration simply means introducing the function (its name) to the compiler before it's usage, similar to a variable; just the function return type is specified. A prototype is where you specify every type the function is associated with i.e. argument type(s) and the return type.

Say you've an add that adds two ints and returns an int. This is a declaration

int add();

while this is a prototype

int add(int, int);

In C89 having a declaration isn't necessary at all. You may just use (call) add(3, 5) and the compiler should infer argument types from the function call and since no return type is known int is assumed. From C99 onwards declaration before usage was made mandatory. Still declaring a prototype isn't necessary. Thus the declaration would be

Note that the argument types isn't part of the declaration. C99 didn't allow the return type to be assumed implicitly as int, it has to be specified in the declaration. Still a function prototype isn't mandatory. Even today C11 doesn't expect a prototype but just a declaration.

Function prototypes were introduced in ANSI C (1989) where a function's declaration can have the argument and return types specified. Though it was never made mandatory for all functions. Only for variadic functions it's mandatory since C89 to declare a function with prototype.


As for your specific program, you declared two functions (one with prototype and one without [see Kerrek SB's comment]) and the used them in main; after main's definition, you've defined the two functions earlier declared.

Community
  • 1
  • 1
legends2k
  • 31,634
  • 25
  • 118
  • 222
0

your questions expects a lot of different topics to be discussed.

1)how compilation takes place? Check This Link For compilation Process

2) how are functions called ? Function Call

read these two links if still you have doubts be more specific on what you want.

KARTHIK BHAT
  • 1,410
  • 13
  • 23
0

Please follow the changes as suggsted by @FoggyDay.

The execution of any program is started from main. so compiler will first find out main and start execution from there.

    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.
}

Generally, execution of a program starts from main and goes step by step until a function is called inside main. As soon as any function is called, execution is redirected to that function until that function returns.

Ayse
  • 2,676
  • 10
  • 36
  • 61