Thursday, December 6, 2012

A classic difference between C & C++

In C++ it is strictly enforced that all functions must be declared before they are used. But in C this isn't necessary.

This code is valid C, but it is not valid C++.

#include <stdio.h>
int main()
{
    test();
    return 0;
}

int test()
{
    printf( "Hello C" );
}



     * invalid code *                * Valid code *                  * valid code *

#include <iostream>           #include <iostream>           #include <iostream>
using namespace std;          using namespace std;         using namespace std;
                                                                          
int main()                          int test();                          int test()
{                                                                            {
    test();                           int main()                          printf("hello cpp");
}                                      {                                     }
                                           test();                    
int test()                           }                                     int main()
{                                                                            {
    printf("Hello cpp");           int test()                            test();
}                                      {                                     }
                                           printf("hello CPP");   
                                        }                              

No comments:

Post a Comment