Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: What's new in C? |
| Date | 2012-12-08 11:32 -0800 |
| Organization | None to speak of |
| Message-ID | <lnlid8nxuc.fsf@nuthaus.mib.org> (permalink) |
| References | (4 earlier) <cb68edf5-5b93-4c00-b72c-06c6ecf00840@r20g2000yql.googlegroups.com> <slrnkc5sjo.1jc.grahn+nntp@frailea.sa.invalid> <nhHws.1049820$Ak.818313@fx24.am4> <slrnkc6mr9.1jc.grahn+nntp@frailea.sa.invalid> <64Mws.748715$Bz2.563356@fx11.am4> |
"BartC" <bc@freeuk.com> writes:
> "Jorgen Grahn" <grahn+nntp@snipabacken.se> wrote in message
> news:slrnkc6mr9.1jc.grahn+nntp@frailea.sa.invalid...
>> On Sat, 2012-12-08, BartC wrote:
>
>>> "Jorgen Grahn" <grahn+nntp@snipabacken.se> wrote in message
>
>>>> Ugh. Yeah, it would be interesting to read Linus' rationale for not
>>>> allowing it.
>>>
>>> Maybe he thought that overall that were more minuses than pluses.
>>>
>>> (I can see so many things wrong with having mixed declarations, that I
>>> wouldn't know where to start. Let's say I can't see many pluses at all!)
>>
>> We are at different ends of the spectrum then. I can think of one
>> semi-reason for banning them ("I want to pretend I have a compre-
>> hensive list of what's on this function's stack, and an idea of how
>> much stack it uses") but that's about it.
>
>> If you really have more arguments, I'm interested and willing to
>> listen.
>
> I've this discussed this here before I think. My arguments weren't received
> well then either!
>
> So, mixed declarations, AIUI, allow you to have unlimited, distinct versions
> of the same identifier within a function. They may or might not have the
> same type.
No, they don't.
> For example, a 3-deep nested for-loop all using 'i' as the index variable!
You've been able to have declarations with the same name *in nested
blocks* at least since C89, and I think K&R C allowed it too:
void func(void) {
int n;
{
double n;
}
}
What C99 added (borrowed from C++) was the ability to mix declarations
and statements in the same block, (whether that block happens to be the
outermost block of a function or not) -- but you still can't have
declarations with duplicate names in the same scope:
void func(void) {
int a;
statement();
int b; /* Illegal in C89/C90, legal in C99 */
double a; /* Illegal in all versions of C */
}
[...]
>> The argument *for* allowing it is simply:
>>
>> I can declare the variable exactly at the point where it's needed,
>> meaning I can give it a guaranteed sane initial value, and frequently
>> make it const.
>
> Yes, it makes it easy to write. But harder to read and to maintain. Maybe I
> should just have said that...
I agree it's easier to write; I don't agree that it makes it harder to
read or maintain:
void func(void) {
int x = something;
/* code that does some calculations affecting x */
const int y = value_that_depends_on(x);
/* code that uses y */
}
As Jorgen says, it means you can initialize y based on information
that's not available at the top of the function.
This is just an easier way to do what you could already do without this
feature:
void func(void) {
int x = something;
/* code that does some calculations affecting x */
{
const int y = value_that_depends_on(x);
/* code that uses y */
}
}
I *think* that any code that depends on mixed declarations and
statements can be transformed to code without mixed declarations and
statements, by adding opening and closing braces. (I could easily
be mistaken on that point; does anyone have a counterexample?)
The problem is that it can result in deeply nested blocks.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
What's new in C? Cal Dershowitz <cal@example.invalid> - 2012-11-28 19:41 -0700
Re: What's new in C? Keith Thompson <kst-u@mib.org> - 2012-11-28 19:15 -0800
Re: What's new in C? Cal Dershowitz <cal@example.invalid> - 2012-12-06 02:43 -0700
Re: What's new in C? James Kuyper <jameskuyper@verizon.net> - 2012-12-06 07:45 -0500
Re: What's new in C? Noob <root@127.0.0.1> - 2012-11-29 10:36 +0100
Re: What's new in C? Weland <weland@sdf.lonestar.org> - 2012-11-29 10:17 +0000
Re: What's new in C? Cal Dershowitz <cal@example.invalid> - 2012-12-04 01:45 -0700
Re: What's new in C? Weland <weland@sdf.lonestar.org> - 2012-11-29 09:48 +0000
Re: What's new in C? David Brown <david@westcontrol.removethisbit.com> - 2012-11-29 13:50 +0100
Re: What's new in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-11-29 05:55 -0800
Re: What's new in C? Les Cargill <lcargill99@comcast.com> - 2012-11-29 12:48 -0600
Re: What's new in C? Rui Maciel <rui.maciel@gmail.com> - 2012-11-30 10:46 +0000
Re: What's new in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-11-30 05:31 -0800
Re: What's new in C? "BartC" <bc@freeuk.com> - 2012-11-30 14:22 +0000
Re: What's new in C? Rui Maciel <rui.maciel@gmail.com> - 2012-11-30 15:04 +0000
Re: What's new in C? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-11-30 16:41 +0000
Re: What's new in C? James Kuyper <jameskuyper@verizon.net> - 2012-11-30 11:49 -0500
Re: What's new in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-11-30 10:12 -0800
Re: What's new in C? Keith Thompson <kst-u@mib.org> - 2012-11-30 11:12 -0800
Re: What's new in C? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-11-30 22:26 +0000
Re: What's new in C? "BartC" <bc@freeuk.com> - 2012-11-30 22:36 +0000
Re: What's new in C? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-11-30 23:27 +0000
Re: What's new in C? Cal Dershowitz <cal@example.invalid> - 2012-12-04 01:39 -0700
Re: What's new in C? Keith Thompson <kst-u@mib.org> - 2012-12-04 00:43 -0800
Re: What's new in C? Cal Dershowitz <cal@example.invalid> - 2012-12-04 03:14 -0700
Re: What's new in C? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-04 12:50 +0000
Re: What's new in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-12-04 05:54 -0800
Re: What's new in C? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-04 20:51 +0000
Re: What's new in C? Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-12-04 16:09 -0500
Re: What's new in C? James Kuyper <jameskuyper@verizon.net> - 2012-12-04 16:19 -0500
Re: What's new in C? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-04 21:45 +0000
Re: What's new in C? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-05 01:13 +0000
Re: What's new in C? James Kuyper <jameskuyper@verizon.net> - 2012-12-04 20:36 -0500
Re: What's new in C? Robert Wessel <robertwessel2@yahoo.com> - 2012-12-04 20:26 -0600
Re: What's new in C? Keith Thompson <kst-u@mib.org> - 2012-12-04 18:48 -0800
Re: What's new in C? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-05 02:57 +0000
Re: What's new in C? "BartC" <bc@freeuk.com> - 2012-12-05 01:42 +0000
Re: What's new in C? Cal Dershowitz <cal@example.invalid> - 2012-12-06 02:06 -0700
Re: What's new in C? James Kuyper <jameskuyper@verizon.net> - 2012-12-06 07:50 -0500
Re: What's new in C? JohnF <john@please.see.sig.for.email.com> - 2012-12-06 13:46 +0000
Re: What's new in C? James Kuyper <jameskuyper@verizon.net> - 2012-12-06 09:39 -0500
Re: What's new in C? Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-11-30 15:52 -0500
Re: What's new in C? Cal Dershowitz <cal@example.invalid> - 2012-12-04 01:53 -0700
Re: What's new in C? Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-12-04 08:16 -0500
Re: What's new in C? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-04 21:18 +0000
Re: What's new in C? Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-12-04 18:04 -0500
Re: What's new in C? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-05 01:19 +0000
Re: What's new in C? Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-12-04 21:05 -0500
Re: What's new in C? Noob <root@127.0.0.1> - 2012-12-05 16:45 +0100
Re: What's new in C? Cal Dershowitz <cal@example.invalid> - 2012-12-06 01:45 -0700
Re: What's new in C? James Kuyper <jameskuyper@verizon.net> - 2012-12-06 08:12 -0500
Re: What's new in C? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-06 21:13 +0000
Re: What's new in C? Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-12-06 09:15 -0500
Re: What's new in C? Cal Dershowitz <cal@example.invalid> - 2012-12-12 18:28 -0700
Re: What's new in C? Keith Thompson <kst-u@mib.org> - 2012-11-30 11:22 -0800
Re: What's new in C? Cal Dershowitz <cal@example.invalid> - 2012-12-04 03:04 -0700
Re: What's new in C? Rui Maciel <rui.maciel@gmail.com> - 2012-11-29 18:19 +0000
Re: What's new in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-11-29 11:41 -0800
Re: What's new in C? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-11-30 20:50 +0000
Re: What's new in C? Jens Gustedt <jens.gustedt@loria.fr> - 2012-11-30 23:35 +0100
Re: What's new in C? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-12-05 03:26 +0000
Re: What's new in C? gwowen <gwowen@gmail.com> - 2012-12-05 23:49 -0800
Re: What's new in C? Cal Dershowitz <cal@example.invalid> - 2012-12-06 01:49 -0700
Re: What's new in C? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-12-08 07:55 +0000
Re: What's new in C? "BartC" <bc@freeuk.com> - 2012-12-08 13:29 +0000
Re: What's new in C? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-12-08 15:23 +0000
Re: What's new in C? Martin Shobe <martin.shobe@yahoo.com> - 2012-12-08 17:46 +0000
Re: What's new in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-12-08 10:45 -0800
Re: What's new in C? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-12-08 19:32 +0000
Re: What's new in C? "BartC" <bc@freeuk.com> - 2012-12-08 18:55 +0000
Re: What's new in C? Ian Collins <ian-news@hotmail.com> - 2012-12-09 08:26 +1300
Re: What's new in C? "BartC" <bc@freeuk.com> - 2012-12-08 20:12 +0000
Re: What's new in C? Ian Collins <ian-news@hotmail.com> - 2012-12-09 09:18 +1300
Re: What's new in C? "BartC" <bc@freeuk.com> - 2012-12-08 21:21 +0000
Re: What's new in C? Ian Collins <ian-news@hotmail.com> - 2012-12-09 10:27 +1300
Re: What's new in C? Keith Thompson <kst-u@mib.org> - 2012-12-08 13:36 -0800
Re: What's new in C? Keith Thompson <kst-u@mib.org> - 2012-12-08 12:59 -0800
Re: What's new in C? "BartC" <bc@freeuk.com> - 2012-12-08 22:10 +0000
Re: What's new in C? Keith Thompson <kst-u@mib.org> - 2012-12-08 16:15 -0800
Re: What's new in C? Ian Collins <ian-news@hotmail.com> - 2012-12-09 14:23 +1300
Re: What's new in C? Ian Collins <ian-news@hotmail.com> - 2012-12-09 14:20 +1300
Re: What's new in C? Keith Thompson <kst-u@mib.org> - 2012-12-08 11:32 -0800
Re: What's new in C? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-09 02:52 +0000
Re: What's new in C? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-09 02:53 +0000
Re: What's new in C? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-12-08 20:08 +0000
Re: What's new in C? "BartC" <bc@freeuk.com> - 2012-12-08 20:25 +0000
csiph-web