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

diff C C++

P: n/a
I've been compiling a list of the differences between C and C++, and
I'd like to see how thorough I've been. Could any of you go over this
list and see if I've missed anything?

Here's the list:

Mid-scope declarations
Default function parameters
new/delete operators
Better implementation of const
References
Exception handling
"Catch all" catch block
Classes
Constructors/Destructors
Access modifiers
Inheritance (multiple inheritance too)
Templates (classes and functions; template specialization)
Initialization Lists
Implicit constructors
Friend access
Namespaces
Anonymous unions
Better casts (type) type(var) static_cast<typeconst_cast<type>
reinterpret_cast<typedynamic_cast<type>
Inline functions
Enhanced static
Member function/data pointers
Virtual functions (pure)
Const pointers
mutable keyword
Anonymous namespaces
You don't need struct, union, enum tags
:: (scope resolution operator)
Function overloading
Operator overloading
Runtime type identification
C++ style includes
// Comments
Functions as an l-value (must return reference)
structs can include functions
this pointer
bool keyword (is it a C++ keyword?)

I/O mechanisms
STL

Thanks!

--
-Rob Hoelz
Dec 20 '06 #1
Share this Question
Share on Google+
17 Replies


P: n/a
Rob Hoelz wrote:
I've been compiling a list of the differences between C and C++, and
I'd like to see how thorough I've been. Could any of you go over this
list and see if I've missed anything?

Here's the list:

Mid-scope declarations
Default function parameters
new/delete operators
Better implementation of const
References
Exception handling
"Catch all" catch block
All catches are a difference from C... isn't the above line redundant
with the preceding line?
Classes
Constructors/Destructors
Access modifiers
Inheritance (multiple inheritance too)
Templates (classes and functions; template specialization)
Initialization Lists
Implicit constructors
Friend access
Namespaces
Anonymous unions
Better casts (type) type(var) static_cast<typeconst_cast<type>
reinterpret_cast<typedynamic_cast<type>
Inline functions
Enhanced static
Member function/data pointers
Virtual functions (pure)
Const pointers
mutable keyword
Anonymous namespaces
You don't need struct, union, enum tags
:: (scope resolution operator)
Function overloading
Operator overloading
Runtime type identification
C++ style includes
// Comments
Functions as an l-value (must return reference)
structs can include functions
As may unions.
this pointer
bool keyword (is it a C++ keyword?)
Yes, it's a keyword in C++.
I/O mechanisms
That description's pretty vague... obviously C has I/O mechanisms...
STL
This is more of a list of differences between the 1990 version of C and
C++, rather than differences between C (which is now defined by the
1999 standard) and C++. If it were the latter, differences such as //
comments, bool (though it's not a keyword in C), inline functions, and
"Mid-scope declarations" would go away (though there would still be
some related differences). New differences such as C's "restrict"
keyword, compound literals, C's complex number implementation, and
Variable Length Arrays would need to be listed.

Here are some further differences between C90 and C++ that spring to
mind:

Removal of non-prototype function declarations.
Removal of implicit int (also removed from C99)
The type of string literals (const char[] vs char*)
The type of character literals (char vs int)
Implicit conversion to/from void* has been removed.
main() cannot be called recursively in C++ (can in C)
Conversion between object pointers and (unsigned char *) is not
explicitly well-defined as it is in C

.... if you really want to be pedantic about such differences, you could
sit the C and C++ language specifications side-by-side, and compare
differences in related sections. That would take quite a while, but you
could end up with a pretty exhaustive list that way :)

-Micah

Dec 20 '06 #2

P: n/a
Rob Hoelz wrote:
I've been compiling a list of the differences between C and C++, and
I'd like to see how thorough I've been. Could any of you go over this
list and see if I've missed anything?

Here's the list:

Mid-scope declarations
Min-scope declarations are in C99 as well
Default function parameters
new/delete operators
Better implementation of const
References
Exception handling
"Catch all" catch block
I'd say that "Catch all" catch blocks falls under "Exception handling"
Classes
Constructors/Destructors
Access modifiers
Inheritance (multiple inheritance too)
Templates (classes and functions; template specialization)
Initialization Lists
Implicit constructors
Friend access
Namespaces
Anonymous unions
Better casts (type) type(var) static_cast<typeconst_cast<type>
reinterpret_cast<typedynamic_cast<type>
Inline functions
Inline functions are in C99
Enhanced static
What do you mean by this?
Member function/data pointers
Virtual functions (pure)
Const pointers
Const pointers are in C
mutable keyword
Anonymous namespaces
You don't need struct, union, enum tags
:: (scope resolution operator)
Function overloading
Operator overloading
Runtime type identification
C++ style includes
What do you mean by "C++ style includes"?
// Comments
These are available in C99 also
Functions as an l-value (must return reference)
structs can include functions
this pointer
bool keyword (is it a C++ keyword?)
Yes, it is a keyword. (C99 has the _Bool keyword, BTW)
>
I/O mechanisms
STL

