C or C++

C

Carmen Sei

if I need to write C++, I will be forced to learn C automatically?

Since I saw many C++ code need to call C library also.

Most program use a combination of C++ code and calling C functions.

Then writting C++, I cannot avoid learning C right?
 
C

Christopher

if I need to write C++, I will be forced to learn C automatically?
False

Since I saw many C++ code need to call C library also.

If a C++ library calls a C function under the covers, it does not mean
you need to know C. If _you_ call a C function than you might, but if
a "C++ library" _forces_ you to call a C function, it is not a "C++
library"
Most program use a combination of C++ code and calling C functions.

Really? how many years have you spent reading g the source code of
every library in existence?
Maybe you could say that the slim amount of programs that you've
examined the source code for do so. But that doesn't mean that those
programs were written well or that they set a global precedent.
Then writting C++, I cannot avoid learning C right?

False
 
L

lbonafide

C++ is a different
language, and valid C code is seldom valid C++ code except where it's written to
the common subset (e.g. in header files meant to work with both languages)..

Really? What do you mean by "valid C++"? I would think most C
programs would compile with a C++ compiler, unless they use keywords
introduced in C++ as identifiers (e.g. new, delete).
 
O

osmium

Carmen Sei said:
if I need to write C++, I will be forced to learn C automatically?

Since I saw many C++ code need to call C library also.

Most program use a combination of C++ code and calling C functions.

Then writting C++, I cannot avoid learning C right?

That's a difficult question to answer briefly. I suggest you browse around
on the page in the link, looking at things that seem to be germane to your
question. My brief answer would be that only a beginner or superficial C++
programmer does not also know C.

http://www.research.att.com/~bs/bs_faq.html#C-is-subset
 
B

Brian Tyler

Occasionally I have needed C libraries in C++, the only thing that I have
noticed as being different is the way that memory is managed. Quite often
before you use custom types from the C library they need to be
initialized, after they are done with destroyed. But that is library
specific and how it's done should be documented.
 
E

Eric Johnson

if I need to write C++, I will be forced to learn C automatically?

Since I saw many C++ code need to call C library also.

Most program use a combination of C++ code and calling C functions.

Then writting C++, I cannot avoid learning C right?

Yes and no. C++ is based on C, so naturally there is a large amount
of overlap between the two languages. In learning C++, you will also
learn some aspects of C programming. In my experience, there are many
C++ programmers who learned C before C++, so they tend to use C-style
even when writing C++. It may benefit your programming career to at
least become familiar with the C standard library and to become
familiar with C, so when you encounter C++ written in a more
traditional C style you will not have as much trouble understanding
it.

If your goal is to learn C++, I would recommend concentrating on
learning the best practices for C++ first and learning C as a 2nd
priority.
 
L

lbonafide

* (e-mail address removed):

You're right that unless a C program is invalid as C++, it's valid.

And depending on your definition of "most" the complete sentence might arguably
be trivially true.

However, it doesn't seem like a very meaningful comment.

No less meaningful than your comment that C code is "seldom" valid C+
+. Can you expound on that or are you claiming proof by strong
assertion?
 
I

Ian Collins

Really? What do you mean by "valid C++"? I would think most C
programs would compile with a C++ compiler, unless they use keywords
introduced in C++ as identifiers (e.g. new, delete).

No, there are a number of differences between C and the C like subset of
C++. The most common difference is automatic conversion from void* to
any pointer type. This makes the idiomatic C form

T* p = malloc(someSize)

illegal in C++.

There are many more.
 
L

lbonafide

No, there are a number of differences between C and the C like subset of
C++.  The most common difference is automatic conversion from void* to
any pointer type.  This makes the idiomatic C form

T* p = malloc(someSize)

illegal in C++.

There are many more.

I stand educated.
 
I

Ioannis Vranos

Carmen said:
if I need to write C++, I will be forced to learn C automatically?

Since I saw many C++ code need to call C library also.

Most program use a combination of C++ code and calling C functions.

Then writting C++, I cannot avoid learning C right?


