Key Differences Between Ansi C and C++

B

blangela

I am teaching a C programming course to C++ programmers and want to
come up with list of _KEY_ differences between the two languages.
Below is a list I have come up with so far. Have I missed any?

Key Differences between ANSI C and C++

1. C is a procedural programming language (where as C++ is an Object
Oriented Programming language). This means that the C programming
language does not support classes and all functionality provided
directly or indirectly through classes. Therefore C does not support
inheritance, polymorphism or encapsulation of data. C does support
"struct"s, which are like classes without support for member functions
and with no ability to encapsulate the data members that are defined
as belonging to the struct (a struct is an ADT).

2. C does not have access to the iostream library. Input and output in
C is done using the stdio.h library which provides access to the
scanf() and printf() functions, used to do input and output
respectively.

3. C does not support exception handling.

4. C does not support templates.

5. C does not support namespaces.

6. C does not support references.

7. C does not support default parameter values.

8. C does not support overloaded function names.

9. C does not allow you to declare a variable in a for statement
header (but C99 does!).

10. C requires that all variables of block scope must be declared at
the beginning of the block (not the case with C99!).

11. C does not support the new and delete operators. Dynamic
variables are created using the malloc() and free() functions, as well
as the calloc() and realloc() functions.

12. C does not support the four C++ cast operators static_cast,
dynamic_cast, const_cast and reinterpret_cast. All casts must be done
using the "C-style" cast operator.

13. C does not support inline functions (but C99 does!).

14. C does not support the unary scope operator.

15. C does not support // to start a comment (but C99 does!).

16. C include file names for C libraries are usually different than C+
+ include file names for the same libraries, even though they contain
prototypes for the same C functions. Generally you remove the leading
'c' and append ".h" to the end of the file name. For example the C++
<cstdio> include file is <stdio.h> in C; the C++ <cstdlib> include
file is <stdlib.h> in C; and the C++ <ctime> include file is <time.h>
in C.

17. C does not support the types bool and long long (but C99 does!).
 
J

John Harrison

blangela said:
I am teaching a C programming course to C++ programmers and want to
come up with list of _KEY_ differences between the two languages.
Below is a list I have come up with so far. Have I missed any?

Key Differences between ANSI C and C++

1. C is a procedural programming language (where as C++ is an Object
Oriented Programming language). This means that the C programming
language does not support classes and all functionality provided
directly or indirectly through classes. Therefore C does not support
inheritance, polymorphism or encapsulation of data. C does support
"struct"s, which are like classes without support for member functions
and with no ability to encapsulate the data members that are defined
as belonging to the struct (a struct is an ADT).

C++ is sometimes called a multi-paradigm language, meaning it supports
many styles of programming, including object-orientated (although no two
people ever agree on what that means), procedural, and others.

C can support inheritance, polymorphism and even data encapsulation, it
just that the language was not designed with these in mind, and so the
constructs needed to support these things are more awkward in C than in C++.

By ADT I guess you mean abstract data type. If so then a struct is most
definitely not an abstract data type, whereas a well designed class is.
2. C does not have access to the iostream library. Input and output in
C is done using the stdio.h library which provides access to the
scanf() and printf() functions, used to do input and output
respectively.

3. C does not support exception handling.

4. C does not support templates.

5. C does not support namespaces.

6. C does not support references.

7. C does not support default parameter values.

8. C does not support overloaded function names.

Or overloaded operators.
9. C does not allow you to declare a variable in a for statement
header (but C99 does!).

10. C requires that all variables of block scope must be declared at
the beginning of the block (not the case with C99!).

11. C does not support the new and delete operators. Dynamic
variables are created using the malloc() and free() functions, as well
as the calloc() and realloc() functions.

Dynamic variables is incorrect terminolgy, dynamically allocated memory
is better.

Also worth noting that C99 supports dynamic arrays, i.e.

int n = something();
int array[n];

which is not legal C++.
12. C does not support the four C++ cast operators static_cast,
dynamic_cast, const_cast and reinterpret_cast. All casts must be done
using the "C-style" cast operator.

13. C does not support inline functions (but C99 does!).

14. C does not support the unary scope operator.

Well, it has no need for it.
15. C does not support // to start a comment (but C99 does!).

