Deutsch English Français Italiano |
<vihlgt$29jun$1@paganini.bofh.team> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!feeds.phibee-telecom.net!2.eu.feeder.erje.net!feeder.erje.net!newsfeed.bofh.team!paganini.bofh.team!not-for-mail From: antispam@fricas.org (Waldek Hebisch) Newsgroups: comp.lang.c Subject: Re: else ladders practice Date: Sun, 1 Dec 2024 12:41:03 -0000 (UTC) Organization: To protect and to server Message-ID: <vihlgt$29jun$1@paganini.bofh.team> References: <3deb64c5b0ee344acd9fbaea1002baf7302c1e8f@i2pn2.org> <slrnvi746p.fkp.dan@djph.net> <else-20241116103316@ram.dialup.fu-berlin.de> Injection-Date: Sun, 1 Dec 2024 12:41:03 -0000 (UTC) Injection-Info: paganini.bofh.team; logging-data="2412503"; posting-host="WwiNTD3IIceGeoS5hCc4+A.user.paganini.bofh.team"; mail-complaints-to="usenet@bofh.team"; posting-account="9dIQLXBM7WM9KzA+yjdR4A"; User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-9-amd64 (x86_64)) X-Notice: Filtered by postfilter v. 0.9.3 Bytes: 2214 Lines: 41 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, - when we have the result we should return it immediately. Once those are fixed code works as expected... -- Waldek Hebisch