Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: nested while loop computing triangular number - issues Date: Sun, 12 Jul 2015 11:48:24 -0700 Organization: None to speak of Lines: 104 Message-ID: References: <8e9f29f8-a7d0-42c4-a146-2a0d0b937051@googlegroups.com> <87si8uu5tr.fsf@bsb.me.uk> <5abf1a27-ebbf-4c5d-89d9-e0b8fc6efd04@googlegroups.com> <7e623eb8-4156-443c-b637-ffe06d4e87de@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="23232"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19jlbefPIza+Cgz8UzqXA1t" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:IAQlLQLyiTzjn5OxdVaqkVWhNpY= sha1:cXAZq1st0XDsLAVyfeVRhFB89mc= Xref: csiph.com comp.lang.c:65440 David Kleinecke writes: > On Saturday, July 11, 2015 at 2:56:52 PM UTC-7, Keith Thompson wrote: >> David Kleinecke writes: >> [...] >> > I have recently taken to writing this way - I repeat in my style: >> > >> > int main (void) { >> > int n; >> > int number; >> > int triangular_number; >> > int counter; >> > # >> > n = 1; >> > counter = 1; >> > triangular_number = 0; >> >> The use of "#" (a null preprocessor directive) is odd. Perfectly legal, >> but odd. I might use an ordinary comment like: >> >> /******************/ > > I want something that looks to the eye like a blank line but isn't. > A line like you show is too strong a divider. I want to write each > function without any genuine blank lines (for other reasons) but I > want to make a break equivalnt to a blank line. A single # resolves > this conflict nicely. Ok. It's not what I'd do. I might use "/**/", or "//" if I didn't insist on C90. I personally find your use of "#" a little jarring; null directives are rarely used in practice, and I had to check the standard to verify that a null directive does nothing. Actually I'd just use a blank line. You didn't say why you don't want to do that; my best guess is that you want to use your editor's paragraph movement commands. You might investigate whether your editor has commands to jump past C functions (vi/vim has "[[" and "]]"). But as I said, it's perfectly legal. > ... > >> > Given compiler control I could extend C to allow an assignment (not an >> > initialization >> > local_variables = {1, 0, 1, 0,}; >> > Maybe this ia already a known extension. >> >> It's not an extension; it's a standard language feature, starting with >> C99, called "compound literals". >> >> local_variables = (struct foo){1, 0, 1, 0}; >> >> (Note that "(struct foo)" is not a cast; it's part of the syntax of the >> compound literal.) >> >> Of course if you insist on sticking to C90 you don't get to use things >> like this. > > I'll stay with C90. > > That's an ugly syntax. Why is the non-cast necessary? What confict > is resolved? The type is necessary because without it the type of {1, 0, 1, 0} would be indeterminate -- unless the type is inferred from the context, but C expressions in general don't work that way. Enclosing the type in parentheses is, I presume, deliberately reminiscent of cast syntax, and the stuff between the braces is explicitly defined in the grammar as an initializer-list. I don't disagree with your statement that it's an ugly syntax, but it doesn't bother me much. What alternative syntax would you suggest? In any case, my main point stands; abandoning the advantage of in-place initialization because you think of a collection of local object declarations as a structure doesn't make much sense. If you want to make it an actual structure, you can initialize the structure. (David, the following is unrelated to C90; you might want to stop reading now.) Incidentally, one thing I dislike about compound literals is that the storage duration of the object depends only on the context in which it appears. A string literal always creates an array object with static storage duration. A compound literal creates an object with static storage duration if it occurs outside a function, or with automatic storage duration if it occurs inside a function. It doesn't matter for the case we're discussing, but there are times it would be convenient to have a block-scope compound literal that refers to a static object. I might suggest an enhancement that permits something like: (static struct foo){1, 0, 1, 0}; at block scope. (Of course non-constant expressions would not be permitted.) -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"