Question:
What is there in C that is not in C++?
Shiv
2007-01-24 12:35:36 UTC
Please do not tell the difference between two languages. Precisely tell the features of C language that are absent in C++.
Eight answers:
manmeen s
2007-01-26 23:18:25 UTC
The C and C++ programming languages are closely related. C++ grew out of C and is mostly a superset of the latter. Due to this, C code is often developed with C++ IDEs, integrated with C++ code, and compiled in C++ compilers. While most C source code will compile as C++ code without any changes, certain language differences prevent C++ from being a strict superset of C.



C++ introduces many features that are not available in C — C++ code is not valid C code. Here, however, we focus on differences that cause valid C code to be invalid C++ code, or to be valid in both languages but to behave differently in C and C++.



Bjarne Stroustrup, the creator of C++, has suggested [1] that the incompatibilities between C and C++ should be reduced as much as possible in order to maximize inter-operability between the two languages. Others have argued that since C and C++ are two different languages, compatibility between them is useful but not vital; according to this camp, efforts to reduce incompatibility should not hinder attempts to improve each language in isolation. The official rationale for the 1999 C standard (C99) [2] "endorse[d] the principle of maintaining the largest common subset" between C and C++ "while maintaining a distinction between them and allowing them to evolve separately," and was "content to let C++ be the big and ambitious language."



Several additions of C99 are not supported in C++ or conflict with C++ features, such as variadic macros, compound literals, variable-length arrays, and native complex-number types. The long long int datatype is also defined in C99, but not included in any official C++ standard. On the other hand, C99 has reduced some other incompatibilities by incorporating C++ features such as // comments and mixed declarations and code.





[edit] Constructs valid in C but not C++

One commonly encountered difference is that C allows a void* pointer to be assigned to any pointer type without a cast, whereas C++ does not; this idiom appears often in C code using malloc memory allocation. For example, the following is valid in C but not C++:



void* ptr;

int *i = ptr; /* Implicit conversion from void* to int* */

or similarly:



int *j = malloc(sizeof(int) * 5); /* Implicit conversion from void* to int* */

In order to make the code compile in both C and C++, one must use an explicit cast:



void* ptr;

int *i = (int *) ptr;

int *j = (int *) malloc(sizeof(int) * 5);

Another portability issue from C to C++ are the numerous additional keywords that C++ introduced. This makes C code that uses them as identifiers invalid in C++. For example:



struct template {

int new;

struct template* class;

};

is legal C code, but is rejected by a C++ compiler, since the keywords "template", "new" and "class" are reserved.



C++ compilers prohibit goto from crossing an initialization, as in the following C99 code:



void fn(void){

goto flack;

int i = 1;

flack:

;

}

There are many other C syntaxes which are invalid or behave differently in C++ [3]:



The comma operator can result in an "l-value" (a quantity that can be used for the left-hand side of an assignment) in C++, but not in C.

C does not allow a given typedef to be duplicated in the same scope, whereas C++ allows redundant typedefs.

Enumeration constants (enum values) are always of type int in C, whereas they are distinct types in C++ and may have size different from int.

C++ identifiers are not allowed to contain two or more consecutive underscores, whereas C identifiers may.

C++ also changes some C standard-library functions to add additional const qualifiers, e.g. strchr returns char* in C and const char* in C++.

In both C and C++ one can define nested struct types, but the scope is interpreted differently (in C++, a nested struct is defined only within the scope/namespace of the outer struct).

Non-prototype ("K&R"-style) function declarations are not allowed in C++, although they have also been deprecated in C since 1990. Similarly, implicit function declarations (using functions that have not been declared) are not allowed in C++, but have also been deprecated in C since 1999.

C allows struct, union, and enum types to be declared in function prototypes, whereas C++ does not. A struct, union, or enum declaration in C++ declares an implicit typedef, while in C it does not.

In order to intermix C and C++ code, any C++ functions which are to be called from C-compiled code must be declared as extern "C".





[edit] Constructs that behave differently in C and C++

There are a few syntactical constructs that are valid in both C and C++, but produce different results in the two languages.



For example, character literals such as 'a' are of type int in C and of type char in C++, which means that sizeof('a') gives different results in the two languages.



The static keyword is used in C to restrict a function or global variable to file scope (internal linkage). This is also valid in C++, although C++ deprecates this usage in favor of anonymous namespaces (which are not available in C). Also, C++ implicitly treats any const global as file scope unless it is explicitly declared extern, unlike C in which extern is the default. Conversely, inline functions in C are of file scope whereas they have external linkage by default in C++.



Several of the other differences from the previous section can also be exploited to create code that compiles in both languages but behaves differently. For example, the implicit typedefs created by C++ struct declarations can change the meaning of an identifier in a given scope.



Both C99 and C++ have a boolean type bool with constants true and false, but they behave differently. In C++, bool is a built-in type and a reserved keyword. In C, these keywords and types are only defined if the program includes the stdbool.h header, and may be redefined by the programmer.





[edit] References

Detailed comparison , sentence by sentence, from a C Standard perspective.

Incompatibilities Between ISO C and ISO C++, David R. Tribble (2001).
Jay
2007-01-25 13:17:51 UTC
C++ has stronger types than C IIRC. Thus in C you could assign a void pointer the address of a character array without type coercion.



C has a simpler/different way to declare functions and prototypes as well as the standard way you'd declare them in C++.



"Traditional C" also supports a funky way of declaring functions

Instead of



float max(float x, float y)

{ ... rest of function ..}



you could do



float max(x,y)

float x,y; /** argument types listed after the definition **/

{ ... rest of function ..}



Because C is not type strong, doesn't have overloading, etc, there is no mangling of function names, so if one wanted to call C routines from Assembly, its quite easy to complile against and link because function names will not be mangled.



C++ has to mangle names due to function overloading, so linking to a C++ function call is a touch tricker).



otherwise, C++ functionality is a superset of C.
2016-03-29 04:38:52 UTC
Because you're formatting as a double instead of an integer seems the most logical answer. It's been a long time for me and I never liked C but I had Turbo C once.
Hex
2007-01-24 12:58:03 UTC
*sigh*...



C allows for a greater deal of output structure. You have the

ability to specify the format, location, and type of the variables

and data types that are used and displayed. C++ on the other hand

is more straight forward by using the << operator to signify the

break or change in data IO within objects.



That being the only Real thing that C has that C++ lacks.





- Void
bonesawosu
2007-01-24 12:43:32 UTC
your question is a bit backwards. Although its done differently-- everything from C made it to C++. i.e. C++ is C...+ +. But that doesnt mean that C is worthless because it usually has a much cleaner looking source code than what C++ would produce.



-Aaron Joseph
2007-01-24 21:11:01 UTC
C has more of pointers.... But C++ has more of classes, inheritances, etc makes it better.

Actually C++ is derived from C so watever C has.... C++ will also have except some unwamted stuff
Kokopelli
2007-01-24 12:38:47 UTC
It is the other way around. C++ has things that plain old C does not. For example, you build C++ interfaces using objects like drop-down lists and such. In C, you would have to create all of those or buy DLL libraries that contained them, then link them into your programs.
2007-01-24 12:42:19 UTC
Everything in C is in C++.



Thanks



Roland


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...