Function declaration order matters in c language or am I doing something wrong?

10,382

Solution 1

Except in a few exceptions, an identifier in C cannot be used before it has been declared.

Here is how to declare a function outside its definition:

// Declare randomStr function
char *randomStr(int length);

Solution 2

The error occurs because symbols must generally be declared before they are used, not after.

A special exception to that is functions. If they are used before declaration, a particular declaration is inferred. In your case, the inferred declaration did not match the eventual definition.

Solution 3

In C89 and earlier, if the compiler saw a function call before a corresponding declaration or definition, it assumed that the function returned int. Similarly, if you left the type specifier off of the function definition, it was assumed to return int (this is why a lot of older examples have an entry of just main() {...} instead of int main(void) {...}).

Since the call to randomStr occurs before its declaration/definition, it is assumed to return int, which is not compatible with char * (you can't assign a value of one to the other without an explicit cast), hence the error.

As of C99, implicit typing is no longer allowed.

The -ansi option causes gcc to compile as C89. To compile as C99, use -std=c99 instead.

I generally put the function definition before its first use within the same file, so my code reads from the bottom up (main is typically the last function defined in the file). That way I don't have to worry about keeping declarations and definitions straight.

Solution 4

You need to put a function declaration before any functions that use a function that isn't defined yet.

For example:

int doesFileExist(const char* str);

would go before main().

The reason for this is that the C compiler runs through your code, and every time it sees a function, it looks to see if the function has been defined yet. If it can't find that function, it gives you an error. main() uses doesFileExist(), which has not been declared by the time the compiler looks at main(). To fix this, you can make a declaration of a function at the top, as I mentioned above. This tells the compiler that, though a function has not been filled in yet, it will be filled in later and so to treat it as if the function does exist, then go connect them later once you've found the function later on (ie, somewhere after the main() function).

Your second block of code works because the compiler has definitions for those functions which you use in main() by the time it gets there. Your first block does not work because the functions neither defined nor declared that they will be defined later.

Solution 5

Just add a function prototype

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

char* randomStr(int length);

int main(int argc, char** argv)
{
        char *rndStr;
        rndStr = randomStr(FILE_SIZE);
        /* Do stuff */
        return 0;
}

/*Generate a random string*/
char* randomStr(int length)
{
        char *result;
        /*Do stuff*/
        return result;
}
Share:
10,382
AturSams
Author by

AturSams

Indie Dev

Updated on June 04, 2022

Comments

  • AturSams
    AturSams almost 2 years

    I get this error:

    arthur@arthur-VirtualBox:~/Desktop$ gcc -o hw -ansi hw1.c
    hw1.c: In function `main':
    hw1.c:27:16: warning: assignment makes pointer from integer without a cast [enabled by default]
    hw1.c: At top level:
    hw1.c:69:7: error: conflicting types for `randomStr'
    hw1.c:27:18: note: previous implicit declaration of `randomStr' was here
    

    While compiling this code:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char** argv)
    {
            char *rndStr;
            rndStr = randomStr(FILE_SIZE);
            /* Do stuff */
            return 0;
    }
    
    /*Generate a random string*/
    char* randomStr(int length)
    {
            char *result;
            /*Do stuff*/
            return result;
    }
    

    If I switch the function order around it works Why?

  • ouah
    ouah over 11 years
    Note that implicit declaration of functions has been removed since C99.
  • matth
    matth over 11 years
    True. Also note that OP isn't using C99.
  • kumowoon1025
    kumowoon1025 over 5 years
    I'm not trying to nitpick, I'm actually not sure, that isn't a prototype is it?
  • Reid Moffat
    Reid Moffat almost 3 years
    That works, but function declarations should be in a separate .h header file