21

recently I ran into a strange grammar of C Programming Language.

First, let's see the code:

main(void) {
int a[10:> ;
printf("asdf");
return 0;
}

And you can compile it with gcc:

/tmp  gcc sample.c
sample.c: In function ‘main’:
sample.c:3: warning: incompatible implicit declaration of built-in function ‘printf’

As you can see, there's no error or any warning related to it. So this means :> equals to ] in CPL?

How can that happen?

BTW: I'm using gcc 4.2.1.

Void Main
  • 2,241
  • 3
  • 27
  • 36
  • 4
    That is a digraph, take a look [here](http://en.wikipedia.org/wiki/Digraphs_and_trigraphs) – Zlatomir May 26 '13 at 13:10
  • I REALLY object to this being marked as a duplicate. The OP did not know about digraphs/trigraphs. The "duplicates" cited above bear little or no resemblence to what the OP reported. We have to presume people asking questions do not always know what we already know about a given topic. – jim mcnamara May 26 '13 at 16:22
  • 11
    @jimmcnamara It _is_ a duplicate. Okay, this used `:>` and the others `<:`, but that's not much of a difference. Being a duplicate doesn't mean it's a bad question (a score of +15 shows that overall it is considered good). It's just, well, a duplicate, the (basically) same has been asked and answered before. – Daniel Fischer May 26 '13 at 18:00
  • 2
    If your question is something like *"So this means `:>` equals to `]` in CPL?"*, then in the future please use a far more descriptive title? (Also, this question [is discussed on Meta](http://meta.stackexchange.com/questions/181864/why-are-questions-closed-as-duplicates-if-you-could-have-never-found-them-withou).) – Arjan May 26 '13 at 18:24
  • @DanielFischer, sorry for the duplicate thing, I just don't know how to express thinks like this. Poor english... – Void Main May 27 '13 at 01:55
  • 3
    @VoidMain There's **nothing** you need to be sorry for here. If you posted a duplicate of something that's trivial to google, you should, perhaps. Digraphs and trigraphs aren't so easy to google. Actually, if you don't already know the answer, it's near impossible to google them. By posting a duplicate question, you placed another page on Stack Overflow where in future people with the same problem can land, that's a service. It's not bad to have duplicates (only to have too many duplicates of the same). It is however better to have the answers concentrated in few places, so dupes get marked. – Daniel Fischer May 27 '13 at 07:40
  • @Arjan I've changed the title according to your advice, to make it even better, I've used some english words to make sure users can search it. (These stop words..) – Void Main Jun 01 '13 at 12:45
  • @DanielFischer I just don't know how you guys searched the term. – Void Main Jun 01 '13 at 12:46
  • @VoidMain If you know the answer, searching for "digraph" turns up a couple of useful results. That's a not too rare problem, however, those who know the right search terms are not those who need the search results :( – Daniel Fischer Jun 01 '13 at 22:38
  • @DanielFischer that's quite a cruel fact.. :( – Void Main Jun 02 '13 at 06:11
  • @VoidMain Well, yes. But it's not as bleak as one might think at first glance. When you don't know it and ask here, there are - for almost anything - people who know here. If you're not the first to ask about that particular thing, your question will probably marked as a duplicate, but if it's a good question, it still gets you some upvotes (hey, this one might even earn you the "Good Question" badge). But most importantly: you get the answer to the question. – Daniel Fischer Jun 02 '13 at 07:57
  • @DanielFischer That's the point for SO isn't it? Learn what you don't know yet! – Void Main Jun 03 '13 at 13:10
  • One of the points. Learn, help others learn (by asking good questions from which they can learn as well as by answering questions). Asking a good duplicate helps as much as the original question. – Daniel Fischer Jun 03 '13 at 14:01
  • @DanielFischer got it, sir! Big thanks for your advice, and this small talk we had. :) – Void Main Jun 04 '13 at 12:14
  • You're welcome. Don't start _intentionally_ asking duplicate questions, but don't feel bad if a question you ask turns out to be a duplicate. It's nothing bad and you shouldn't feel bad if one of your questions is marked as a duplicate [it's not even being called "closed as duplicate" anymore to set it apart from the closed questions one has a reason to feel bad about]. – Daniel Fischer Jun 04 '13 at 12:22
  • @DanielFischer These words should be added to the instructions of asking questions! – Void Main Jun 04 '13 at 12:43

1 Answers1

25

Yes, this works.

It's called a digraph and was invented because in the old days (mid-1990s) there were still people using serial terminals (kind of like a PC, but without local processing), and some of those used a 7-bit character set. It was derived from ASCII, however where ASCII had [ and ], the derived character set would instead have other glyphs (e.g, Å and Ä).

Hannele
  • 9,301
  • 6
  • 48
  • 68
  • 6
    I'm pretty sure digraphs were invented before 1989. – aschepler May 26 '13 at 13:15
  • 1
    digraphs were around well before the 1990's. Back when a tty looked more like a desk. And ran at 50 baud. – jim mcnamara May 26 '13 at 13:30
  • 5
    Sorry, I was unclear that I was talking about the C language only. Digraphs **in C** were added to the language in the mid-1990s. Trigraphs were in the 1989 standard. Pascal used digraphs (`(.` and `.)`` at least) in the 1980s, possibly 1970s. I'm sure there were earlier ones as well. It's all useless baggage in the language(s) now, though. –  May 26 '13 at 13:47
  • 1
    +1 for explaining *why* digraphs exist. – Steve May 26 '13 at 14:26