Deutsch   English   Français   Italiano  
<vihvm2$2j9p0$1@dont-email.me>

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

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Janis Papanagnou <janis_papanagnou+ng@hotmail.com>
Newsgroups: comp.lang.c
Subject: Re: else ladders practice
Date: Sun, 1 Dec 2024 16:34:24 +0100
Organization: A noiseless patient Spider
Lines: 63
Message-ID: <vihvm2$2j9p0$1@dont-email.me>
References: <3deb64c5b0ee344acd9fbaea1002baf7302c1e8f@i2pn2.org>
 <slrnvi746p.fkp.dan@djph.net> <else-20241116103316@ram.dialup.fu-berlin.de>
 <vihlgt$29jun$1@paganini.bofh.team>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 01 Dec 2024 16:34:26 +0100 (CET)
Injection-Info: dont-email.me; posting-host="bc2f5dd95a43e5fa73e13528031ef472";
	logging-data="2729760"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19/2DtfrcGDbrh0w/um4ti2"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
 Thunderbird/45.8.0
Cancel-Lock: sha1:c4dyHh7nD/cAr5tfA9O8q8SHAvk=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <vihlgt$29jun$1@paganini.bofh.team>
Bytes: 3172

On 01.12.2024 13:41, Waldek Hebisch wrote:
> Stefan Ram <ram@zedat.fu-berlin.de> wrote:
>>
>>   My bad if the following instruction structure's already been hashed
>>   out in this thread, but I haven't been following the whole convo!
>>
>>   In my C 101 classes, after we've covered "if" and "else",
>>   I always throw this program up on the screen and hit the newbies
>>   with this curveball: "What's this bad boy going to spit out?".
>>
>>   Well, it's a blue moon when someone nails it. Most of them fall
>>   for my little gotcha hook, line, and sinker.
>>
>> #include <stdio.h>
>>
>> const char * english( int const n )
>> { const char * result;
>>   if( n == 0 )result = "zero";
>>   if( n == 1 )result = "one";
>>   if( n == 2 )result = "two";
>>   if( n == 3 )result = "three";
>>   else        result = "four";
>>   return result; }
>>
>> void print_english( int const n )
>> { printf( "%s\n", english( n )); }
>>
>> int main( void )
>> { print_english( 0 );
>>   print_english( 1 );
>>   print_english( 2 );
>>   print_english( 3 );
>>   print_english( 4 ); }
>>
> 
> That breaks two rules:
> - instructions conditioned by 'if' should have braces,

I suppose you don't mean

  if (n == value) { result = string; }
  else            { result = other; }

which I'd think doesn't change anything. - So what is it?

Actually, you should just add explicit 'else' to fix the problem.
(Here there's no need to fiddle with spurious braces, I'd say.)

> - when we have the result we should return it immediately.

This would suffice to fix it, wouldn't it?

> 
> Once those are fixed code works as expected...

I find this answer - not wrong, but - problematic for two reasons.
There's no accepted "general rules" that could get "broken"; it's
just rules that serve in given languages and application contexts.
And they may conflict with other "rules" that have been set up to
streamline code, make it safer, or whatever.

Janis