News for hackers. Stuff that matters.
Re:URLs in C (2011) by jasode on Wednesday November 04, 2020 @07:30AM (#24988398)
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...

Re:URLs in C (2011) by robalni on Wednesday November 04, 2020 @11:19AM (#24990374)
This 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.
Re:URLs in C (2011) by nayuki on Wednesday November 04, 2020 @08:52AM (#24989020)
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/
  }
Re:URLs in C (2011) by sgt on Wednesday November 04, 2020 @11:12AM (#24990296)
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
  }
Re:URLs in C (2011) by klodolph on Wednesday November 04, 2020 @10:32AM (#24989889)
Another gotcha is that a statement must follow. The following is an error:

    int main() {
        https://error/
        int x = 3;
    }
Re:URLs in C (2011) by colejohnson66 on Wednesday November 04, 2020 @06:09PM (#24994024)
Why?
Re:URLs in C (2011) by eindiran on Wednesday November 04, 2020 @09:20PM (#24995103)
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...

Re:URLs in C (2011) by tyingq on Wednesday November 04, 2020 @11:05AM (#24990232)
Capitalizing different parts of the scheme would give you some mileage. The standard allows for it.
Re:URLs in C (2011) by iforgotpassword on Wednesday November 04, 2020 @09:47AM (#24989489)
Due to a duplicate protocol
Re:URLs in C (2011) by kevincox on Wednesday November 04, 2020 @09:53AM (#24989547)
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.

Re:URLs in C (2011) by Kuraj on Wednesday November 04, 2020 @11:14AM (#24990317)
And how much we take syntax coloring for granted :)
Re:URLs in C (2011) by rkangel on Wednesday November 04, 2020 @10:44AM (#24990023)
Whereas I saw it the other way round. I parsed that as a comment before I parsed it as a URL.
Re:URLs in C (2011) by aidenn0 on Wednesday November 04, 2020 @04:33PM (#24993277)
That 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/41ab3712d83d7edbdc03bb8e43a7af...

Re:URLs in C (2011) by Saint_Genet on Wednesday November 04, 2020 @07:45AM (#24988486)
Just wish the answer was hidden so I could have given it 10 seconds of thought without accidentally reading it.
Re:URLs in C (2011) by abetusk on Wednesday November 04, 2020 @12:36PM (#24991159)
Yes, while gotos are frowned upon, they're still allowed in C/C++.

Where to goto? A label, of course.

Re:URLs in C (2011) by astrobe_ on Wednesday November 04, 2020 @01:10PM (#24991469)
I'm guessing it is not compatible with calling conventions in C, but one could goto a function name.
Re:URLs in C (2011) by chriszhang on Wednesday November 04, 2020 @01:49PM (#24991826)
Can 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?
Re:URLs in C (2011) by uryga on Wednesday November 04, 2020 @02:18PM (#24992110)
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:

  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/...

Re:URLs in C (2011) by piscisaureus on Wednesday November 04, 2020 @03:17PM (#24992659)
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".

Re:URLs in C (2011) by uryga on Wednesday November 04, 2020 @07:05PM (#24994388)
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:URLs in C (2011) by mastrsushi on Wednesday November 04, 2020 @07:28AM (#24988384)
TLDR http: is parsed as a label and the rest of the url following // becomes a comment.

Nothing special

Re:URLs in C (2011) by simias on Wednesday November 04, 2020 @08:41AM (#24988916)
There are literally 4 sentences in the article before the answer is introduced, I don't think a TLDR is warranted...
Re:URLs in C (2011) by ktpsns on Wednesday November 04, 2020 @07:46AM (#24988490)
goto https;

// nothing to see here