C or C++

Discussion in 'C++' started by Carmen Sei, Apr 9, 2008.

  1. Carmen Sei

    Carmen Sei Guest

    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?
     
    Carmen Sei, Apr 9, 2008
    #1
    1. Advertising

  2. Carmen Sei

    Christopher Guest

    On Apr 9, 12:31 pm, Carmen Sei <> wrote:
    > 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
     
    Christopher, Apr 9, 2008
    #2
    1. Advertising

  3. Carmen Sei

    Guest

    On Apr 9, 12:39 pm, "Alf P. Steinbach" <> wrote:

    > 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).
     
    , Apr 9, 2008
    #3
  4. Carmen Sei

    osmium Guest

    "Carmen Sei" writes:

    > 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
     
    osmium, Apr 9, 2008
    #4
  5. Carmen Sei

    Brian Tyler Guest

    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.
     
    Brian Tyler, Apr 9, 2008
    #5
  6. Carmen Sei

    Eric Johnson Guest

    On Apr 9, 10:31 am, Carmen Sei <> wrote:
    > 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.
     
    Eric Johnson, Apr 9, 2008
    #6
  7. Carmen Sei

    Guest

    On Apr 9, 1:37 pm, "Alf P. Steinbach" <> wrote:
    > * :
    > >  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).

    >
    > 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?
     
    , Apr 9, 2008
    #7
  8. Carmen Sei

    Ian Collins Guest

    wrote:
    > On Apr 9, 12:39 pm, "Alf P. Steinbach" <> wrote:
    >
    >> 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).


    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.

    --
    Ian Collins.
     
    Ian Collins, Apr 9, 2008
    #8
  9. Carmen Sei

    Guest

    On Apr 9, 2:13 pm, Ian Collins <> wrote:
    > wrote:
    > > On Apr 9, 12:39 pm, "Alf P. Steinbach" <> wrote:

    >
    > >> 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).

    >
    > 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.
     
    , Apr 9, 2008
    #9
  10. Carmen Sei wrote:
    > 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.
     
    Ioannis Vranos, Apr 9, 2008
    #10
  11. Clarification:


    Ioannis Vranos wrote:
    > Carmen Sei wrote:
    >> 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++ ==> alone 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.
     
    Ioannis Vranos, Apr 9, 2008
    #11
  12. Carmen Sei

    Ian Collins Guest

    Ioannis Vranos wrote:
    > Carmen Sei wrote:
    >> 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).


    No, they are not "minor exceptions".

    --
    Ian Collins.
     
    Ian Collins, Apr 9, 2008
    #12
  13. Ian Collins wrote:
    > Ioannis Vranos wrote:
    >> Carmen Sei wrote:
    >>> 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).

    >
    > 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?
     
    Ioannis Vranos, Apr 9, 2008
    #13
  14. Carmen Sei

    Ian Collins Guest

    Ioannis Vranos wrote:
    > Ian Collins wrote:
    >> Ioannis Vranos wrote:
    >>> Carmen Sei wrote:
    >>>> 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).

    >> 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.
    >
    > Have I forgotten anything?


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

    --
    Ian Collins.
     
    Ian Collins, Apr 9, 2008
    #14
  15. Ian Collins wrote:
    > Ioannis Vranos wrote:
    >> Ian Collins wrote:
    >>> Ioannis Vranos wrote:
    >>>> Carmen Sei wrote:
    >>>>> 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).
    >>> 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.
    >>
    >> Have I forgotten anything?

    >
    > Implicit int, missing function prototypes, use of const,


    Right, those too.


    > assignments to
    > enums...



    An example?
     
    Ioannis Vranos, Apr 9, 2008
    #15
  16. Carmen Sei

    Ian Collins Guest

    Ioannis Vranos wrote:
    > Ian Collins wrote:
    >> Ioannis Vranos wrote:
    >>> Ian Collins wrote:
    >>>> Ioannis Vranos wrote:
    >>>>> Carmen Sei wrote:
    >>>>>> 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).
    >>>> 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.
    >>>
    >>> Have I forgotten anything?

    >> Implicit int, missing function prototypes, use of const,

    >
    > Right, those too.
    >

    I wouldn't call any of the above "minor exceptions".

    >> assignments to
    >> enums...

    >
    > An example?
    >

    typedef enum { one, two } E;

    E e = 42;

    --
    Ian Collins.
     
    Ian Collins, Apr 9, 2008
    #16
  17. Carmen Sei

    Rolf Magnus Guest

    Alf P. Steinbach wrote:


    >> What do you mean by "valid C++"?

    >
    > 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.

    >> 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).

    >
    > 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.
     
    Rolf Magnus, Apr 10, 2008
    #17
  18. Carmen Sei

    Rolf Magnus Guest

    Alf P. Steinbach wrote:

    > * Rolf Magnus:
    >> Alf P. Steinbach wrote:
    >>
    >>
    >>>> What do you mean by "valid C++"?
    >>> 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".

    >
    > 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).

    >> 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.

    >
    > 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.
     
    Rolf Magnus, Apr 10, 2008
    #18
  19. Rolf Magnus wrote:
    >
    > 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).


    >> 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 agree with that. The C++ standard must become self-contained.
     
    Ioannis Vranos, Apr 10, 2008
    #19
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page