Thanks!

--
Clark S. Cox III
cl*******@gmail.com
Dec 20 '06 #3

P: n/a
"Clark S. Cox III" <cl*******@gmail.comwrote:
Rob Hoelz wrote:
I've been compiling a list of the differences between C and C++, and
I'd like to see how thorough I've been. Could any of you go over
this list and see if I've missed anything?

Here's the list:

Mid-scope declarations

Min-scope declarations are in C99 as well
I've based this off of C89.
>
Default function parameters
new/delete operators
Better implementation of const
References
Exception handling
"Catch all" catch block

All catches are a difference from C... isn't the above line redundant
with the preceding line?
I'd say that "Catch all" catch blocks falls under "Exception handling"
If I post this list elsewhere, I will remove this.
>
Classes
Constructors/Destructors
Access modifiers
Inheritance (multiple inheritance too)
Templates (classes and functions; template specialization)
Initialization Lists
Implicit constructors
Friend access
Namespaces
Anonymous unions
Better casts (type) type(var) static_cast<typeconst_cast<type>
reinterpret_cast<typedynamic_cast<type>
Inline functions

Inline functions are in C99
Once again, C89.
>
Enhanced static

What do you mean by this?
I mean that the meaning of static in C++ has been expanded from the
original meaning in C.
>
Member function/data pointers
Virtual functions (pure)
Const pointers

Const pointers are in C
mutable keyword
Anonymous namespaces
You don't need struct, union, enum tags
:: (scope resolution operator)
Function overloading
Operator overloading
Runtime type identification
C++ style includes

What do you mean by "C++ style includes"?
I mean include statements like this:
#include <cstring>
#include <iostream>
>
// Comments

These are available in C99 also
Functions as an l-value (must return reference)
structs can include functions
As may unions.
Thanks for this addition!
this pointer
bool keyword (is it a C++ keyword?)

Yes, it is a keyword. (C99 has the _Bool keyword, BTW)

I/O mechanisms

That description's pretty vague... obviously C has I/O mechanisms...
By this I simply meant the difference between using stdio and iostream.
STL

Thanks!

Thanks for all of the corrections, feel free to keep them coming!

--
-Rob Hoelz
Dec 20 '06 #4

P: n/a
Rob Hoelz wrote:
structs can include functions
As they can in C.

--
Ian Collins.
Dec 20 '06 #5

P: n/a
Ian Collins <ia******@hotmail.comwrote:
Rob Hoelz wrote:
structs can include functions

As they can in C.
rob@TheRing ~ $ gcc -o test test.c
test.c:6: error: field 'foo' declared as a function

Doesn't seem like it.

-Rob Hoelz
Dec 20 '06 #6

P: n/a
Rob Hoelz wrote:
Ian Collins <ia******@hotmail.comwrote:

>>Rob Hoelz wrote:

>>>structs can include functions

As they can in C.
rob@TheRing ~ $ gcc -o test test.c
test.c:6: error: field 'foo' declared as a function

Doesn't seem like it.
struct X
{
void (*fn)( struct X* );
};

void xFn( struct X* this ) {}

int main()
{
struct X x;
x.fn = xFn;
}

--
Ian Collins.
Dec 20 '06 #7

P: n/a
Ian Collins <ia******@hotmail.comwrote:
Rob Hoelz wrote:
Ian Collins <ia******@hotmail.comwrote:

>Rob Hoelz wrote:
structs can include functions

As they can in C.
rob@TheRing ~ $ gcc -o test test.c
test.c:6: error: field 'foo' declared as a function

Doesn't seem like it.

struct X
{
void (*fn)( struct X* );
};

void xFn( struct X* this ) {}

int main()
{
struct X x;
x.fn = xFn;
}
But that's a function pointer, not a function...

--
-Rob Hoelz
Dec 20 '06 #8

P: n/a
Rob Hoelz wrote:
"Clark S. Cox III" <cl*******@gmail.comwrote:
>>Rob Hoelz wrote:
>>>I've been compiling a list of the differences between C and C++, and
I'd like to see how thorough I've been. Could any of you go over
this list and see if I've missed anything?
[snipped items from his list]
>>>C++ style includes

