5

Possible Duplicate:
Must declare function prototype in C?

I'm learning C and in the book i'm reading this tidbit of code has a statement of void scalarMultiply(int nRows, int nCols, int matrix[nRows][nCols], int scalar);. The program seems to work even if I do not include this line?

    int main(void)
    {

        void scalarMultiply(int nRows, int nCols, int matrix[nRows][nCols], int scalar);
        void displayMatrix(int nRows, int nCols, int matrix[nRows][nCols]);
    int sampleMatrix[3][5] = {
        { 7, 16, 55, 13, 12},
        { 12, 10, 52, 0, 7 },
        { -2, 1, 2, 4, 9   }

    };

    scalarMultiply(3, 5, sampleMatrix, 2);


}    void scalarMultiply(int nRows, int nCols, int matrix[nRows][nCols], int scalar){
        int row, column;

        for (row = 0; row < nRows; ++row)
            for (column = 0; column < nCols; ++column)
                matrix[row][column] *= scalar;

    }
Community
  • 1
  • 1
steve
  • 51
  • 1
  • 2

4 Answers4

5

If you don't declare a function before it's used the compiler might try to guess the signature of the function, and this could work.

Anyway you could get very weird results if the function the compiler guessed is different from the actual function: for example if you pass a long long as first parameter to scalarMultiply, it won't get converted to an int, and this will result in undefined behavior: most likely you'll corrupt your stack (the function will read parameters in a different way you meant) and everything will blow up.


Look:

#include <stdio.h>
int main( ) {
    f( (long long int) -1 );
    g( 1, 1 );
    return 0;
}
void f( int a, int b ) {
    printf( "%d, %d\n", a, b );
}
void g( long long int a ) {
    printf( "%lld\n", a );
}

Output will be:

-1, -1
4294967297

Weird, uh?

peoro
  • 25,562
  • 20
  • 98
  • 150
0

If you want to call a function before it's defined, you have to declare a prototype. Otherwise, no. This is why many C programs are written with main at the bottom and little helper functions at the top. The only time you really, really need function prototypes within a compilation unit is with mutual recursion.

nmichaels
  • 49,466
  • 12
  • 107
  • 135
0

Your traditional C compiler doesn't require prototyping and calling a function and declaring later will work as long as the call and later declaration don't conflict. This is not true of C++.

Caveat: I've not written a C only module in a while so I don't know what the latest versions of gcc will do in this case for a .c file.

Lou
  • 1,955
  • 14
  • 16
0

The required behaviour of C89 when encountering a function call for which no definition or prototype is available is to assume that it returns an int and has the number and types of the arguments that you supply in the call. If you subsequent call it with different argument types that cannot be legally implicitly cast, or a different number of arguments, the compiler will reject the code.

The compiler or linker will complain when it eventually finds a mis-matching definition. The use of a prototype allows the compiler to generate the error at the call rather than the definition, of when trying to link the definition from another module. So in short don't do it if you want the compiler to help you write working code!

In C99 and C++ prototypes are required if the definition is not already visible.

Clifford
  • 88,407
  • 13
  • 85
  • 165