Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we define function prototype in main function in C?

Tags:

c

prototype

in my university our teacher taught us "we define function prototype before main function. Like:

#include<stdio.h>
void findmax(float, float);
int main()
{
    //code goes here
}

But today my friend showed me they learned they put prototype inside main function. Like:

#include<stdio.h>
int main()
{
    void findmax(float, float);

    float firstnum, secondnum;

    printf("Enter first:");
    scanf("%f", &firstnum);

    printf("Enter second:");
    scanf("%f", &secondnum);

    findmax(firstnum, secondnum);
}

void findmax(float x, float y)
{
    float maxnum;

    if(x>y)
    {
            maxnum=x;
    }

    else
    {
       maxnum=y;
    }

    printf("The max is %f", maxnum);
}

They both works.I wonder if there are differences between them. Thanks.

like image 991
bitsedegitsek Avatar asked Dec 10 '17 12:12

bitsedegitsek


People also ask

What is the prototype of main function in C?

A function prototype is simply the declaration of a function that specifies function's name, parameters and return type. It doesn't contain function body. A function prototype gives information to the compiler that the function may later be used in the program.

What is the prototype of main function?

A function prototype is a definition that is used to perform type checking on function calls when the EGL system code does not have access to the function itself. A function prototype begins with the keyword function, then lists the function name, its parameters (if any), and return value (if any).

Where must a function prototype be declared?

Convention is to always declare a prototype in a header file that has the same name as the source file containing the function. In C99 or C11, standard C requires a function declaration in scope before you call any function.

Does C have function prototype?

A function prototype is one of the most important features of C programming which was originated from C++. A function prototype is a declaration in the code that instructs the compiler about the data type of the function, arguments and parameter list.


3 Answers

Can we define function prototype in main function in C?

Yes.

I wonder if there are differences between them

The difference is that in first snippet prototype is global while in second it is local to main. findmax will be visible after its declaration and/or definition.

#include<stdio.h>
void foo();
int main()
{
    void findmax(float, float); 
    foo();
    findmax(10, 20);  // It knows findmax by the prototype declared above
}

void findmax(float x, float y)
{
    float maxnum;

    if(x>y)
        maxnum=x;
    else
        maxnum=y;

    printf("The max is %f", maxnum);
}

void foo(){   // foo is using findmax after its definition.
    findmax(12, 30);
}
like image 87
haccks Avatar answered Sep 18 '22 10:09

haccks


If foo is declared in outside a function, it can be called from any function in the same file:

void foo(); // <-- global declaration

int main() {
  foo();    // <-- works, foo() is declared globally
}

void otherfunc() {
  foo();    // <-- works, foo() is declared globally
}

However, if foo is declared inside a function, it can only be used within the same scope:

int main() {
  void foo(); // <-- scoped declaration
  foo();      // works, foo() is declared in same scope
}

void otherfunc() {
  foo();      // ERROR: foo() is not declared in scope of otherfunc()
}

In either case, foo must be declared before it is used.

like image 45
Frxstrem Avatar answered Sep 22 '22 10:09

Frxstrem


If you declare the function in main() it is scoped in the main() and you cannot access it in another function. But if you declare it at the start of the file or in a header file you can use it in any function.

like image 25
Lucas Rosenberger Avatar answered Sep 22 '22 10:09

Lucas Rosenberger