What do you mean by "C++ style includes"?


I mean include statements like this:
#include <cstring>
#include <iostream>
In which regards does this #include statement differ from the C #include
statement? AFAIK, the pre-compiler from C89 only differs from C++
pre-compiler with regard to the handling of one-line-comments.

Regards,
Stuart
Dec 20 '06 #9

P: n/a
Stuart Redmann <De*******@web.dewrote:
Rob Hoelz wrote:
"Clark S. Cox III" <cl*******@gmail.comwrote:
>Rob Hoelz wrote:

I've been compiling a list of the differences between C and C++,
and I'd like to see how thorough I've been. Could any of you go
over this list and see if I've missed anything?

[snipped items from his list]
>>C++ style includes

What do you mean by "C++ style includes"?

I mean include statements like this:
#include <cstring>
#include <iostream>

In which regards does this #include statement differ from the C
#include statement? AFAIK, the pre-compiler from C89 only differs
from C++ pre-compiler with regard to the handling of
one-line-comments.

Regards,
Stuart
Here's the difference:
#include <string.his a C-style include. It includes the file
extension.

#include <iostreamis a C++-style include; it doesn't include the
extension.

#include <cstring>

This is another C++-style include; including the standard C string
library. This has the same effect as the C-style include I demonstrated
before; but this one will only work in C++.

I realize the difference is rather superficial, but like I said, I just
wanted to be thorough.
--
-Rob Hoelz
Dec 20 '06 #10

P: n/a
On Dec 20, 11:30 am, Rob Hoelz <h...@wisc.eduwrote:
Stuart Redmann <DerTop...@web.dewrote:
Rob Hoelz wrote:
I mean include statements like this:
#include <cstring>
#include <iostream>
In which regards does this #include statement differ from the C
#include statement? AFAIK, the pre-compiler from C89 only differs
from C++ pre-compiler with regard to the handling of
one-line-comments.
Regards,
StuartHere's the difference:
#include <string.his a C-style include. It includes the file
extension.

#include <iostreamis a C++-style include; it doesn't include the
extension.

#include <cstring>

This is another C++-style include; including the standard C string
library. This has the same effect as the C-style include I demonstrated
before; but this one will only work in C++.
Not exactly the same effect AFAIK, <cstringplaces the functions and
whatnot in <string.hin the std-namespace.

--
Erik Wikström

Dec 20 '06 #11

P: n/a
Rob Hoelz <ho***@wisc.eduwrote:
I've been compiling a list of the differences between C and C++, and
I'd like to see how thorough I've been. Could any of you go over this
list and see if I've missed anything?

Here's the list:
[list redacted]

A fairly comprehensive document detailing the incompatibilities between
C++ and various versions of C can be found here:

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

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Dec 20 '06 #12

P: n/a

Rob Hoelz wrote:
Ian Collins <ia******@hotmail.comwrote:
Rob Hoelz wrote:
Ian Collins <ia******@hotmail.comwrote:
>
>
>>Rob Hoelz wrote:
>>
>>
>>>structs can include functions
>>
>>As they can in C.
>>
rob@TheRing ~ $ gcc -o test test.c
test.c:6: error: field 'foo' declared as a function
>
Doesn't seem like it.
struct X
{
void (*fn)( struct X* );
};

void xFn( struct X* this ) {}

int main()
{
struct X x;
x.fn = xFn;
}

But that's a function pointer, not a function...

--
-Rob Hoelz
I think Rob is right. If C can have a function definition inside a
struct, would the struct become a "class" ?

Anyway, a struct having a function pointer should be in C from day one.

Dec 21 '06 #13

P: n/a
Ian Collins wrote:
Rob Hoelz wrote:
>Ian Collins <ia******@hotmail.comwrote:

>>Rob Hoelz wrote:
structs can include functions
As they can in C.
rob@TheRing ~ $ gcc -o test test.c
test.c:6: error: field 'foo' declared as a function

Doesn't seem like it.

struct X
{
void (*fn)( struct X* );
};

void xFn( struct X* this ) {}

int main()
{
struct X x;
x.fn = xFn;
}
That is a struct that contains a pointer (nothing special there), *not*
a struct that contains a function. A struct that contains a function
looks like this (valid C++, invalid C):

struct X
{
void fn() {}
};
--
Clark S. Cox III
cl*******@gmail.com
Dec 22 '06 #14

P: n/a

