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, 22:21:27
Autres entêtes
Organisation : Fix this later
Message-ID : <vrfcgn$1l75r$1@dont-email.me>
References : 1 2 3 4
User-Agent : Mozilla Thunderbird
On 19/03/2025 20:45, DFS wrote:
On 3/19/2025 4:53 AM, Keith Thompson wrote:
<snip>
I'm actually using
scanf() for integer input, something I wouldn't do in normal code
(because the behavior is undefined if the scanned value is out
of range). This is a rare case where it's safe to assume that
stdin has nothing harmful.
My very 1st submission for 'Missing Number' was accepted, but each use of scanf() generated a warning:
Like Keith, I don't usually use scanf.
My preference is to read the number into a text buffer using fgets, and then convert it using strtoul if I can, or strtol if I have to allow for negatives.
//identify the missing number in a set of consecutive integers
Not sure why you're sorting and malloc-ing.
Loop through the input, adding as you go.
Let the first number be x.
Let the last number be y.
Let the total of all numbers be t.
Let m = ((y*(y+1))-(x*(x+1)))/2 - t;
"The missing number is: %lld\n", m
-- Richard HeathfieldEmail: rjh at cpax dot org dot uk"Usenet is a strange place" - dmr 29 July 1999Sig line 4 vacant - apply within