Fundamental Concepts / Compatiblity with C
Subtitles of the Movie
C++ originated as a pre-processor that transformed classes and templates and such into C. So at first the two languages were identical. Since then the languages have drifted apart and the two have some differences. Mostly they are the same and if you program in one language, you can program in the other. You could even mix the two by linking modules together. Most compilers that compile one language also compile the other. The way the compiler tells them apart determines which language is being compiled is most often by the file's name. Almost all programmers that work in one language also write programs in the other. But to be able to mix the two, you need to be aware of some differences. In C, a void pointer can be assigned to a pointer of any type and it will be converted automatically. This is a simple example. The void pointer is assigned to an int pointer and it becomes an int pointer. In C++, you have to specially cast the pointer to its new type. Casting it this way works in both languages, so the casting in portable. This means that in either language you can allocate memory and store its address in a pointer with (unintelligible) by simply casting. As long as you cast all address conversions, it will work in both languages. The comma operator works a bit differently in the two languages. In C the expressions are evaluated left to right. The comma is a sequence point. The residual value is that of the second expression. Therefore, both J and K are assigned to the value two. In C++, this order is not guaranteed. A few key words have been added to C++ and they're not key words in C. These are perfectly valid as variables or function names in C but will not work in C Plus Plus. In C++, you can use type def to redefine the same symbol a number of times in one source file. In C, this is an error. Redefinition is not allowed. Enums are different. In C, enums are always ints. In C++, the enum symbols can be different data types. Some functions in the library of C Plus Plus have been overloaded with const parameters and const return values. For example, the function STRSTR, which locates sub-strings, if passed const pointers will return a const pointer. But this is an overload. The original form of the function still exists. You can define a struct within a struct in both languages but in C, the internal struct definition can be used to declare standalone structs. In C++ it can only be used as a struct member as it's defined. So far everything I've described will be caught by the compiler. That is, they work in one language but will cause errors in the other one's compiler. These are easy to catch. The subtle ones are those that do one thing in one language and do something else in the other. For example, character literals are a different data type. The size of operator on a character literal is different. In C, a character literal is type int so the size of operator in this example will result in four. In C++, it's a care type so the result will be one. In C Plus Plus, any declaration declared as a const is limited to file scope unless it is explicitly declared as an extern. In C, extern is the default. It has to be declared as static to a limited file scope. Inline functions in C are limited to file scope but they have global scope in C++. Both C and C++ have Boolean data types but they are a little different. In C the built-in Boolean type is named underscore Bool. In C++ it's simply named Bool. There is a C header named STDBool.h that allows you to use the lowercase name. This way the two have the same name but they are still a little bit different. They both have constants named true and false. But the C Boolean type is an unsigned int constrained to the values zero and one. But the main concern comes when linking modules together. The problem is that C++ considers the list of argument types to part of the function name. Combining all this information into one symbol is known as mangling. C doesn't mangle and C++ does so the same name in the two languages is not really the same name. So the trick to getting the two to link together is to suppress the name mangling in C++. You can do that in C++ by declaring C linkage. That is you define a symbol as having an external C style name and it won't be mangled. You can have several functions defined this way. You can even link to libraries of C code as long as you prevent mangling this way. You may find other differences and no doubt the differences between the two languages will increase as time goes along. But as long as the method of passing arguments stays the same and you're able to prevent name mangling in C++, the two languages should remain linkable.
Tutorial Information
| Course: | Advanced C Programming |
| Author: | Arthur Griffith |
| SKU: | 33965 |
| ISBN: | 1-935320-24-6 |
| Release Date: | 2009-01-30 |
| Duration: | 5.5 hrs / 82 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 