by EGreg · 7y ago · 0 comments
int main()
{
int a = 8;
{
int a = 4;
/* a is only scoped to this block */
}
printf("%d", a); /* prints 8 */
}
It is also why C++ is not a strict superset of CA perhaps obscure feature is that you can "unshadow" a global variable like this:
#include <stdio.h>
int global = 0;
int main()
{
int global = 1;
{
extern int global;
printf("%d\n", global); // prints 0
}
}Can you explain? That code in C++ also scopes ‘a’ to the block.
EDIT: I see you’ve edited the code, but I think it’s still true in C++. I’ve often done that for RAII and unless I’m mistaken it works just as well when shadowing variables like you’re doing as when not.
#include <stdio.h>
int main() {
printf("%d\n", sizeof('a'));
}
It's the second thing C++ mentions in its list of incompatibility with C (the first is "new keywords").A more obscure difference is this program:
int i;
int i;
int main() {
return i;
}
It's legal C but not legal C++.So if sizeof ('a') == 1, then either you're compiling as C++ or you're compiling as C under a rather odd implementation.
Both POSIX and Windows require CHAR_BIT==8. The only systems I know of with CHAR_BIT>8 are for digital signal processors (DSPs).
If you want to tell whether you're compiling C or C++:
#ifdef __cplusplus
...
#else
...
#fi int main() {
return 4//**/2
;
}