News for hackers. Stuff that matters.
Re:A nice, little known C feature: Static array indices in p... by tptacek on Monday February 18, 2013 @12:41PM (#5239444)
I am convinced there is some kind of long-term drinking game going on in the C standards committee to see how many different uses they can come up with for the word "static".
Re:A nice, little known C feature: Static array indices in p... by matthavener on Monday February 18, 2013 @01:32PM (#5239720)
They don't really have a choice, do they? If you want to add features you can either: 1) make a new reserve word, possibly breaking existing code 2) reuse a reserve word in a new context.
Re:A nice, little known C feature: Static array indices in p... by evincarofautumn on Monday February 18, 2013 @03:43PM (#5240513)
Or you can add context-sensitive keywords, which are keywords only where it’s syntactically valid and identifiers elsewhere. Those two contexts are almost always mutually exclusive, and it’s obvious which one you’re in. ActionScript 3, though absolutely not a good example of language design in general, has a good example of this. You can say:

    public function get foo():int { return 5; }
But you can also say:

    var get:int = foo;
Re:A nice, little known C feature: Static array indices in p... by bunderbunder on Monday February 18, 2013 @05:32PM (#5241205)
C# is up to about 25 of them, and it works well as long as you can resist the temptation to do silly things like

    var var = 1;
    var select = var.ToString();
    var from = from c in @select select c.GetHashCode();
(The @ thing, which tells the compiler, "This is an identifier and not a keyword", can help quite a bit in a pinch.)
Re:A nice, little known C feature: Static array indices in p... by ggchappell on Monday February 18, 2013 @04:32PM (#5240819)
C++11 also has them: override and final.

So:

  class Foo
  {
      virtual void bar() final;
  };
But:

  int final = 3;
So, for example, this is legal:

  class Foo
  {
      virtual void final() final;
  };
Re:A nice, little known C feature: Static array indices in p... by rwmj on Monday February 18, 2013 @03:56PM (#5240583)
Larry Wall (author of Perl) used this as an argument for "sigils", that is, putting $/@/% in front of every variable.

http://stackoverflow.com/questions/1091634/why-do-perl-varia...

(I merely state this as fact and offer no judgement!)

Re:A nice, little known C feature: Static array indices in p... by drivers99 on Monday February 18, 2013 @05:53PM (#5241334)
Deleted my previous comment because it was wrong (I was saying that it didn't help with the names of subroutines). It appears you can even have subroutines named after keywords, but only as long as you put "&" in front of it when you call it:

    sub while
    {
        print "It works!\n";
    }

    &while();
I guess it is still the case that you can break perl code by adding keywords, if it collides with a subroutine that is called with the "&" symbol. Seems most code doesn't use "&" though, but it's a relatively quick fix.
Re:A nice, little known C feature: Static array indices in p... by leviathan on Monday February 18, 2013 @06:01PM (#5241384)
Calling a sub with & has a special meaning, it exposes the current argument list @_ to the called sub. That's why you don't see it in most code.
Re:A nice, little known C feature: Static array indices in p... by drivers99 on Tuesday February 19, 2013 @11:59AM (#5245388)
I did not know that. Interesting. However, it only works if you call the sub without parentheses like this: &foo

Calling it with parentheses like &foo() would make @_ empty inside of foo. (Or if you said &foo("whatever") it would pass that as @_ instead.)

    sub while
    {
            print "@_\n";
            &foo();
    }

    sub foo
    {
            print "@_\n";
    }

    &while("a","b","c");
produces:

    a b c
    (blank line)
as the output, and

    sub while
    {
            print "@_\n";
            &foo;
    }

    sub foo
    {
            print "@_\n";
    }

    &while("a","b","c");
produces:

    a b c
    a b c 
as the output. At any rate, back to the original topic: it still doesn't prevent new keywords from potentially colliding. Oh well.
Re:A nice, little known C feature: Static array indices in p... by tolmasky on Monday February 18, 2013 @09:13PM (#5242195)
I prefer the objective-c method of putting @ in front of every keyword.
Re:A nice, little known C feature: Static array indices in p... by dietrichepp on Tuesday February 19, 2013 @02:37AM (#5243140)
Well, I might be upset if this were true for all keywords.

    @static @inline @unsigned @int ilog2(@unsigned @int x)
    {
        @for (@unsigned @int i = 0; i < 32; i++)
            @if ((x >> i) == 0)
                @return i;
        @return 32;
    }
I'm a bit dizzy looking at that code, let me sit down for a moment.
Re:A nice, little known C feature: Static array indices in p... by redial on Tuesday February 19, 2013 @03:41AM (#5243319)
I believe the @ is used only by the obj-c keywords and not the ones from C. So it'll be something more like this

    @autoreleasepool{
        for (int index = 0; index < 100; index++){
            int error = [MyObject performSelector:@selector(addCoolFilterToImage:)];
            if (error) {
                return 32;
            }
        }
    }
Re:A nice, little known C feature: Static array indices in p... by dietrichepp on Tuesday February 19, 2013 @05:52AM (#5243644)
Yeah, that was kind of my point.
Re:A nice, little known C feature: Static array indices in p... by bunderbunder on Monday February 18, 2013 @01:37PM (#5239750)
One better choice that comes to mind:

  void bar(int myArray[10+]);
Re:A nice, little known C feature: Static array indices in p... by vexal on Monday February 18, 2013 @01:43PM (#5239799)
That's not better. It's more confusing to see a symbol that's normally an operator than it is to see a keyword.
Re:A nice, little known C feature: Static array indices in p... by abcd_f on Monday February 18, 2013 @02:59PM (#5240262)
It's only confusing if you are a language parser. I doubt that anyone else would have any problem guessing what this means.
Re:A nice, little known C feature: Static array indices in p... by astine on Monday February 18, 2013 @03:37PM (#5240477)
It looks like a syntax error to me. You can add numbers in array index brackets so a plus sign already has meaning there. Considering that a[10+] is just one character off from a[10++], it seems to me that this is just a bug waiting to happen.
Re:A nice, little known C feature: Static array indices in p... by bunderbunder on Monday February 18, 2013 @04:16PM (#5240699)
It would be a syntax error. . . unless you add syntax for it.

As far as it being a bug waiting to happen, C already has plenty of traps like that - '=' vs '==', '&' vs '&&', and so on.

In terms of grammatical clarity, I shouldn't think it would be any more confusing than any other operators that have both unary and binary versions. By my count C already has three of those: '-', '&', '*'. The latter two are even examples where the unary meaning and the binary meaning are wholly unrelated.

Besides, it's a way of using + that is already well-established in everyday idiom. And there are semantically-related uses for + that are already well-established in computer languages, too - the Kleene +, for example, should be familiar to everybody.

In terms of how it would work for the grammar I realize this would be somewhat of a departure since it's repurposing a symbol that's normally an operator to be used in a manner that would be more like a keyword according to C syntax. . . but I think that's a detail that should be much more interesting to standards committees than it is to people who primarily just use the language. And in terms of the human factor I submit that it's much more workable than taking a word from the English language and repurposing it to mean something that's more-or-less the opposite of what it means in English.

Re:A nice, little known C feature: Static array indices in p... by robinh on Monday February 18, 2013 @08:20PM (#5242001)
"As far as it being a bug waiting to happen, C already has plenty of traps like that - '=' vs '==', '&' vs '&&', and so on."

And C programmers are already spending plenty of their time cursing the world because of those things. Let's not add more of that, shall we? ;)

Re:A nice, little known C feature: Static array indices in p... by spoiler on Monday February 18, 2013 @09:33PM (#5242257)
I never ever had problems with it in C. I actually had a problem with it in another language (might have been a BASIC) where = was contextual, so one could do "a = 10" and then somewhere down the line "if x = 5 then". I used == and couldn't figure out what was wrong for nearly 5 minutes (I think it was recognising (or not) as an unexpected token).

= and == should have different meanings.

I also like how JavaScript has ===, although it is a little superfluous.

Re:A nice, little known C feature: Static array indices in p... by einhverfr on Tuesday February 19, 2013 @01:49AM (#5243028)
Interestingly PostgreSQL's pl/pgsql does that.

There are two operators, equality = and assignment :=

But where it is clear that assignment was meant, = is treated as assignment. this means you can:

    a = 5; -- exactly equivalent to a := 5 here
    if a = 6 then
        raise exception 'this should never happen';
    end if;
Interestingly this is entirely a Postgres-ism. Oracle's PL/SQL offers no such "feature."
Re:A nice, little known C feature: Static array indices in p... by ckarmann on Monday February 18, 2013 @01:51PM (#5239855)
The problem with the plus sign is that people have a natural assumption about what it means (that is, the addition). Changing that make things more confusing. At least, C programmer are used now to the fact that the static keyword can have strange and different meanings and another one is no big deal.
Re:A nice, little known C feature: Static array indices in p... by PySlice on Monday February 18, 2013 @08:56PM (#5242125)
That looks nice, but I think it could be simply void bar(int myArray[10]), since C effectively ignores the number inside the first pair of brackets in a parameter list, they could have added some meaning to it. No need for another meaning for "static"
Re:A nice, little known C feature: Static array indices in p... by mikeash on Monday February 18, 2013 @02:45PM (#5240191)
C reserves part of the namespace for the language or implementation, so they have a third way, which they've used for e.g. boolean types in C99. Make a new reserved word in the reserved namespace (either starting with __ or just _ followed by a capital letter) and then add a standard header to #define or typedef the unfriendly reserved word to something nicer for code that wants it.
Re:A nice, little known C feature: Static array indices in p... by barrkel on Monday February 18, 2013 @05:10PM (#5241073)
Observe the differences between Managed C++ and C++/CLI to see how poorly this approach works in practice.
Re:A nice, little known C feature: Static array indices in p... by mikeash on Monday February 18, 2013 @07:56PM (#5241916)
Knowing nothing about either, can you elaborate?
Re:A nice, little known C feature: Static array indices in p... by gurkendoktor on Tuesday February 19, 2013 @04:05AM (#5243382)
Managed C++ was chock full of __keywords for .NET memory management - like Apple's ARC in the worst case, except there were no good cases. While it probably was a clean superset of C++, it was painfully hideous.

CLI/C++, by which Microsoft replaced Managed C++, added new operators and keywords without much regard for backwards compatibility (=without underscores).

The implication being that people value code readability over __backward __compatibility. I have no data on the popularity of Managed C++ vs CLI/C++ though. I was only a fanboy because CLI/C++ looks awesome and I generally enjoy Herb Sutter's work, but then got sucked into the Appleverse.

Re:A nice, little known C feature: Static array indices in p... by mikeash on Tuesday February 19, 2013 @11:15AM (#5245032)
Just adding __ keywords is not the approach I described, though. I described adding __keywords, and adding a standard header that redefines them into non-underscore keywords. This avoids breaking old code (it won't include the header) while allowing new code to use nicer keywords, if it wishes.
Re:A nice, little known C feature: Static array indices in p... by gurkendoktor on Thursday February 21, 2013 @04:35AM (#5256440)
Right - I was just trying to provide context :) If there is anything in the Managed C++ -> CLI/C++ transition that is better than the ISO C approach, it is maybe the contextual keywords in CLI/C++ - something that can't be achieved with #define.
Re:A nice, little known C feature: Static array indices in p... by bunderbunder on Monday February 18, 2013 @12:53PM (#5239513)
With bonus points for uses that have absolutely nothing to do with the word's common meaning.
Re:A nice, little known C feature: Static array indices in p... by unimpressive on Monday February 18, 2013 @05:11PM (#5241079)
"Nobody really knows what the Bourne shell's grammar is. Even examination of the source code is little help."

— Tom Duff

Not exactly the same situation, considering that compilers work.

Re:A nice, little known C feature: Static array indices in p... by chubot on Tuesday February 19, 2013 @12:52AM (#5242866)
(off topic) I'm sure that was true at some point, but the comment is out of date now. To my surprise, if you download the current bash source code from [1], it actually has a yacc grammar -- see parse.y. It's 6000 lines, but it would be less intelligible without yacc I'm sure.

Honestly, it might be Stockholm syndrome, but I don't find bash hard to parse at all. The only real problem I see is that people get in trouble when they try to nest quoting styles. But there's basically never any reason to do that. User-defined functions basically eliminate this (may not be POSIX compliant, but supported in all shells I know of),

I factor all my shell scripts into 2 to 10 line functions and they are super easy to maintain and are 5-10x shorter than any alternative.

[1] http://tiswww.case.edu/php/chet/bash/bashtop.html

Re:A nice, little known C feature: Static array indices in p... by nitrogen on Tuesday February 19, 2013 @09:09AM (#5244310)
User-defined functions basically eliminate this (may not be POSIX compliant, but supported in all shells I know of),

User-defined functions are part of POSIX, but the function keyword is not. This bit me when I was porting a script from bash to dash (worth the effort on slower devices, as dash is significantly faster loading).

On bash you could do something like this:

  function foo()
  {
      echo "Foo"
  }
In POSIX shell (and, thus, also bash) you do this:

  bar()
  {
      echo "Bar"
  }
Reference: https://wiki.ubuntu.com/DashAsBinSh#function
Re:A nice, little known C feature: Static array indices in p... by caf on Tuesday February 19, 2013 @07:01AM (#5243837)
bash is not the Bourne shell - it's the Bourne Again shell. sh is the Bourne shell.
Re:A nice, little known C feature: Static array indices in p... by gatherknwldg on Monday February 18, 2013 @11:41PM (#5242655)
That is a comment on the style of C that Bourne used in his shell implementation. He used the pre-processor to e.g. name { BEGIN, } END and do other Algol-isms.

Duff would apparently rather read C that was written in C.

Re:A nice, little known C feature: Static array indices in p... by cmccabe on Monday February 18, 2013 @09:32PM (#5242252)
It's not at all the same situation for C. C's grammar is very-well defined and not at all hard to parse.

C++, on the other hand, has a horribly complex grammar. It would be unfair to say that "nobody really knows what it is," given that it has been standardized. But I think you will find that many tweaks to your code are needed when going between different C++ compilers.

I think what a lot of commenters in this thread are missing is that while the concept of a highly context-sensitive grammar seems simple, the reality is not. And having such a grammar makes it virtually impossible to have good tools.

Before some pedant reminds me that C's grammar is also context-sensitive: yes, I know. But you can parse C with lexx and yacc anyway, whereas you have no hope of doing this in C++.

Re:A nice, little known C feature: Static array indices in p... by unimpressive on Monday February 18, 2013 @10:23PM (#5242408)
I meant more on the programmers side than the implementors side. [0] But that's probably a moot point anyway, as I understand it most C code is probably crap plagiarized from amateur tutorials.

[0]: Nobody would here be an obvious exaggeration, as evidenced by the above blog post.

Re:A nice, little known C feature: Static array indices in p... by cmccabe on Monday February 18, 2013 @11:50PM (#5242689)
I think you are getting mixed up. The grammar of C isn't complex. Unrelatedly, C does have a few lesser-known features, but remarkably few for a 40-year-old language.
Re:A nice, little known C feature: Static array indices in p... by unimpressive on Tuesday February 19, 2013 @01:15AM (#5242918)
I never got into C because of books like "Expert C Programming"[0], knowing they exist tells me that theres a ton of "gotchas", and life is too short for that if I'm not really crazy about it in the first place.

Then again, as far as actual grammars go, I've heard C++ is bad enough that the compilers are the standard, and that if you want to be "compliant" with real world C++ code you copy every feature [1] of GCC.

[0]: http://www.amazon.com/Expert-Programming-Peter-van-Linden/dp...

[1]: ftp://ftp.trailing-edge.com/pub/rsx11freewarev2/rsx81b/374001/jargon.txt

Re:A nice, little known C feature: Static array indices in p... by cmccabe on Tuesday February 19, 2013 @01:48AM (#5243025)
Almost every language has a book titled "Expert $LANGUAGE". If a language doesn't have one of those, it's probably either very new, or nobody actually uses it, or both.

C and C++ are good for doing low-level stuff. If you don't want to do low-level stuff, then you should not worry about them. But in that case, you might also want to avoid commenting about them :)

clang is copying gcc's features because most of them are good features, and standards bodies move slowly. This is kind of an odd thing to criticize C++ for, since a lot of newer languages don't even have a standard, but just a reference implementation. It's hard to criticize your neighbor for living in a tent when you live in a sleeping bag.

Re:A nice, little known C feature: Static array indices in p... by unimpressive on Tuesday February 19, 2013 @02:16AM (#5243091)
>Almost every language has a book titled "Expert $LANGUAGE". If a language doesn't have one of those, it's probably either very new, or nobody actually uses it, or both.

I said books. I got that vibe in general from the people I met who claimed to be C wizards. Incredibly offputting.

>C and C++ are good for doing low-level stuff. If you don't want to do low-level stuff, then you should not worry about them. But in that case, you might also want to avoid commenting about them :)

I was going to delete my original comment because reading it over it felt like a bad idea. (I don't like jumping into ignorance induced shitstorms.)[0][1]

>This is kind of an odd thing to criticize C++ for, since a lot of newer languages don't even have a standard, but just a reference implementation.

I should have put "features" in quotes. I was specifically using the definition in the old jargon file. [2] So what I really meant to say is that the compilers end up supporting each others bugs for compatibility reasons.

As for reference implementations, if the reference implementation is for all practical reasons the only implementation, then you don't need a standard.

[0]: The comments I got in response are interesting enough that I'm actually glad I didn't.

[1]: EDIT. My ignorance.

[2]: See footnote on the last post, I should have made that more clear or used different terminology.

Re:A nice, little known C feature: Static array indices in p... by cmccabe on Tuesday February 19, 2013 @03:33AM (#5243301)
So what I really meant to say is that the compilers end up supporting each others bugs for compatibility reasons.

Example?

Re:A nice, little known C feature: Static array indices in p... by unimpressive on Tuesday February 19, 2013 @09:56AM (#5244561)
There was an "I heard" in that original context. Back in the great grand parent post. I might be able to find some examples, but since I don't have one off hand I'd rather drop the conversation.

So that's what I'm going to do unless I suddenly get the urge to go running through clangs commit log.

Re:A nice, little known C feature: Static array indices in p... by Evbn on Monday February 18, 2013 @01:20PM (#5239645)
Hooray for forward compatibility. "static" means "Semantic Token Always Taken In Context."
Re:A nice, little known C feature: Static array indices in p... by crest on Monday February 18, 2013 @06:51PM (#5241644)
Today i learned what s.t.a.t.i.c. stands for.