Reputation: 335
I was seeing this code
#include <stdio.h>
main(t,_,a)
char *a;
{
return!0<t?t<3?main(-79,-13,a+main(-87,1-_,main(-86,0,a+1)+a)):
1,t<_?main(t+1,_,a):3,main(-94,-27+t,a)&&t==2?_<13?
main(2,_+1,"%s %d %d\n"):9:16:t<0?t<-72?main(_,t,
"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l+,/n{n+,/+#n+,/#\
;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l \
q#'+d'K#!/+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# \
){nl]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#n'wk nw' \
iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;#'rdq#w! nr'/ ') }+}{rl#'{n' ')#\
}'+}##(!!/")
:t<-50?_==*a?putchar(31[a]):main(-65,_,a+1):main((*a=='/')+t,_,a+1)
:0<t?main(2,2,"%s"):*a=='/'||main(0,main(-61,*a,
"!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);
}
and I saw this _
main()
I never saw this sign in use and never read about anywhere,
what is it used for?
Upvotes: 6
Views: 11768
Reputation: 145297
You are looking at an example of C code obfuscation.
The programmer uses a single underscore _
as the name for the second argument to the main
function.
He uses an old style declaration for main()
somewhat equivalent to modern int main(int t, int _, char *a)
.
This prototype for main
is invalid per all versions of the C Standard, but may function on some systems and the main
function actually calls itself recursively with arguments of the expected types. The program can tell if it is the main invocation by testing if t > 0
. This is not portable as arguments may be passed differently for different prototypes.
Obfuscated C is a game for advanced C programmers that can reach amazing levels of sophistication.
There is a world wide competition: The International Obfuscated C Code Contest.
Many world class programmers have spent countless hours polishing amazing code gems, including a compiler for a subset of C by Fabrice Bellard that can compile itself.
There is another game for aimless programmers: code golfing. The goal is to produce the smallest program to solve a given problem. Stack Exchange has a full site dedicated to this activity: https://codegolf.stackexchange.com/ . More wasted hours for fun and pleasure.
Upvotes: 3
Reputation: 31
The underscore is one of the characters allowed in the names of variables and functions in C. It's not commonly used, especially at the begin or end of names. For this reason, so it's sometimes useful for:
Your example uses it in the second version, for example by defining a variable simply called _
.
Upvotes: 3
Reputation: 53026
It's just an identifier, it's valid. You can use _
by itself as an identifier.
For example you can do this
int _;
that makes _
a variable of type int
.
The code though, exposes a fundamental problem because it calls main()
from within the program. Doing so, invokes undefined behavior.
Upvotes: 7