6 ms·
URLs in C (2011)
- mastrsushi 6y agoTLDR http: is parsed as a label and the rest of the url following // becomes a comment. Nothing special
- jasode 6y agoRelated wikipedia: https://en.wikipedia.org/wiki/Polyglot_(computing) 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-operator-in-c/ 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-program-erroneously-rejected-by-three-c-compilers https://stackoverflow.com/questions/5508110/why-is-this-prog...
- Saint_Genet 6y agoJust wish the answer was hidden so I could have given it 10 seconds of thought without accidentally reading it.
- nayuki 6y agoThe 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/ }
- iforgotpassword 6y agoDue to a duplicate protocol
- klodolph 6y agoAnother gotcha is that a statement must follow. The following is an error: int main() { https://error/ int x = 3; }
- colejohnson66 6y agoWhy?
- eindiran 6y agoIn 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) https://en.wikipedia.org/wiki/Statement_(computer_science) [1] https://stackoverflow.com/questions/49861965/declarations-definitions-as-statements-in-c-and-c https://stackoverflow.com/questions/49861965/declarations-de...
- tyingq 6y agoCapitalizing different parts of the scheme would give you some mileage. The standard allows for it.
- sgt 6y agoLuckily 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 }
- kevincox 6y agoSomehow 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.
- robalni 6y agoThis reminds me of when I tried to write HTML code in bash. <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.
- abetusk 6y agoYes, while gotos are frowned upon, they're still allowed in C/C++. Where to goto? A label, of course.
- astrobe_ 6y agoI'm guessing it is not compatible with calling conventions in C, but one could goto a function name.
- chriszhang 6y agoCan you explain "goto a function name" further? 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?
- uryga 6y agoi 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: 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/1998/01/pal-manual.pdf https://www.microsoft.com/en-us/research/wp-content/uploads/...
- piscisaureus 6y agoThis 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".
- uryga 6y agoi 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)
- aidenn0 6y agoThat reminds me of the expression in C that will evaluate to 0 or 1 depending on whether or not "//" style comments are supported: 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/41ab3712d83d7edbdc03bb8e43a7af6e https://gist.github.com/jasom/41ab3712d83d7edbdc03bb8e43a7af...