By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,691 Members | 1,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,691 IT Pros & Developers. It's quick & easy.

Key Differences Between Ansi C and C++

P: n/a
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++
<cstdioinclude file is <stdio.hin C; the C++ <cstdlibinclude
file is <stdlib.hin C; and the C++ <ctimeinclude file is <time.h>
in C.

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

Mar 3 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
blangela wrote:
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++
<cstdioinclude file is <stdio.hin C; the C++ <cstdlibinclude
file is <stdlib.hin C; and the C++ <ctimeinclude 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
Mar 3 '07 #2

P: n/a
blangela wrote:
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.

--
Ian Collins.
Mar 3 '07 #3

P: n/a
(message (Hello 'blangela)
(you :wrote :on '(3 Mar 2007 12:00:34 -0800))
(

bI am teaching a C programming course to C++ programmers and want to
bcome up with list of _KEY_ differences between the two languages.
bBelow 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)
"?? ???? ??????? ?????")
Mar 3 '07 #4

P: n/a
On Mar 3, 12:28 pm, John Harrison <john_androni...@hotmail.comwrote:
blangela wrote:
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.
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.
>

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++
<cstdioinclude file is <stdio.hin C; the C++ <cstdlibinclude
file is <stdlib.hin C; and the C++ <ctimeinclude 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.
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.
Mar 4 '07 #5

P: n/a
On Mar 3, 3:00 pm, "Alex Mizrahi" <udode...@users.sourceforge.net>
wrote:
(message (Hello 'blangela)
(you :wrote :on '(3 Mar 2007 12:00:34 -0800))
(

bI am teaching a C programming course to C++ programmers and want to
bcome up with list of _KEY_ differences between the two languages.
bBelow 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.

Mar 4 '07 #6

P: n/a
>>
>>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.
ADT is commonly used to mean abstract data type, see for instance here

http://en.wikipedia.org/wiki/Abstract_data_type

I've never heard ADT used to mean aggregate data type before. I don't
think its a useful concept in this case anyway. A class could also be
considered an aggregate data type.

john
Mar 4 '07 #7

P: n/a
blangela wrote:
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.
Mar 4 '07 #8

P: n/a
On Mar 3, 12:00 pm, "blangela" <Bob_Langel...@telus.netwrote:
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?
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++
<cstdioinclude file is <stdio.hin C; the C++ <cstdlibinclude
file is <stdlib.hin C; and the C++ <ctimeinclude 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

Mar 4 '07 #9

P: n/a
Jacek Dziedzic wrote:
blangela wrote:
>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.

--
Ian Collins.
Mar 4 '07 #10

P: n/a
On 2007-03-03 12:00:34 -0800, "blangela" <Bo***********@telus.netsaid:
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.
--
Clark S. Cox III
cl*******@gmail.com

Mar 5 '07 #11

P: n/a
blangela <Bo***********@telus.netwrote:
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;

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Mar 5 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.