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'
will generally give different results in the two languages: in C++ it will be 1
, while in C it will be sizeof(int)
which on architectures with 8 bit wide char
will be at least 2
. As another consequence of this type difference, in C 'a'
will always be a signed expression, regardless of whether or not char
is a signed or unsigned type, whereas for C++ this is compiler implementation specific.
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 following function will return different values in C and C++:
extern int T; int size(void) { struct T { int i; int j; }; return sizeof(T); /* C: return sizeof(int) * C++: return sizeof(struct T) */ }This is due to C requiring struct
in front of structure tags (and so sizeof(T)
refers to the variable), but C++ allowing it to be omitted (and so sizeof(T)
refers to the implicit typedef
). Beware that the outcome is different when the extern
declaration is placed inside the function: then the presence of an identifier with same name in the function scope inhibits the implicit typedef
to take effect for C++, and the outcome for C and C++ would be the same. Observe also that the ambiguity in the example above is due to the use of the parenthesis with the sizeof
operator. Using sizeof T
would expect T
to be an expression and not a type, and thus the example would not compile with C++.
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 C99, a new keyword, _Bool
, is introduced as the new boolean type. In many aspects, it behaves much like an unsigned int
, but conversions from other integer types or pointers always constrained to 0
and 1
. Other than for other unsigned types, and as one would expect for a boolean type, such a conversion is 0
if and only if the expression in question evaluates to 0
and it is 1
in all other cases. The header stdbool.h
provides macros bool
, true
and false
that are defined as _Bool
, 1
and 0
, respectively.
Read more about this topic: Compatibility Of C And C++
Famous quotes containing the words constructs, behave and/or differently:
“To me the female principle is, or at least historically has been, basically anarchic. It values order without constraint, rule by custom not by force. It has been the male who enforces order, who constructs power structures, who makes, enforces, and breaks laws.”
—Ursula K. Le Guin (b. 1929)
“Do you think that the things people make fools of themselves about are any less real and true than the things they behave sensibly about?”
—George Bernard Shaw (18561950)
“I have more in common with a Mexican man than with a white woman.... This opinion ... chagrins women who sincerely believe our female physiology unequivocally binds all women throughout the world, despite the compounded social prejudices that daily affect us all in different ways. Although women everywhere experience life differently from men everywhere, white women are members of a race that has proclaimed itself globally superior for hundreds of years.”
—Ana Castillo (b. 1953)