When using C++ can you mix a bit of C into it or does it have to be compelty C++
When using C++ can you mix a bit of C into it or does it have to be compelty C++
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++.
>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.
Can you give me an example? I'm not doubting your credibility, I would just like to see an example.
>Can you give me an example?
Here is one of the more common ones:
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.Code:/* ** Valid C, invalid C++. */ char *p; p = malloc ( 1024 );
-Prelude
My best code is written with the delete key.
Dosen't that depend on the compiler?
>Dosen't that depend on the compiler?
Yes, it depends whether it's a C or C++ compiler.
>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.
Maybe, but there are only a few respectable compilers out there. Most are not up to the standards anyway.Originally posted by cyrix
Dosen't that depend on the compiler?
The C and C++ standards are slowly drifting apart. I think that C++ is a specializaton of C.
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.
>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.
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.
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++.
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.
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.Originally posted by Granger9
When using C++ can you mix a bit of C into it or does it have to be compelty C++
Last edited by Troll_King; 08-21-2002 at 03:36 PM.