16. C include file names for C libraries are usually different than C+
+ include file names for the same libraries, even though they contain
prototypes for the same C functions. Generally you remove the leading
'c' and append ".h" to the end of the file name. For example the C++
<cstdio> include file is <stdio.h> in C; the C++ <cstdlib> include
file is <stdlib.h> in C; and the C++ <ctime> include file is <time.h>
in C.

C++ also supports the C include file names.
17. C does not support the types bool and long long (but C99 does!).

C also does not support the wchar_t type as a distinct type (it's a
typedef in C).

Two important things that are not in your list explicitly, are the
concepts of constructors and destructors. Destructors in particular are
one of C++'s key features. No other commonly used language has
destructors that I am aware of.

C++ also has the typeid operator and the typeinfo class.

Seems like a pretty complete list of features, but I think the key
difference is the style in which the two languages are used by competent
programmers. I think C++ is generally programmed with much more thought
given to design issues, C is much more free and easy.

john
 
I

Ian Collins

blangela said:
I am teaching a C programming course to C++ programmers and want to
come up with list of _KEY_ differences between the two languages.
Below is a list I have come up with so far. Have I missed any?
See

http://david.tribble.com/text/cdiffs.htm

Key Differences between ANSI C and C++

1. C is a procedural programming language (where as C++ is an Object
Oriented Programming language).

C++ is a multi-paradigm language.
 
A

Alex Mizrahi

