Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C program is standard C++ program?

Reply
Thread Tools

C program is standard C++ program?

 
 
John Carson
Guest
Posts: n/a
 
      10-22-2005
"Michael Mair" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)
> John Carson wrote:
>> "Martin Ambuhl" <(E-Mail Removed)> wrote in message
>> news:F8o6f.19590$(E-Mail Removed) nk.net
>>
>>> http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>>>
>>>> I want if "a C program is a standard C++ program, but not vice
>>>> versa" is a correct statement?
>>>
>>> It is a grossly erroneous statement. C and C++ are different
>>> languages. There are countless C programs that are not C++
>>> programs.

>>
>> "C++ was developed from the C programming language and, with few
>> exceptions, retains C as a subset."
>> Bjarne Stroustrup, The C++ Programming Language, 3rd ed., p. 8.

>
> This may have been the intent; however, Martin's statement still is
> true as C++ semantics differ from C89 in many points.


The statement that "C++ ... with few exceptions retains C as a subset" is
not a statement of intent but a description of the relationship between the
two languages, albeit one that seems to be directed at C89. I quote again:

"With minor exceptions, C++ is a superset of C. Most differences stem from
C++'s greater emphasis on type checking. Well-written C programs tend to be
C++ programs as well."
Bjarne Stroustrup, The C++ Programming Language, 3rd ed., Appendix B,
Compatibility, p. 816.

> C99 introduced enough additional non-trivial differences which cannot
> be easily overcome.


That does indeed add to the incompatibility, though there are efforts
underway to bring the two languages closer together, particularly by adding
C99 stuff to C++.

> The C standard does not make any claim w.r.t. C++
> compatibility, even though there may be a rationale saying exactly
> that. I do not own the C++ standard, so I cannot say anything from
> this point of view.


Section 1.1/2 of the C++ standard:

"C++ is a general purpose programming language based on the C programming
language as described in ISO/IEC 9899:1990 Programming languages - C (1.2).
In addition to the facilities provided by C, C ++ provides additional data
types, classes, templates, exceptions, namespaces, inline functions,
operator overloading, function name overloading, references, free store
management operators, and additional library facilities."

Annex C (appropriately enough) of the C++ standard deals with the
relationship between C and C++, noting the areas of incompatibility.

--
John Carson

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      10-22-2005
John Carson wrote:

> "Well-written C programs tend to be C++ programs as well."
> Bjarne Stroustrup, The C++ Programming Language, 3rd ed., Appendix B,
> Compatibility, p. 816.


I tend to disagree. In C, dynamic memory is usually allocated with malloc or
other functions that return a pointer to void. That pointer gets converted
implicitly into the target type in C, and a cast should be avoided in a
"well-written C program". However, in C++, it doesn't compile without a
cast. Dynamic memory is something that is used quite often, so I'd say that
most well-written C programs are not C++ programs and would need lots of
changes (in all places where dynamic memory is allocated).

 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      10-22-2005
"Rolf Magnus" <(E-Mail Removed)> wrote in message
news:djddml$nl5$01$(E-Mail Removed)-online.com
> John Carson wrote:
>
>> "Well-written C programs tend to be C++ programs as well."
>> Bjarne Stroustrup, The C++ Programming Language, 3rd ed., Appendix B,
>> Compatibility, p. 816.

>
> I tend to disagree. In C, dynamic memory is usually allocated with
> malloc or other functions that return a pointer to void. That pointer
> gets converted implicitly into the target type in C, and a cast
> should be avoided in a "well-written C program". However, in C++, it
> doesn't compile without a cast. Dynamic memory is something that is
> used quite often, so I'd say that most well-written C programs are
> not C++ programs and would need lots of changes (in all places where
> dynamic memory is allocated).


Your point is covered in the preceding sentence: "Most differences stem from
C++'s greater emphasis on type checking." But I guess that this preceding
sentence somewhat gives the lie to the one that follows, so strictly
speaking you are probably right that "most well-written C programs are not
C++ programs".

I am more sceptical about your claim that they would need "lots of changes".
Well written C programs will hide most of the malloc calls inside
initialization functions --- manual equivalents of constructors --- and the
number of these shouldn't be very large.

