Deutsch   English   Français   Italiano  
<vrfcgn$1l75r$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder9.news.weretis.net!news.quux.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Richard Heathfield <rjh@cpax.org.uk>
Newsgroups: comp.lang.c
Subject: Re: Suggested method for returning a string from a C program?
Date: Wed, 19 Mar 2025 21:21:27 +0000
Organization: Fix this later
Lines: 40
Message-ID: <vrfcgn$1l75r$1@dont-email.me>
References: <vrd77d$3nvtf$2@dont-email.me>
 <slrnvtkrqc.ii1.ike@faeroes.freeshell.org>
 <875xk5v1td.fsf@nosuchdomain.example.com> <vrfadv$1jska$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 19 Mar 2025 22:21:29 +0100 (CET)
Injection-Info: dont-email.me; posting-host="b7503893adb236434c108755aac52888";
	logging-data="1744059"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/nYU1MOb0lxauL2HROdpxYkE4SLDOEGS2h5CeQC1ukew=="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:y+Xy3jIykC1CPrYlm8noBxoTlQQ=
Content-Language: en-GB
In-Reply-To: <vrfadv$1jska$2@dont-email.me>
Bytes: 2355

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 Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within