Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Suggested method for returning a string from a C program? Date: Thu, 20 Mar 2025 05:09:21 -0700 Organization: A noiseless patient Spider Lines: 83 Message-ID: <86r02roqdq.fsf@linuxsc.com> References: <868qp1ra5f.fsf@linuxsc.com> <20250319115550.0000676f@yahoo.com> <20250319201903.00005452@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Thu, 20 Mar 2025 13:09:22 +0100 (CET) Injection-Info: dont-email.me; posting-host="7c01e97b5d1836204810413d59dbf156"; logging-data="3257331"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+wsnSKDEtXWw6sRRvtPD6oRoDtNyN1PjI=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:oGZLkv25GOECxoTQ+t4hImH1Vq0= sha1:IqfuRj+e+MtkouOJl8rr6oBHDmw= Xref: csiph.com comp.lang.c:391394 Michael S writes: > On Wed, 19 Mar 2025 17:38:12 +0000 > Richard Heathfield wrote: > >> On 19/03/2025 17:23, DFS wrote: >> >>> On 3/19/2025 5:55 AM, Michael S wrote: >>> >>>> On Wed, 19 Mar 2025 00:38:44 -0400 >>>> DFS wrote: >>>> >>>>> On 3/18/2025 11:07 PM, Tim Rentsch wrote: >>>>> >>>>> >>>>>> Have you thought about how large the value of 'n' can >>>>>> become inside the while() loop? >>>>> >>>>> I was too smug in my first reply. After Keith pointed out I >>>>> needed >>>>> to read from stdin, I submitted the code again and it passed some >>>>> tests but failed with 'OUTPUT LIMIT EXCEEDED' when n = 159487. >>>>> >>>>> Updating int to long worked, and now I'm bona fide! >>>>> >>>>> So thanks. >>>> >>>> What you did happens to be sufficient for a particular environment >>>> (supposedly, x86-64 Linux) used both by yourself and by the >>>> server that >>>> tests results. >>>> In more general case, 'long' is not guaranteed to handle >>>> numbers in >>>> range up to 18,997,161,173 that can happen in this test. >>> >>> How did you determine that? >> >> By the language definition. > > Well, not exactly. > I never read C Standard docs except the very first one that I read > more that I read more than 33 years ago, so not very likely to > remember it literally. > Let's say that I know this particular bit of trivia from 1st hand > experience and from reading few ABI definitions. > >> ++++++++++++++++++++++++++++++++++++++++++++++ >> >> 5.2.4.2.1 Sizes of integer types >> >> [...] >> >> ? minimum value for an object of type long int >> LONG_MIN >> -2147483647 // ?(231 ? 1) >> >> ? maximum value for an object of type long int >> LONG_MAX >> +2147483647 // 231 ? 1 >> >> ++++++++++++++++++++++++++++++++++++++++++++++ >> >> That is, the long int type is required to have a sign bit and at >> least 31 value bits, giving a guaranteed minimum range of >> -2147483647 to 2147483647. That's 2 thou mill. >> >> You can squeeze another bit out of it by going unsigned: 0 to >> 4294967295. That's 4 thou mill. >> >> From C99 onwards you can use long long int to give you 63 (or 64 >> for unsigned) value bits - printf with %lld or %llu. Roughly 9 >> mill mill mill and 18 mill mill mill respectively. > > I suspected that, but was not sure, so suggested to DFS a type that I am > sure about. The width of char and [un]signed char must be at least 8 bits. The width of [un]signed short must be at least 16 bits. The width of [un]signed int must be at least 16 bits. The width of [un]signed long must be at least 32 bits. The width of [un]signed long long must be at least 64 bits. That should be easy enough to remember now.