Rob Hoelz wrote:
I've been compiling a list of the differences between C and C++, and
I'd like to see how thorough I've been. Could any of you go over this
list and see if I've missed anything?

Here's the list:

Mid-scope declarations
Default function parameters
new/delete operators
Better implementation of const
References
Exception handling
"Catch all" catch block
Classes
Constructors/Destructors
Access modifiers
Inheritance (multiple inheritance too)
Templates (classes and functions; template specialization)
Initialization Lists
Implicit constructors
Friend access
Namespaces
Anonymous unions
Better casts (type) type(var) static_cast<typeconst_cast<type>
reinterpret_cast<typedynamic_cast<type>
Inline functions
Enhanced static
Member function/data pointers
Virtual functions (pure)
Const pointers
mutable keyword
Anonymous namespaces
You don't need struct, union, enum tags
:: (scope resolution operator)
Function overloading
Operator overloading
Runtime type identification
C++ style includes
// Comments
Functions as an l-value (must return reference)
I suspect that the above statement is _NOT_ corrrect. Here is an
example that compiles on Comeau that shows a function being used as an
l-value returning a pointer, _NOT_ a reference. I suspect this code
would compile on C compiler as well (substituting printf() etc. for
cout etc of course).

#include <iostream>
int * func();
int main()
{
*func() = 20; // will print 10 the first time
func(); // should print 20 the second time.
return 0;
}

int * func()
{
static int i = 10;
std::cout << i << std::endl;
return &i;
}

structs can include functions
this pointer
bool keyword (is it a C++ keyword?)

I/O mechanisms
STL

Thanks!

--
-Rob Hoelz
Dec 22 '06 #15

P: n/a
blangela wrote:
Rob Hoelz wrote:
>Functions as an l-value (must return reference)

I suspect that the above statement is _NOT_ corrrect. Here is an
example that compiles on Comeau that shows a function being used as an
l-value returning a pointer, _NOT_ a reference.
The function is not returning an lvalue, it is returning a pointer.
Dereferencing that pointer (which is an rvalue) yields an lvalue.
I suspect this code
would compile on C compiler as well (substituting printf() etc. for
cout etc of course).

#include <iostream>
int * func();
int main()
{
*func() = 20; // will print 10 the first time
func(); // should print 20 the second time.
return 0;
}

int * func()
{
static int i = 10;
std::cout << i << std::endl;
return &i;
}
--
Clark S. Cox III
cl*******@gmail.com
Dec 22 '06 #16

P: n/a

Clark S. Cox III wrote:
blangela wrote:
Rob Hoelz wrote:
Functions as an l-value (must return reference)
I suspect that the above statement is _NOT_ corrrect. Here is an
example that compiles on Comeau that shows a function being used as an
l-value returning a pointer, _NOT_ a reference.

The function is not returning an lvalue, it is returning a pointer.
Dereferencing that pointer (which is an rvalue) yields an lvalue.
I suspect this code
would compile on C compiler as well (substituting printf() etc. for
cout etc of course).

#include <iostream>
int * func();
int main()
{
*func() = 20; // will print 10 the first time
func(); // should print 20 the second time.
return 0;
}

int * func()
{
static int i = 10;
std::cout << i << std::endl;
return &i;
}

--
Clark S. Cox III
cl*******@gmail.com
The end result is the same - that a function can be invoked on the
left hand side of an assignment operator. So even why bother mentioning
the fact that a C++ function can return an L-value?

Dec 23 '06 #17

P: n/a
blangela wrote:
Clark S. Cox III wrote:
>blangela wrote:
>>Rob Hoelz wrote:
Functions as an l-value (must return reference)
I suspect that the above statement is _NOT_ corrrect. Here is an
example that compiles on Comeau that shows a function being used as an
l-value returning a pointer, _NOT_ a reference.
The function is not returning an lvalue, it is returning a pointer.
Dereferencing that pointer (which is an rvalue) yields an lvalue.
>>I suspect this code
would compile on C compiler as well (substituting printf() etc. for
cout etc of course).

#include <iostream>
int * func();
int main()
{
*func() = 20; // will print 10 the first time
func(); // should print 20 the second time.
return 0;
}

int * func()
{
static int i = 10;
std::cout << i << std::endl;
return &i;
}

The end result is the same - that a function can be invoked on the
left hand side of an assignment operator. So even why bother mentioning
the fact that a C++ function can return an L-value?
Because it is a difference between the two languages; and that is what
the OP was looking for.
--
Clark S. Cox III
cl*******@gmail.com
Dec 23 '06 #18

This discussion thread is closed

Replies have been disabled for this discussion.