[LCP]sizeof question.

Greg Black gjb at gbch.net
Sat Sep 21 15:24:01 UTC 2002


David wrote:

| If sizeof performs differently under c++ then say so and I will post my 
| question to the c++ list.

I partly answered this previously, but neglected to address the
actual point.  Yes, the sizeof operator does give different
results in C from those it gives in C++.  The standard example
is the following program, which prints different results when
compiled under C than it gives when compiled under C++.

    #include <stdio.h>

    int main(void)
    {
	printf("sizeof 'a' = %d\n", sizeof 'a');
	return 0;
    }

Observe:

    $ cc -o size-c size.c
    $ c++ -o size-cpp size.c
    $ ./size-c
    sizeof 'a' = 4
    $ ./size-cpp
    sizeof 'a' = 1

People with the interest can doubtless provide other examples of
the differences, but this single illustration demonstrates why
you cannot use a C++ compiler to compile C code and expect the
results to be correct.

Greg



More information about the linuxCprogramming mailing list