Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can int _$[:>=<%-!.0,}; compile?

Tags:

c

syntax

Today I found strange syntax like

 int _$[:>=<%-!.0,}; 

in some old code, but in fact the code is not commented. There seems to be no report of compile errors for this line. I tested it separately and it can compile too:

int main(){     int _$[:>=<%-!.0,};     return 0; } 

Why can it compile?

like image 718
ggrr Avatar asked Aug 14 '15 03:08

ggrr


1 Answers

With Digraph (see below), the line is converted to:

int _$[]={-!.0,}; 

On the right hand side, .0 is the double literal, ! is the logical negation operator, - is the arithmetic negation operator, and , is the trailing comma. Together {-!.0,} is an array initializer.

The left hand side int _$[] defines an int array. However, there's one last problem, _$ is not a valid identifier in standard C. Some compilers (e.g, gcc) supports it as extension.


C11 §6.4.6 Punctuators

In all aspects of the language, the six tokens

<: :> <% %> %: %:%: 

behave, respectively, the same as the six tokens

[  ]  {  }  #  ## 
like image 195
Yu Hao Avatar answered Sep 22 '22 08:09

Yu Hao