Sujet : Re: Suggested method for returning a string from a C program?
De : rjh (at) *nospam* cpax.org.uk (Richard Heathfield)
Groupes : comp.lang.cDate : 19. Mar 2025, 18:38:12
Autres entêtes
Organisation : Fix this later
Message-ID : <vreve4$19klp$2@dont-email.me>
References : 1 2 3 4 5
User-Agent : Mozilla Thunderbird
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 <nospam@dfs.com> 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.
++++++++++++++++++++++++++++++++++++++++++++++
5.2.4.2.1 Sizes of integer types <limits.h>
[...]
— 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.
-- Richard HeathfieldEmail: rjh at cpax dot org dot uk"Usenet is a strange place" - dmr 29 July 1999Sig line 4 vacant - apply within