C and C++ - The Difference Please?

Discussion in 'C++' started by Paul Woodward, Jul 19, 2003.

  1. I am new to programming in the C / C++ language and I am using a mixture of
    online materials plus a small collection of C and C++ books.

    What I basically want to know is how do I know if something I am learning is
    from the C Language or the C++ Language and should I avoid mixing the two.

    I am under the impression that C++ is the successor of C but I would imagine
    at the same time there are some functions that have become obsolete under
    C++ from the C language and I want to avoid using them.

    I am developing on a Linux machine and compiling using gcc and simply using
    VIM as a text editor for my source code.

    --

    Kind Regards,

    Paul Woodward
     
    Paul Woodward, Jul 19, 2003
    #1
    1. Advertising

  2. On Sat, 19 Jul 2003 14:34:17 +0100, "Paul Woodward" <> wrote:

    >I am new to programming in the C / C++ language and I am using a mixture of
    >online materials plus a small collection of C and C++ books.
    >
    >What I basically want to know is how do I know if something I am learning is
    >from the C Language or the C++ Language


    There are some subtle syntactical and semantic differences, but for these
    you'll just have to assume that what you see in a C++ book or article is
    proper C++. Don't rely on the compiler. Many compilers allow C constructs,
    such as (C99) dynamic size arrays, in C++ code.

    C doesn't have classes or templates, so all classes and templates are C++.

    Regarding library functions: if you can put "std::" in front of the name,
    then it's C++. However, the opposite is not necessarily true. For example,
    "assert" is implemented as a macro (you cannot put "std::" in front), but
    is available in both C and C++.



    >and should I avoid mixing the two.


    Yes, you should. In particular, avoid C memory allocation (malloc and
    friends). And wherever practical, use C++ standard library classes such
    as std::string and std::vector instead of raw arrays and pointers.

    As a novice, avoid C i/o such as printf; use C++ std::cout and friends
    instead.

    Don't use old-style C headers such as


    #include <stddef.h>


    but use the C++ wrappers such as


    #include <cstddef>




    >I am under the impression that C++ is the successor of C but I would imagine
    >at the same time there are some functions that have become obsolete under
    >C++ from the C language and I want to avoid using them.


    Not many, but see above.



    >I am developing on a Linux machine and compiling using gcc and simply using
    >VIM as a text editor for my source code.


    g++ (gcc) is one of the compilers that by default allows C99 constructs
    in C++ code. That doesn't mean it's a bad compiler. On the contrary,
    but it's a good idea to keep that in mind, and check if there are options
    you can use to restrict the compiler to standard C++.
     
    Alf P. Steinbach, Jul 19, 2003
    #2
    1. Advertising

  3. Alf P. Steinbach wrote:
    > On Sat, 19 Jul 2003 14:34:17 +0100, "Paul Woodward" <> wrote:
    >

    [great suggestion snipped]
    >
    > g++ (gcc) is one of the compilers that by default allows C99 constructs
    > in C++ code. That doesn't mean it's a bad compiler. On the contrary,
    > but it's a good idea to keep that in mind, and check if there are options
    > you can use to restrict the compiler to standard C++.
    >


    One additional suggestion is the version of the gcc - get the latest
    (3.3) and get gcc-3.4 when it comes out.

    The gcc-2.9x C++ compiler is lacking.

    G
     
    Gianni Mariani, Jul 19, 2003
    #3
  4. (Alf P. Steinbach) wrote in message news:<>...
    > g++ (gcc) is one of the compilers that by default allows C99 constructs
    > in C++ code. That doesn't mean it's a bad compiler. On the contrary,
    > but it's a good idea to keep that in mind, and check if there are options
    > you can use to restrict the compiler to standard C++.


    In particular, the -pedantic and probably -ansi options.

    Tommy McDaniel
     
    Tommy McDaniel, Jul 20, 2003
    #4
  5. "Paul Woodward" <> wrote in message
    news:3f1948d9$0$18486$...
    > I am new to programming in the C / C++ language and I am using a mixture

    of
    > online materials plus a small collection of C and C++ books.
    >
    > What I basically want to know is how do I know if something I am learning

    is
    > from the C Language or the C++ Language and should I avoid mixing the two.
    >
    > I am under the impression that C++ is the successor of C but I would

    imagine
    > at the same time there are some functions that have become obsolete under
    > C++ from the C language and I want to avoid using them.
    >
    > I am developing on a Linux machine and compiling using gcc and simply

    using
    > VIM as a text editor for my source code.
    >
    > --
    >
    > Kind Regards,
    >
    > Paul Woodward
    >
    >


    To start off with read the classic programming text for "C" : "The C
    Programming Language" by Kernighan and Ritchie.
    All "C" programs are C++ programs. Since essentially C++ is a superset of
    "C" with object-oriented syntax and semantics added,
    mainly objects and classes. ( C uses structs).

    Then for a one of the best tutorial reference , refer to ( borowed from a
    library ) or get yourself a copy of "The C++ Programming Language"
    by Bjarne Stroustrup( the inventor of C++). It is a bit hard to read at
    first, so you may refer to a less terse C++ tutorial
    book , to help break you into the Strousop book. Depending on your
    background, choose for yourself a beginner
    C++ tutorial book(suitable for your understanding level), because I dont
    know your programming history or coding experience.
    Both books mentioned above are quite easy to find both have white covers


    Use pico , advanced editor or xemacs as programming editor for editing
    source code ( as your confidence grows.) vim or vi
    is a bit hard for a Unix or Linux beginner.

    Regards,
    Gavin
     
    Govindan Chandran, Jul 20, 2003
    #5
  6. On Sun, 20 Jul 2003 21:54:53 +0800, "Govindan Chandran" <> wrote:

    >All "C" programs are C++ programs.


    Otherwise good advice, but the above is incorrect.

    It's not difficult to write code that compiles as both C and C++,
    which means writing in the common subset of the two languages.

    But C is not a proper subset of C++.
     
    Alf P. Steinbach, Jul 20, 2003
    #6
  7. Paul Woodward

    Steinar Guest

    > But C is not a proper subset of C++.

    That's especially true after C99. The following things are some of the
    features *not* supported in C++ but are in the new C standard
    (according to Steven Prata, C primer plus, 4th ed.)

    * Restricted pointers,
    * Variable-length arrays
    * Flexible array members
    * Macros with a variable number of arguments

    and more
     
    Steinar, Jul 20, 2003
    #7
  8. Stroustrup had some very informative articles on this subject in the C++
    User's Journal (http://www.cuj.com/); they should still be available online,
    so I won't summarize them here.
     
    Anthony Albert Nassar, Jul 23, 2003
    #8
    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.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,228
  2. KK
    Replies:
    2
    Views:
    595
    Big Brian
    Oct 14, 2003
  3. Nick Timkovich
    Replies:
    0
    Views:
    69
    Nick Timkovich
    Feb 25, 2014
  4. Skip Montanaro
    Replies:
    0
    Views:
    65
    Skip Montanaro
    Feb 25, 2014
  5. Peter Otten
    Replies:
    1
    Views:
    87
    Mark Lawrence
    Feb 25, 2014
Loading...

Share This Page