With minor exceptions C95 (ISO/IEC 9899:1995) is a subset of C++
(ISO/IEC 14882:2003). More accurately it is almost the entire functional
paradigm subset of C++ (C++ supports 4 paradigms completely, the
functional paradigm, the object oriented paradigm, the generic
programming paradigm (templates), and the modular paradigm (namespaces).

Each paradigm is supported well with close to optimal space and time
efficiencies.

If the question is about learning C before C++, it is not needed.
Actually it is not recommended. Learn C++ only if you do not need to
know C programming.

Currently C (ISO/IEC 9899:1999) and C++ (ISO/IEC 14882:2003) are two
different languages.
 
I

Ioannis Vranos

Clarification:


Ioannis said:
With minor exceptions C95 (ISO/IEC 9899:1995) is a subset of C++
(ISO/IEC 14882:2003). More accurately it is almost the entire functional
paradigm subset of C++ (C++ supports 4 paradigms completely, the
functional paradigm, the object oriented paradigm, the generic
programming paradigm (templates), and the modular paradigm (namespaces).

Each paradigm is supported well with close to optimal space and time
efficiencies.

If the question is about learning C before C++, it is not needed.
Actually it is not recommended. Learn C++ ==> alone if you do not need to
 
I

Ioannis Vranos

Ian said:
No, they are not "minor exceptions".


Supposing you are not talking about the word "exception", the exceptions
I know are the following:


1. Not implicit void * to any other pointer type conversion, as in C95.
2. 'a' is a char in C++ and not an int as in C95.
3. 0 is to be preferred than NULL, which does not apply to C95.
4. POD types can be considered as char/unsigned char sequences in C++,
but only as unsigned char sequences in C95.
5. Empty parentheses in function declarations/definitions are equivalent
to void in C++, but is a different thing in C95.


(6?) The following is guaranteed to not compile in C95, but it compiles
in my C++ compiler, but I am not sure if it is a difference or a
compiler-thing:


int main(void)
{
register char array[]= "Test";

char *p= array;

return 0;
}


Have I forgotten anything?
 
I

Ian Collins

Ioannis said:
Supposing you are not talking about the word "exception", the exceptions
I know are the following:


1. Not implicit void * to any other pointer type conversion, as in C95.
2. 'a' is a char in C++ and not an int as in C95.
3. 0 is to be preferred than NULL, which does not apply to C95.
4. POD types can be considered as char/unsigned char sequences in C++,
but only as unsigned char sequences in C95.
5. Empty parentheses in function declarations/definitions are equivalent
to void in C++, but is a different thing in C95.

Have I forgotten anything?

Implicit int, missing function prototypes, use of const, assignments to
enums...
 
R

Rolf Magnus

Alf P. Steinbach wrote:

The C programming language is defined by a set of international standards.

Usually when we refer to "C" we mean the previous standard, C89 (a.k.a.
C90).

C89/C90 is not a standard. It ceased to be one about 9 years ago. Not sure
if you mean that by "the previous standard". Btw: It's kind of strange that
the C++ standard refers to C90 in some places, even says that the whole
language is based on it, but you can't order its definition anymore, since
it's been replaced by C99. In my eyes, that makes the C++ standard severely
broken and incomplete.
You're right that unless a C program is invalid as C++, it's valid.

And depending on your definition of "most" the complete sentence might
arguably be trivially true.

I don't think that it is for a reasonable definition of "most". (To the OP)
One example would be calls to the function malloc(). Its return value is of
type pointer to void, and in C, the general advice it to not cast it when
assigning it to a pointer of different type. In C++ however, you'll get an
error if you assign it to other pointer types without casting it. So "most"
C programs that use malloc will not compile as C++ without a modification.
 
R

Rolf Magnus

Alf said:
* Rolf Magnus:

Yes, that's correct (the first literally in an impractical formalistic
narrow interpretation,

It's how the C99 standard officially defines it. And it also results from
the fact that you can't obtain C89, neither from ANSI, nor from ISO. You
just need to happen to know someone who has an old printed copy (or have
one on your own) to be able to read it. I'd say that's pretty impractical,
too. I know that there are still lots of compilers that have broken or no
C99 support, even though it has been there for almost 10 years, and that
there are also lots of C programmers that ignore C99 (maybe for just that
reason, or maybe because they just don't care at all).
The current C++ standard was adopted in 1998, before C99. ;-)

That doesn't change the fact that it's broken now. In the period from 1998
to 1999, it was ok. If you don't have access to an old C90 document, there
is no way to get a full definition of ISO C++. IMHO, that's not an
acceptable state for an international standard.
The 2003 version is not new standard but Technical Corrigendum 1 (TC1).

It could not change the reference to the C standard.

It's a reference to something that's not available, so it's still broken. It
could have had an addenum that contained the relevant parts of C90.
 
I

Ioannis Vranos

Rolf said:
That doesn't change the fact that it's broken now. In the period from 1998
to 1999, it was ok. If you don't have access to an old C90 document, there
is no way to get a full definition of ISO C++. IMHO, that's not an
acceptable state for an international standard.


Actually the C subset if C++ is C95 (9899:1995 - Amendment 1).

It's a reference to something that's not available, so it's still broken. It
could have had an addenum that contained the relevant parts of C90.


I agree with that. The C++ standard must become self-contained.
 

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

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top