URLs in C (2011)(susam.in)
susam.in
URLs in C (2011)
https://susam.in/blog/urls-in-c/
23 コメント
Related wikipedia: https://en.wikipedia.org/wiki/Polyglot_(computing)
And C/C++ also has the "-->" operator which, of course, is the "goes to zero" operator: https://stackoverflow.com/questions/1642028/what-is-the-oper...
And the famous joke thread from Stackoverflow with examples of file format abuse: https://stackoverflow.com/questions/5508110/why-is-this-prog...
And C/C++ also has the "-->" operator which, of course, is the "goes to zero" operator: https://stackoverflow.com/questions/1642028/what-is-the-oper...
And the famous joke thread from Stackoverflow with examples of file format abuse: https://stackoverflow.com/questions/5508110/why-is-this-prog...
Just wish the answer was hidden so I could have given it 10 seconds of thought without accidentally reading it.
The gotcha is that if in the same function you put in another URL with the same protocol, the compiler will refuse to compile the code due to a duplicate label.
int main() {
https://acceptable/
http://okay/
https://duplicate.error/
}Luckily there are other protocols we can work with to bypass this limitation.
int main() {
gopher://myhost:151/hello.txt
ftp://ftp.acc.umu.se/pub/netbsd
ldap://yesherewego
dav://getmyfiles
}Another gotcha is that a statement must follow. The following is an error:
int main() {
https://error/
int x = 3;
}Why?
In C, a label requires a statement[0] to come after it. A declaration in C is not a statement (though it is in C++).
gcc for example gives the error "error: a label can only be part of a statement and a declaration is not a statement" for the code in OP's comment.
g++ happily compiles it.
See the first answer here: [1]
[0] https://en.wikipedia.org/wiki/Statement_(computer_science)
[1] https://stackoverflow.com/questions/49861965/declarations-de...
gcc for example gives the error "error: a label can only be part of a statement and a declaration is not a statement" for the code in OP's comment.
g++ happily compiles it.
See the first answer here: [1]
[0] https://en.wikipedia.org/wiki/Statement_(computer_science)
[1] https://stackoverflow.com/questions/49861965/declarations-de...
Capitalizing different parts of the scheme would give you some mileage. The standard allows for it.
Due to a duplicate protocol
Somehow I quickly recognized the `https:` as a label. But I could not for the life of me figure out how `//` would compile. I was thinking it must be some sort of operator.
Brains are funny sometimes, and it is amazing how much we rely on context to remember and parse things.
Brains are funny sometimes, and it is amazing how much we rely on context to remember and parse things.
This reminds me of when I tried to write HTML code in bash.
When I played with this a few years ago I actually discovered a bug in bash. If I had a bash alias with the same name as one of the filenames, bash would do alias expansion on the filename. This bug was triggered by executing a line with two redirection operators before any command.
So if you follow these steps:
<html> hi </html> a
This code when run as a bash script reads from a file with name "html" and writes to file "hi".
It also reads from file "html" (in the root directory) and writes to a file called "a".
The "a" in the end is just to avoid bash errors about unexpected newline.When I played with this a few years ago I actually discovered a bug in bash. If I had a bash alias with the same name as one of the filenames, bash would do alias expansion on the filename. This bug was triggered by executing a line with two redirection operators before any command.
So if you follow these steps:
$ alias a='abc xyz'
$ </ >a
you would get the error (in addition to a file created with name "abc"): bash: xyz: command not found
This happened because bash expanded the second line to: $ </ >abc xyz
and therefore treated "xyz" as a command (and "abc" and the root directory as filenames).
This bug doesn't work anymore in new versions of bash because it has been fixed.Yes, while gotos are frowned upon, they're still allowed in C/C++.
Where to goto? A label, of course.
Where to goto? A label, of course.
I'm guessing it is not compatible with calling conventions in C, but one could goto a function name.
Can you explain "goto a function name" further?
I wrote this:
I wrote this:
#include <stdio.h>
void f() {
printf("f");
}
int main() {
goto f;
return 0;
}
It creates error: foo.c:8:10: error: use of undeclared label 'f'
goto f;
^
1 error generated.
How can we goto a function name?i think GP means that conceptually, you could imagine goto-ing to a function's code - they do compile to labels after all.
fun note: C-- has something like that! iirc it's like calling a function, but reusing the current stackframe:
https://www.microsoft.com/en-us/research/wp-content/uploads/...
fun note: C-- has something like that! iirc it's like calling a function, but reusing the current stackframe:
jump f(expr1,. . .,exprn);
> "the `jump` statement performs a control jump but carrying parameters. It has as target [...] a procedure address and can optionally transfer arguments to that procedure."https://www.microsoft.com/en-us/research/wp-content/uploads/...
This is the first time I've heard about a language called C--, thank you for that.
The concept is not so unique as you might think tho; the more commonly used term is "tail call".
The concept is not so unique as you might think tho; the more commonly used term is "tail call".
i always thought "tail call" means a function call that can be optimized in this way (located in a tail position), not the act of call-by-jumping itself. both uses make sense though
re: C--, i think it's mostly used as a compiler IR. (notably in Haskell's GHC)
re: C--, i think it's mostly used as a compiler IR. (notably in Haskell's GHC)
That reminds me of the expression in C that will evaluate to 0 or 1 depending on whether or not "//" style comments are supported:
Full working demo: https://gist.github.com/jasom/41ab3712d83d7edbdc03bb8e43a7af...
int x = 1 //*
//*/2
;
Without "//" comments this is whitespace equivalent to int x = 1 / 2 ;
With "//" comments this is whitespace equivalent to int x = 1 ;
[edit]Full working demo: https://gist.github.com/jasom/41ab3712d83d7edbdc03bb8e43a7af...
Nothing special