-2

Here is my code:

#include <stdio.h>
#include <stdlib.h>

int compare(int a, int b);

main()
{

int x,y;
x = 2, y = 1;
printf("%d", compare(x,y));

    int compare(int a, int b)
    {
        int returnValue = 0;
        if(a>b)
            returnValue = 1;
        else if(a<b)
            returnValue = -1;
        else
            returnValue = 0;

        return(returnValue);
    }
}

And this is the compiler error that I recieve:

In function `main':
asdf.c:(.text+0x21): undefined reference to `compare'
collect2: error: ld returned 1 exit status

I have looked into this problem, and every question that I can find with this error is because people are importing functions from different files. These two functions are in the same file, and the compare() function is declared before the main(). I would appreciate any help as to why I am getting this error.

Thank you for your time.

Niall
  • 30,036
  • 10
  • 99
  • 142

6 Answers6

1

You must define function outside the main function. your code should be:

#include <stdio.h>
#include <stdlib.h>

int compare(int a, int b);

main()
{

int x,y;
x = 2, y = 1;
printf("%d", compare(x,y));
}
int compare(int a, int b)
    {
        int returnValue = 0;
        if(a>b)
            returnValue = 1;
        else if(a<b)
            returnValue = -1;
        else
            returnValue = 0;

        return(returnValue);
    }
hoangddt
  • 33
  • 2
  • 7
0

The function compare must be out side the main program. Either you define the function header before the main() and have the function after the main or have the function before the main().

#include <stdio.h>
#include <stdlib.h>

int compare(int a, int b);

main()
{

int x,y;
x = 2, y = 1;
printf("%d", compare(x,y));

}

    int compare(int a, int b)
    {
        int returnValue = 0;
        if(a>b)
            returnValue = 1;
        else if(a<b)
            returnValue = -1;
        else
            returnValue = 0;

        return returnValue;
    }
Thanushan
  • 532
  • 2
  • 8
0

Nested function does not exist in C. But again speaking that it's a compiler specific

You can see gcc extension for nested function which is nonstandard.

So for solution simply move your function definition to outside the main.

Community
  • 1
  • 1
Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
0

You've declared the global function compare at file scope, but only defined a nested function main::compare. As commenters have pointed out, this is not standard C, it's a gcc extension that you don't want to be using.

Move compare out to file scope:

int main()
{
   // do stuff
   return 0;
}

int compare(int a, int b)
{
   // do stuff
   return value;
}
etheranger
  • 1,233
  • 7
  • 17
0

Put compare function out of main function. C language doesn't allow functions defined inside other functions.

Code

#include <stdio.h>
#include <stdlib.h>

int compare(int a, int b);

int main(void )
{

int x,y;
x = 2, y = 1;
printf("%d\n", compare(x,y));

}

int compare(int a, int b)
{
    return ((a<b) - (a>b));
}
Nikhil Vidhani
  • 709
  • 5
  • 11
0

You have two different functions compare: One at global scope, which is only declared, but not defined, and one defined at local scope. The local compare can only be called from local scope, but only after it is defined. (Declaring a nested function inside the body of main before its definition doesn't work, because the C standard permits declarations in function bodies, but treats them as global.)

If you must use nested functions, move the definition of your local compare to the beginning of main. Nested functions are non-standard.

It is better to move the definition of compare outside the function into file scope. If you keep the definition of compare close to where it's called and make it static, you will get the same behaviour.

M Oehm
  • 28,726
  • 3
  • 31
  • 42