Thread: C & C++

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    C & C++

    When using C++ can you mix a bit of C into it or does it have to be compelty C++

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    8
    C++ is a superset of C, meaning all C code is valid C++. However, C++ code is linked differently, if you want to retain C style linking you need to add some preprocessor directives. This probably isn't important unless you are writing some sort of library. So yeah, you can use any valid C in C++.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So yeah, you can use any valid C in C++.
    Most valid C, but not all. There are situations where valid C is invalid C++.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    8
    Can you give me an example? I'm not doubting your credibility, I would just like to see an example.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Can you give me an example?
    Here is one of the more common ones:
    Code:
    /*
    ** Valid C, invalid C++.
    */
    char *p;
    p = malloc ( 1024 );
    Casting malloc is required in C++ but optional in C, and in C it is recommended that the cast not be included because it can hide other potential problems.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    8
    Dosen't that depend on the compiler?

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    >Dosen't that depend on the compiler?

    Yes, it depends whether it's a C or C++ compiler.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Dosen't that depend on the compiler?
    No, it is because C++ is more strongly typed than C so implicit conversion of void pointers is disallowed. Any conforming C compiler will allow this, and any conforming C++ compiler will not.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Originally posted by cyrix
    Dosen't that depend on the compiler?
    Maybe, but there are only a few respectable compilers out there. Most are not up to the standards anyway.

    The C and C++ standards are slowly drifting apart. I think that C++ is a specializaton of C.

  10. #10
    Registered User
    Join Date
    Aug 2002
    Posts
    8
    Guess that's defined by ANSI C++ (the whole casting thing) didn't know that, but you learn something new everyday. That means some of those C++ books and tutorials are wrong about "all C is valid C++". Watch out everyone.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >That means some of those C++ books and tutorials are wrong
    God forbid, some C++ books are wrong?!

    -Prelude
    My best code is written with the delete key.

  12. #12
    Shadow12345
    Guest
    C is crap and everyone that programs in C should get extra high taxes and basically excommunicated from society...either that or beat up with pillows by 13 angry pygmys.

  13. #13
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    when they call C a subset of C++ they aren't saying that ALL syntax without exception is carried over but that the language for the most part is carried over. This is a pretty legitimate claim considering the fact that you have to dig into something like a cast to say they are different. Also, you can't use "new" as a variable name in C++ where you can in C. well DUH!! That doesn't make C less of a subset. It's still pretty much contained in C++. So yes, you can use C code in C++.

  14. #14
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    The C programming language and the C++ programming language have different standards. In some cases people use a C compiler when writing C code and a C++ compiler when writing C++ code. They are different langauges.

    I think that C++ supports almost all of the langauge features of ANSI C, even most of the C99 features.

    The C++er should use libraries such as the STL and take advantage of the design features of OOP.

  15. #15
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784

    Re: C & C++

    Originally posted by Granger9
    When using C++ can you mix a bit of C into it or does it have to be compelty C++
    No, not really, but C++ has link compatibility with C. Most of what you learned in C is part of C++ through inheritance of ANSI C.
    Last edited by Troll_King; 08-21-2002 at 03:36 PM.

Popular pages Recent additions subscribe to a feed