Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: Using "extra" blocks to declare local variables (Was: switch/extension for see below strongly needed)
Date: Fri, 22 May 2026 23:44:02 -0700
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <86cxymhbgt.fsf@linuxsc.com>
References: <10uapjs$19723$1@dont-email.me> <10uej0o$2besu$1@dont-email.me> <10uffq4$m4se$1@paganini.bofh.team> <10ufgg4$2l9ge$1@dont-email.me> <10ugtih$298li$1@news.xmission.com> <10ugvb2$324u3$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Sat, 23 May 2026 06:44:05 +0000 (UTC)
Injection-Info: dont-email.me; logging-data="2192210"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+S1Q0ewt7N1dRAaqiVKLZLLo73v59hhro="; posting-host="bfea1ad8920711d5190b1ffe56c7d4f2"
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:eGDgCAUE4sY26cCKjbWFkxV3hQU= sha1:ozU+I/L0Wa1ztPL3HcD4KTola9g= sha256:ImshS2bT5/wzQw0RnT33kYIkwWYXhRHoezWNOAsQe4M= sha1:5eEtTbPMWIsv4DGrdqNCg/Ntx8M=
Xref: csiph.com comp.lang.c:399343
scott@slp53.sl.home (Scott Lurndal) writes:
> I often declare local variables within a compound statement
> introduced by a do/while/for/case construct.
I do this too, although I don't know if I would say often.
Probably closer to 50/50. Also the same applies to if().
I commonly use the declaration syntax in for() statements to have
an iteration variable (or variables) local to the loop.
> In pre or post C99 code, I do not create compound statements
> willy-nilly just to hide a prior defined local variable with the
> same name (a maintenance nightmare, to be sure) or to locate the
> declaration closer to the use in a function.
I agree with both of these. Very rarely I might introduce a
compound statement "out of the blue" not for either those reasons
but expressly to limit the scope of a variable in the midst of a
long and complicated function. Especially because such cases are
rare, I like to flag them with double braces, something like
{{ ULL carry = 0;
for(...){
... uses of carry
... uses of carry
}
if(...) uses of carry
if(...)
}}
(and yes, in case anyone is wondering, there are reasons why the
code in this case couldn't be abstracted into a sub-function).