Thursday, April 24, 2008

allowed in C, but not in C++?

Followings are from Internet source (openasthra.com). Some are interesting and I didn't notice before:

  1. sizeof (’1′) == sizeof (int) in C; but it is sizeof (char) in C++. So we get the result 4 and 1 with compiler gcc and g++, respectively.
  2. Usually you should not use *alloc()/free() in C++, but they are the only such functions in C.
  3. Functions need not be prototyped in C, but it is a must in C++.
  4. struct a {  
             struct b {
                    int a; 
              };
    };
    struct b b; /*allowed in C, but not in C++ */
    not quite sure this one, but it has error in g++ compiler(error: aggregate 'b b' has incomplete type and cannot be defined. With following codes:
    sturct a {
            int a;
    }
    struct a a;
    some compilers are OK, some will report error.
  5. const int b = 1; /* allowed in C and C++ */
    const int a ;      /* allowed in C, not in C++ */
  6. global variables can be defined more than once in C, not in C++
    int a; /* works in C, C++ */
    int a = 10 ; /* works in C, not in C++ */
    int main()
    {...}
  7. const a; /* equivalent to const in a; in C, illegal in C++ */
  8. char s[7] = "1234567"; /* allowed in C, error in C++ */
  9. K&R style function definitions are not allowed in C++, such as:
    void foo(a)
    int a;
    {...}

 

No comments:

Post a Comment