Groups | Search | Server Info | Login | Register


Groups > comp.lang.c.moderated > #465

Re: Why sizeof(main) = 1?

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c.moderated
Subject Re: Why sizeof(main) = 1?
Date 2013-09-06 23:25 -0500
Organization A noiseless patient Spider
Message-ID <clcm-20130906-0002@plethora.net> (permalink)
References <clcm-20130104-0002@plethora.net> <clcm-20130902-0002@plethora.net>

Show all headers | View raw


On 09/02/2013 05:07 AM, kzelechowski@e3tech.local wrote:
> (sizeof main == 1) because (main) is treated as a logical value.  Compare: 
> { if (main) yadda; }

Why are you resurrecting a thread that started eight months ago, and was
completely resolved six months ago?

The behavior of an if() statement depends upon whether the if-condition
compares equal to 0. You could think of if(main) as if it evaluates
main==0, though that's not how the C standard describes it. You might
think that an equality comparison expression should have "a logical
value" - but this is C, and as originally designed, C didn't have have a
logical type (_Bool was a late addition, in C99). In C the expression
main==0 has the type 'int'  It's possible that sizeof(int)==1, but it's
not exactly common. For that matter, the standard doesn't say anything
about sizeof(_Bool), either; it could be > 1.

The if(main) example relies upon the fact that "... a function
designator with type ‘‘function returning type’’ is converted to an
expression that has type ‘‘pointer to function returning type’’."
(6.3.2.1p4). However, the "..." in the above citation represents the
following words: "Except when it is the operand of the sizeof operator,
the _Alignof operator,65) or the unary & operator, ...". Therefore, no
such conversion occurs in the sizeof expression (this is from n1570.pdf,
the final draft of C2011 - I think _Alignof may have been removed from
that sentence in the actual standard).
In fact, such code violates a constraint: "The sizeof operator shall not
be applied to an expression that has function type, ..." (6.5.3.4p1)

I you had bothered to read Jason Betts' response, you would already be
aware of (6.5.3.4p1). He said that it "... does not require a dignostic
...", but I'm afraid that's true only because he misspelled diagnostic.
6.5.3.4 is a Constaints* section. At least one diagnostic is required
for any program that violates a constraint.

* - spelling complaints are required to contain at least one spelling
mistake.
-- 
James Kuyper
-- 
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line.  Sorry.

Back to comp.lang.c.moderated | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Why sizeof(main) = 1? Myth__Buster <raghavanil4m@gmail.com> - 2013-01-04 18:14 -0600
  Re: Why sizeof(main) = 1? Barry Schwarz <schwarzb@dqel.com> - 2013-02-26 10:51 -0600
    Re: Why sizeof(main) = 1? Keith Thompson <kst-u@mib.org> - 2013-03-11 18:25 -0500
  Re: Why sizeof(main) = 1? Jasen Betts <jasen@xnet.co.nz> - 2013-02-26 10:51 -0600
  Re: Why sizeof(main) = 1? gordonb.k8xjg@burditt.org (Gordon Burditt) - 2013-02-26 10:51 -0600
  Re: Why sizeof(main) = 1? <kzelechowski@e3tech.local> - 2013-09-02 04:07 -0500
    Re: Why sizeof(main) = 1? James Kuyper <jameskuyper@verizon.net> - 2013-09-06 23:25 -0500
      Re: Why sizeof(main) = 1? Keith Thompson <kst-u@mib.org> - 2013-09-11 17:26 -0500
        Re: Why sizeof(main) = 1? James Kuyper <jameskuyper@verizon.net> - 2013-09-12 11:29 -0500
    Re: Why sizeof(main) = 1? Keith Thompson <kst-u@mib.org> - 2013-09-06 23:25 -0500
      Re: Why sizeof(main) = 1? Robert Wessel <robertwessel2@yahoo.com> - 2013-09-11 17:27 -0500
        Re: Why sizeof(main) = 1? James Kuyper <jameskuyper@verizon.net> - 2013-09-12 11:29 -0500
        Re: Why sizeof(main) = 1? Keith Thompson <kst-u@mib.org> - 2013-09-12 11:30 -0500
    Re: Why sizeof(main) = 1? Ken Brody <kenbrody@spamcop.net> - 2013-09-06 23:25 -0500

csiph-web