Path: utzoo!utgpu!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!bloom-beacon!bu-cs!encore!bzs From: bzs@Encore.COM (Barry Shein) Newsgroups: comp.windows.x Subject: Re: stupid C question Message-ID: <4543@xenna.Encore.COM> Date: 31 Dec 88 16:37:12 GMT References: <8812300028.AA17754@EXPO.LCS.MIT.EDU> Organization: Encore Computer Corp, Marlboro, MA Lines: 26 In-reply-to: rws@EXPO.LCS.MIT.EDU's message of 30 Dec 88 00:28:57 GMT Posting-Front-End: GNU Emacs 18.41.15 of Tue Jun 9 1987 on xenna (berkeley-unix) From: rws@EXPO.LCS.MIT.EDU (Bob Scheifler) >#define foo "abcde" >Is sizeof(foo) defined to be 5, 6, or >=5? My Sun compiler gives 6, >I have a bug report claiming it should be 5, and the C manuals I have >are no help. from "A C Reference Manual", Harbison and Steele, pg. 22 "The type of a string constant is ``array of char,'' and its value is the n+1 characters. For example, the value of sizeof("abcdef") is 7..." K&R (either edition) doesn't seem to say anything explicit but the fact that "xyz" is of type "array of char" and that a null is added by the compiler is quite clear, I can't imagine any argument which says that null is then somehow ignored by sizeof() (how would it know?) I'm pretty sure the confusion, if any, is some belief that strlen("abc") should return the same result as sizeof("abc") which is false. What's worse are compilers (known to exist!) which return sizeof(char *) for sizeof(string constant), that is unacceptable but worth a moment's thought when relying on these semantics or trying to track down a problem. -Barry Shein, ||Encore||