A char isn't 2 bytes in C++, it's one - it's the size of a char literal that
changes between C and C++:

#include <stdio.h>

int main()
{
     char x;
     printf("sizeof(char)=%d, sizeof x = %d, sizeof('a')=%d\n", \
          sizeof(char), sizeof x, sizeof('a'));
}
.
But Peter's program isn't demonstrating anything about the size of chars.

It it helps to figure out what's happening, there are two sorts of 'foobar'
there - the global one outside of main, and the struct foobar inside of
main.

In C, inside main after the struct declaration, the expression
sizeof(foobar) relates to the one outside of main().

In C++, inside main() after the struct declaration, the expression
sizeof(foobar) relates to the struct foobar inside of main, since in C++ all
the following are equivilant when used in such a context:
  class foobar
  struct foobar
  foobar

In other words, since 'foobar' is now shorthand for '[class|struct] foobar'
in C++, the declaration of struct foobar in main() 'hides' the global
version of it.

There is no such shorthand in C, thus 'foobar' and 'struct foobar' refer to
different entities.

On Thu, Jun 25, 2009 at 1:39 AM, wolexzo2 <[email protected]> wrote:

> Interesting.
> But could you elucidate why a global char is 2 bytes in c++ to begin with
> and why it is not the same in c.
>
> thanks;
> Lawal.O
>
>
> --- In [email protected], "peternilsson42" <peternilsso...@...>
> wrote:
> >
> > Sanjeev Gopinath <sanjeevsince90@> wrote:
> > >
> > > Hello all,
> > > I've some series of questions...!
> > > * Is there any difference when a C program runs in a C++
> > > compiler?
> >
> > There's no such thing as a C program to a C++ compiler.
> >
> > The following is a C program. Tell me which language your
> > C++ compiler thinks it is...
> >
> >   #include <stdio.h>
> >
> >   char foobar;
> >
> >   int main()
> >   {
> >     struct foobar { char a[2]; };
> >
> >     if (sizeof(foobar) == sizeof(struct foobar))
> >       puts("C++ compiler.");
> >     else
> >       puts("C compiler.");
> >   }
> >
> > >   (I mean the approaches taken internally by the C++ compiler)
> >
> > A C++ compiler will, by definition compile C++ programs, whatever
> > the source code author's notion of which language it was written
> > in.
> >
> > These days, C and C++ compilers tend to be bundled together.
> > Modern compilers (e.g. GNU) will actually translate the source
> > language into an intermediate language anyway, thus using the
> > same optimisors and assemblers for both.
> >
> > But there have been systems where C++ calling conventions are
> > different to C calling conventions. There are also subtle
> > differences in the languages which mean that a C++ compiler
> > must treat the same line of code differently to a C compiler.
> >
> > > * Will the C++ compiler understand it as a C program?
> >
> > No more than a COBOL or FORTRAN compiler will understand it
> > as a C program.
> >
> > > If so, how does it differentiate?
> >
> > It doesn't. Some command line 'compilers' are really wrappers,
> > detecting the file extension and passing the source to the
> > appropriate 'real' compiler. The real compilers however
> > generally only understand one language.
> >
> > > * Will there be any storage difference between a C and C++
> > > compiler?
> >
> > Storage difference?
> >
> > C++ implementations tend to be much larger than C ones because
> > the language is considerably more complex, and C++ has a much
> > larger standard library.
> >
> > --
> > Peter
> >
>
>
>
>
> ------------------------------------
>
> To unsubscribe, send a blank message to <mailto:
> [email protected]>.Yahoo! Groups Links
>
>
>
>


-- 
PJH

http://shabbleland.myminicity.com/com
http://www.chavgangs.com/register.php?referer=9375


[Non-text portions of this message have been removed]

Reply via email to