--
John Carson

 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      10-22-2005
John Carson wrote:
> "Michael Mair" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)
>
>> John Carson wrote:
>>
>>> "Martin Ambuhl" <(E-Mail Removed)> wrote in message
>>> news:F8o6f.19590$(E-Mail Removed) nk.net
>>>
>>>> (E-Mail Removed) wrote:
>>>>
>>>>> I want if "a C program is a standard C++ program, but not vice
>>>>> versa" is a correct statement?
>>>>
>>>> It is a grossly erroneous statement. C and C++ are different
>>>> languages. There are countless C programs that are not C++
>>>> programs.
>>>
>>> "C++ was developed from the C programming language and, with few
>>> exceptions, retains C as a subset."
>>> Bjarne Stroustrup, The C++ Programming Language, 3rd ed., p. 8.

>>
>> This may have been the intent; however, Martin's statement still is
>> true as C++ semantics differ from C89 in many points.

>
> The statement that "C++ ... with few exceptions retains C as a subset" is
> not a statement of intent but a description of the relationship between the
> two languages, albeit one that seems to be directed at C89. I quote again:
>
> "With minor exceptions, C++ is a superset of C. Most differences stem from
> C++'s greater emphasis on type checking. Well-written C programs tend to be
> C++ programs as well."
> Bjarne Stroustrup, The C++ Programming Language, 3rd ed., Appendix B,
> Compatibility, p. 816.


Hmmm, my C code tends to look quite different from my C++ code.
Things I find quite acceptable in C are not acceptable in C++
where better solutions to address certain problems exist.

Apart from the different semantics of void* and ambiguities w.r.t.
overloaded functions, there are better ways to perform explicit
type conversions in C++ than the good old C-style cast.


>> C99 introduced enough additional non-trivial differences which cannot
>> be easily overcome.

>
> That does indeed add to the incompatibility, though there are efforts
> underway to bring the two languages closer together, particularly by adding
> C99 stuff to C++.


Yes, AFAIR there were a couple of good articles by B.Stroustrup in CUJ
about the areas where this is easily possible and where in his opinion,
either C++ or C should change semantics. They may still be available
online.


>> The C standard does not make any claim w.r.t. C++
>> compatibility, even though there may be a rationale saying exactly
>> that. I do not own the C++ standard, so I cannot say anything from
>> this point of view.

>
> Section 1.1/2 of the C++ standard:
>
> "C++ is a general purpose programming language based on the C programming
> language as described in ISO/IEC 9899:1990 Programming languages - C (1.2).
> In addition to the facilities provided by C, C ++ provides additional data
> types, classes, templates, exceptions, namespaces, inline functions,
> operator overloading, function name overloading, references, free store
> management operators, and additional library facilities."
>
> Annex C (appropriately enough) of the C++ standard deals with the
> relationship between C and C++, noting the areas of incompatibility.


Thank you for the information


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
Branimir Maksimovic
Guest
Posts: n/a
 
      10-22-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>I want if "a C program is a standard C++ program, but not vice versa"
> is a correct statement?
>
> In a C++ program, we can use standard C libraries.


No we can't

extern "C"{
void strcpy(char* restrict , const char* restrict);
}

Standard C++ compiler should spit error.
Only non standard C++ compiler with appropriate
extensions can use standard C libraries.

However, we cannot
> use C++ libraries inside C program.


Yes we can
extern "C" void myfunc(int){ MyClass o; } // defines C function
// that can be called from C

Greetings, Bane.


 
Reply With Quote
 
Branimir Maksimovic
Guest
Posts: n/a
 
      10-22-2005

"Branimir Maksimovic" <(E-Mail Removed)> wrote in message
news:djdjg2$dav$(E-Mail Removed)...
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>>I want if "a C program is a standard C++ program, but not vice versa"
>> is a correct statement?
>>
>> In a C++ program, we can use standard C libraries.

>
> No we can't
>
> extern "C"{
> void strcpy(char* restrict , const char* restrict);

void strcpy(char* restrict to, const char* restrict from);
> }
>
> Standard C++ compiler should spit error.

