Search code examples
cdev-c++

i'm unable to compile a simple C function program in Dev c++?


I'm trying to compile in Dev-C++, program is:

main( )
{
    message( ) ;
    printf ( "\nCry, and you stop the monotony!" ) ;
}

message( )
{
    printf ( "\nSmile, and the world smiles with you..." ) ;
}

Errors are:

E:\Dev\fn.cpp [Error] 'message' was not declared in this scope
E:\Dev\fn.cpp [Error] ISO C++ forbids declaration of 'message' with no type [-fpermissive]

Edit: Silly mistake. Was compiling a c program as cpp


Solution

  • Firstly, compile your C code as C code, not C++ code. The extension of C code should be .c, not .cpp.

    Then, recognize that there may be wrong things in books and correct code. You shouldn't omit types of return value and arguments, and functions used should be declared before it is used.

    Try this:

    #include <stdio.h> /* declaration of printf() will be here */
    
    /* bring this function before using */
    void message(void)
    {
        printf ( "\nSmile, and the world smiles with you..." ) ;
    }
    
    int main(void)
    {
        message( ) ;
        printf ( "\nCry, and you stop the monotony!" ) ;
        return 0;
    }