Do you need to declare functions in C?

Possible duplicate:
Should I declare a function prototype in C?

I study C and in the book I read, this tidbit of code has a statement void scalarMultiply(int nRows, int nCols, int matrix[nRows][nCols], int scalar);. Does the program seem to work even if I don't 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;

    }
+3
source share
4 answers

If you do not declare a function before using it, the compiler may try to guess the signature of the function, and this may work.

, , , : , long long scalarMultiply, int, undefined: , ( -, ), .


:

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

:

-1, -1
4294967297

, ?

+1

, . . C main . , , , - .

0

C , , . ++.

: C , , gcc .c.

0

C89 , , , , int , . , , , .

, . , , . , , , !

C99 ++ , .

0
source

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


All Articles