Worse, if there are no variables it can interpret "restrict" as parameter
name
instead of type qualifier

> Only non standard C++ compiler with appropriate
> extensions can use standard C libraries.
>



 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-22-2005
Michael Mair <(E-Mail Removed)> writes:
> John Carson wrote:

[...]
>> The statement that "C++ ... with few exceptions retains C as a
>> subset" is not a statement of intent but a description of the
>> relationship between the two languages, albeit one that seems to be
>> directed at C89. I quote again: "With minor exceptions, C++ is a
>> superset of C. Most differences stem from C++'s greater emphasis on
>> type checking. Well-written C programs tend to be C++ programs as
>> well."
>> Bjarne Stroustrup, The C++ Programming Language, 3rd ed., Appendix B,
>> Compatibility, p. 816.

>
> Hmmm, my C code tends to look quite different from my C++ code.
> Things I find quite acceptable in C are not acceptable in C++
> where better solutions to address certain problems exist.
>
> Apart from the different semantics of void* and ambiguities w.r.t.
> overloaded functions, there are better ways to perform explicit
> type conversions in C++ than the good old C-style cast.


Stroustrup's statement is that well-written C programs tend to be C++
programs, not that well-written C programs tend to be *well-written*
C++ programs.

The former statement is basically true apart from the issue of casting
the result of malloc(). The latter is not.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-22-2005
"Branimir Maksimovic" <(E-Mail Removed)> writes:
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>>I want if "a C program is a standard C++ program, but not vice versa"
>> is a correct statement?
>>
>> In a C++ program, we can use standard C libraries.

>
> No we can't
>
> extern "C"{
> void strcpy(char* restrict , const char* restrict);
> }
>
> Standard C++ compiler should spit error.
> Only non standard C++ compiler with appropriate
> extensions can use standard C libraries.


Presumably a header intended to be used by both C and C++ compilers
could have

#ifdef __cplusplus
#define restrict
#endif

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      10-22-2005
John Carson wrote:

> "With minor exceptions, C++ is a superset of C. Most differences stem from
> C++'s greater emphasis on type checking. Well-written C programs tend to be
> C++ programs as well."
> Bjarne Stroustrup, The C++ Programming Language, 3rd ed., Appendix B,
> Compatibility, p. 816.


This is an example of why one should *not* cite Stroustrup as an
authority on this issue. He is obviously an authority on C++, but the
fact is that well-written C programs tend to be uncompilable as C++.
Best C practice is often illegal in C++. Remember that BS has a horse
in this race.
 
Reply With Quote
 
Branimir Maksimovic
Guest
Posts: n/a
 
      10-22-2005

"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Branimir Maksimovic" <(E-Mail Removed)> writes:
>> <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) oups.com...
>>>I want if "a C program is a standard C++ program, but not vice versa"
>>> is a correct statement?
>>>
>>> In a C++ program, we can use standard C libraries.

>>
>> No we can't
>>
>> extern "C"{
>> void strcpy(char* restrict , const char* restrict);
>> }
>>
>> Standard C++ compiler should spit error.
>> Only non standard C++ compiler with appropriate
>> extensions can use standard C libraries.

>
> Presumably a header intended to be used by both C and C++ compilers
> could have
>
> #ifdef __cplusplus
> #define restrict
> #endif


This can be dangerous. restrict type qualifier has specific meaning to C
compiler,
and does not mean anything to C++ compiler, so C++ calling C function with
such parameter can lead to undefined behavior,
therefore it is better to live that as it is.

Greetings, Bane.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
standard libraries don't behave like standard 'libraries' Sriram Srinivasan Python 13 11-12-2009 06:05 PM
What are the standard network functions provided in standard C? disappearedng@gmail.com C Programming 5 06-10-2008 08:57 PM
add pexpect to the standard library, standard "install" mechanism. funkyj Python 5 01-20-2006 08:35 PM
C program is standard C++ program? strutsng@gmail.com C++ 50 10-30-2005 10:28 PM
How standard is the standard library? steve.leach Python 1 04-18-2005 04:07 PM



Advertisments