Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!usc!snorkelwacker.mit.edu!bloom-beacon!eru!hagbard!sunic!mcsun!ukc!mucs!logitek!grep!frank From: frank@grep.co.uk (Frank Wales) Newsgroups: comp.lang.c Subject: Re: sizeof() confusion Message-ID: <1990Nov15.141004.1668@grep.co.uk> Date: 15 Nov 90 14:10:04 GMT References: <9156@latcs1.oz.au> <27432@mimsy.umd.edu> <2665@cirrusl.UUCP> <1990Nov10.123204.8968@druid.uucp> <2692@cirrusl.UUCP> Reply-To: frank@grep.co.uk (Frank Wales) Organization: Grep Limited, LEEDS, UK Lines: 42 In article <2692@cirrusl.UUCP> dhesi%cirrusl@oliveb.ATC.olivetti.com (Rahul Dhesi) writes: >If characters are promoted to ints in expressions, then why isn't >|sizeof c| equivalent to |sizeof (int) c|? The confusion arises >because the term "expression" is defined differently in the definition >of C and in colloquoal conversation. Not really, since sizeof operates on objects, rather than expressions, although the object may be defined by an expression. For example, sizeof (int)c returns the size of the (anonymous) object which is the result of evaluating the expression (int)c . If sizeof c operated on the result of the expression c rather than the object named by c, then sizeof could not be used to find out the size of the object c itself. Some quoting mechanism would then be needed, and things might end up syntactically more complex than they currently are. >From my point of view -- call it naive if you will -- anything >that has a value is an "expression". Okay; "it's naive". :-) Although expressions have values, that doesn't mean that all values are the result of an expression. There are times when it must be possible to ask questions about objects themselves, rather than the values they normally evaluate to. It just so happens that sizeof makes this distinction through virtue of its definition. This is also why it must be a part of the language itself, rather than an external function. >Therefore, if |c| as used above >has a value, it's an expression. Therefore |c| must be promoted to >int. Therefore |sizeof c| is equivalent to |sizeof (int) c|. Hence >the surprise. The logic is sound, but the premise is faulty. >But they can only guarantee what things mean, not >whether the meanings they define will surprise programmers. Ah, now you're talking about the interaction of people and ideas, at which point the psychology of programming becomes the subject, and so we must leave comp.lang.c ... -- Frank Wales, Grep Limited, [frank@grep.co.uk<->uunet!grep!frank] Kirkfields Business Centre, Kirk Lane, LEEDS, UK, LS19 7LX. (+44) 532 500303