Google

Wednesday, March 12, 2008

WHAT ARE THE THINGS WHICH ARE ALLOWED IN C AND C++ BUT WITH DIFFERENT MEANINGS

Some incompatibilities unavoidably arose between C and C++ .They are:


1:

C specifies that a variable declared with a constant qualifier is not a modifiable object and in C it is an external linkage.

ex:

  const int           i = 4;  // External linkage
extern const int j = 7; // 'extern' optional
static const int k = 8; // 'static' required

C++ specifies that a Constant object with file scope has internal linkage by default, meaning that the object's name is not visible outside the source file in which it is declared

ex:

 const int           i = 4;  // Internal linkage
extern const int j = 7; // 'extern' required
static const int k = 8; // 'static' optional
2:

sizeof (’1′) == sizeof (int) in C; but it is sizeof (char) in C++.

3:

C does not allow a given typedef to appear more than once in the same scope.

C++ handles typedefs and type names differently than C, and allows redundant occurrences of a given typedef within the same scope.

Thus the following code is valid in C++ but invalid in C:

ex:

typedef int i
typedef int i; // Valid C++, invalid C


4:


Nested structure types may be declared within other structures.
The scope of the inner structure tag extends outside the scope of the
outer structure in C,but does not do so in C++.Structure declarations possess their own scope in C++
ex:
struct Outer
{
struct Inner // Nested structure declaration
{
int a;
float f;
} in;

enum E // Nested enum type declaration
{
UKNOWN, OFF, ON
} state;
};

struct Inner si; // Nested type is visible in C,
// Not visible in C++

enum E et; // Nested type is visible in C,
// Not visible in C++

4 comments:

gRv said...

this post really helped me

can u tell y java robust,thnx in advance

addiction said...

gud stuff, i hope i'll get detailed tech. discussions here.

addiction said...

n ya its me, prash.

Unknown said...

can i get some more information on discrepancies of C&C++

-----i'll be highly thankful to u for ur fantabulous Blog-OM