Here are some C code with the JavaScript quirks. Credits to luna.
#include <stdio.h>
int main() {
puts("-0.5" + 1);
printf("%d\n", 50 ** "2");
return 0;
}
To build and run the above code, just do the following and ignore the warnings.
$ gcc c-js.c && ./a.out
0.5
2500
Let’s look at the warnings first.
c-js.c:4:15: warning: adding 'int' to a string does not append to the string [-Wstring-plus-int]
puts("-0.5" + 1);
~~~~~~~^~~
c-js.c:4:15: note: use array indexing to silence this warning
puts("-0.5" + 1);
^
& [ ]
1 warning generated.
Now let’s explain it.
puts
takes a const char*
as the param. "-0.5"
is a string.
50 ** "2" = 50 * (*"2") = 50 * ("2"[0]) = 50 * ('2') = 50 * 50
The ASCII code of the digit 2 happens to be 50.