Home > C++ Introduction > Compiling a C program by a C++ compiler

Compiling a C program by a C++ compiler

For the sake of completeness, it must be mentioned here that C++ is `almost’ a superset of C. There are some small differences which you might encounter when you just rename a file to an extension .cc and run it
through a C++ compiler:

In C, sizeof(‘c’) equals sizeof(int), ‘c’ being any ASCII character. The underlying philosophy is probably that char’s, when passed as arguments to functions, are passed as integers anyway.
Furthermore, the C compiler handles a character constant like ‘c’ as an integer constant. Hence, in C, the function calls

putchar(10);

and

putchar(‘\n’);

are synonyms.

In contrast, in C++, sizeof(‘c’) is always 1 (but see also section 3.3.2), while an int is still an int. As we shall see later (see section 2.5.13), two function calls

somefunc(10);

and

somefunc(‘\n’);

are quite separate functions: C++ discriminates functions by their arguments, which are different in these two calls: one function requires an int while the other one requires a char.

C++ requires very strict prototyping of external functions. E.g., a prototype like

extern void func();

means in C that a function func() exists, which returns no value. However, in C, the declaration doesn’t specify which arguments (if any) the function takes.

In contrast, such a declaration in C++ means that the function func() takes no arguments at all.

Categories: C++ Introduction Tags:
  1. Test
    May 18th, 2010 at 05:33 | #1

    Hi all!

    Bye

  2. SemyClecy
    June 8th, 2010 at 08:38 | #2

    Great tips! I will try it definitely
    thanks for sharing this!

  1. No trackbacks yet.