Can we define a function prototype in a main function in C?

at my university, our teacher taught us "we define the prototype of a function before the main function. For example:

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

But today, my friend showed me that they found out that they put the prototype inside the main function. How:

#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 work. I wonder if there are differences between them. Thank.

+4
source share
5 answers

Can we define a function prototype in a main function in C?

Yes.

I wonder if there are differences between the two.

The difference lies in the fact that in the first code fragment the prototype is global, and in the second it is local to main. findmaxwill be displayed after its announcement and / or determination.

#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);
}
+5
source

main(), main(), . , .

+2

foo , :

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

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

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

, foo , :

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()
}

foo .

+2

. , .

, , - , . , , . . :

#include <stdio.h>

/* Can be accessed from anywhere in this file
 * because defined before anything else.
 */
void print_hello() {
        puts("Hello!");
}

int main() {
        void print_hello(); /* Not needed actually */
        void print_namaste();
        void print_hi();

        print_namaste();
        print_hi();
}

/* The following two functions cannot be
 * accessed from within the above two functions
 * unless these two are declared inside them
 * or at the starting of this file
 */
void print_namaste() {
        puts("Namaste!");
        print_hello();
}

void print_hi() {
        puts("Hi!");
}

, print_hello() main(). . print_hello() . , ( , ). , print_hello() main() , . print_namaste(), , - .

print_namaste() print_hi(). , main(), , main() .

print_namaste() , , , print_namaste(). print_namaste() print_hello() ( ), main(), , print_hi, print_namaste().

0

VC2008 :

int main(void)
{
    void foo(void);
    //...
}
void test(void)
{
    foo();
}

, , , , .

C99:

6.2.1

; ; , ; typedef; ; ; ... , , ( ) , ... , ( ). , , , . , , , , .

This means that the VC2008 is erroneous.

-1
source

Source: https://habr.com/ru/post/1690508/


All Articles