(message (Hello 'blangela)
(you :wrote :eek:n '(3 Mar 2007 12:00:34 -0800))
(

b> I am teaching a C programming course to C++ programmers and want to
b> come up with list of _KEY_ differences between the two languages.
b> Below is a list I have come up with so far. Have I missed any?

imho one of important differences is that C++ has more strict conversion
rules.
i.e. C will allow conversion from void* to any pointer type, while C++
won't.

this can lead to problems when trying to compile C source file with C++
compiler.
(i remember some compiler from Nokia Symbian SDK did not have C mode at all,
so i had to fix conversions to compile C files with C++ compiler).

)
(With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
"?? ???? ??????? ?????")
 
B

blangela

C++ is sometimes called a multi-paradigm language, meaning it supports
many styles of programming, including object-orientated (although no two
people ever agree on what that means), procedural, and others.

C can support inheritance, polymorphism and even data encapsulation, it
just that the language was not designed with these in mind, and so the
constructs needed to support these things are more awkward in C than in C++.

By ADT I guess you mean abstract data type. If so then a struct is most
definitely not an abstract data type, whereas a well designed class is.

By ADT, I mean Aggregate Data Type, which is what a believe a struct
is.

2. C does not have access to the iostream library. Input and output in
C is done using the stdio.h library which provides access to the
scanf() and printf() functions, used to do input and output
respectively.
3. C does not support exception handling.
4. C does not support templates.
5. C does not support namespaces.
6. C does not support references.
7. C does not support default parameter values.
8. C does not support overloaded function names.

Or overloaded operators.


9. C does not allow you to declare a variable in a for statement
header (but C99 does!).
10. C requires that all variables of block scope must be declared at
the beginning of the block (not the case with C99!).
11. C does not support the new and delete operators. Dynamic
variables are created using the malloc() and free() functions, as well
as the calloc() and realloc() functions.

Dynamic variables is incorrect terminolgy, dynamically allocated memory
is better.

Also worth noting that C99 supports dynamic arrays, i.e.

int n = something();
int array[n];

which is not legal C++.


12. C does not support the four C++ cast operators static_cast,
dynamic_cast, const_cast and reinterpret_cast. All casts must be done
using the "C-style" cast operator.
13. C does not support inline functions (but C99 does!).
14. C does not support the unary scope operator.

Well, it has no need for it.

You may be confusing the binary scope operator with the unary scope
operator. The unary scope operator is used to unhide a global
variable in C++ (at least that is one use for it) which I believe
would be just as useful in C.
C++ also supports the C include file names.




C also does not support the wchar_t type as a distinct type (it's a
typedef in C).

Two important things that are not in your list explicitly, are the
concepts of constructors and destructors. Destructors in particular are
one of C++'s key features. No other commonly used language has
destructors that I am aware of.

Since ctors and dtors are member functions, and I have already stated
that classes do not exist, I thought it would be redundant.
C++ also has the typeid operator and the typeinfo class.

Without inheritance and polymorphism, how useful would these be?
Seems like a pretty complete list of features, but I think the key
difference is the style in which the two languages are used by competent
programmers. I think C++ is generally programmed with much more thought
given to design issues, C is much more free and easy.

john- Hide quoted text -

- Show quoted text -

Thanks for your feedback.
 
B

blangela

(message (Hello 'blangela)
(you :wrote :eek:n '(3 Mar 2007 12:00:34 -0800))
(

b> I am teaching a C programming course to C++ programmers and want to
b> come up with list of _KEY_ differences between the two languages.
b> Below is a list I have come up with so far. Have I missed any?

imho one of important differences is that C++ has more strict conversion
rules.
i.e. C will allow conversion from void* to any pointer type, while C++
won't.

this can lead to problems when trying to compile C source file with C++
compiler.
(i remember some compiler from Nokia Symbian SDK did not have C mode at all,
so i had to fix conversions to compile C files with C++ compiler).

)
(With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
"?? ???? ??????? ?????")

That is a good one! I will add it to my list.
 
J

Jacek Dziedzic

blangela said:
I am teaching a C programming course to C++ programmers and want to
come up with list of _KEY_ differences between the two languages.
Below is a list I have come up with so far. Have I missed any?

> [snip]

AFAIK C allows anonymous unions whereas C++ does not.

I _think_ C also has implicit int whereas C++ doesn't.

HTH,
- J.
 
G

Greg Herlihy

I am teaching a C programming course to C++ programmers and want to
come up with list of _KEY_ differences between the two languages.
Below is a list I have come up with so far. Have I missed any?

+ include file names for the same libraries, even though they contain
prototypes for the same C functions. Generally you remove the leading
'c' and append ".h" to the end of the file name. For example the C++
<cstdio> include file is <stdio.h> in C; the C++ <cstdlib> include
file is <stdlib.h> in C; and the C++ <ctime> include file is <time.h>
in C.

You have left out the reason why C++ effectively renames the C
Standard Library headers: to place their contents in the std::
namespace.

Appendix C of the C++ Standard enumerates many of the differences
between the two langugages in the areas in which they overlap. Some of
the differences are obscure (C allows a program to call main(), C++
does not) while some could potentially affect the program's behavior -
such a char literal (like 'a') is an int in C and a char in C++.

Greg
 
I

Ian Collins

Jacek said:
blangela said:
I am teaching a C programming course to C++ programmers and want to
come up with list of _KEY_ differences between the two languages.
Below is a list I have come up with so far. Have I missed any?

[snip]

AFAIK C allows anonymous unions whereas C++ does not.

I _think_ C also has implicit int whereas C++ doesn't.
Implicit int has been removed from C99.
 
C

Clark Cox

I am teaching a C programming course to C++ programmers and want to
come up with list of _KEY_ differences between the two languages.
Below is a list I have come up with so far. Have I missed any?

Key Differences between ANSI C and C++
[snip]


17. C does not support the types bool and long long (but C99 does!).

C++ (currently) doesn't support the long long type either, so this
isn't a difference.

In C++, the type of a character literal (i.e. 'a') is (char), while,
in C, it is (int).

In C, there is an implicit conversion from (void*) to any other object pointer.

In C++, the NULL macro has an integer type, while in C, it can have a
type of (void*)

In C, one cannot overload operators.
 
M

Marcus Kwok

blangela said:
I am teaching a C programming course to C++ programmers and want to
come up with list of _KEY_ differences between the two languages.
Below is a list I have come up with so far. Have I missed any?

Key Differences between ANSI C and C++

I did not see these mentioned yet:

1. C++ does not allow for the old-style function declaration:

int f(a, b)
int a;
int b;
{
int c = a + b;
return c;
}

as opposed to:

int f(int a, int b)
{
// ...
}

2. A function declaration prototype:

int f();

in C means that its parameters are unspecified, wheras in C++ means
that it takes no parameters. In C, to achieve the same effect, you
must say:

int f(void);

3. In C++, when you declare a struct, you no longer need to specify the
'struct' keyword:

struct MyStruct { };
MyStruct ms;

In C, you must do:

struct MyStruct { };
struct MyStruct ms;

or use the typedef trick:

typedef struct MyStruct_ { } MyStruct;
MyStruct ms;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top