News for hackers. Stuff that matters.
Re:Some Obscure C Features by EGreg on Thursday August 22, 2019 @05:19PM (#20772454)
Here is an obscure c feature:

  int main()
  {
     int a = 8;
     {
       int a = 4;
       /* a is only scoped to this block */
     }
     printf("%d", a); /* prints 8 */
  }
It is also why C++ is not a strict superset of C
Re:Some Obscure C Features by eMSF on Thursday August 22, 2019 @05:29PM (#20772550)
That's hardly obscure: it's Javascript that's the odd one out of the languages with C-like syntax, with its wacky function-level scoping instead of variable shadowing when using 'var', and falling back to global scope when not declaring a variable properly. Also, C and C++ behave identically with your given example.

A perhaps obscure feature is that you can "unshadow" a global variable like this:

  #include <stdio.h>
  int global = 0;
  int main()
  {
    int global = 1;
    {
      extern int global;
      printf("%d\n", global); // prints 0
    }
  }
Re:Some Obscure C Features by dkersten on Thursday August 22, 2019 @05:22PM (#20772480)
> It is also why C++ is not a strict superset of C

Can you explain? That code in C++ also scopes ‘a’ to the block.

EDIT: I see you’ve edited the code, but I think it’s still true in C++. I’ve often done that for RAII and unless I’m mistaken it works just as well when shadowing variables like you’re doing as when not.

Re:Some Obscure C Features by scott_s on Friday August 23, 2019 @11:22AM (#20778556)
Agreed. And I have also used it in C++ for RAII purposes. In C++, braces introduce a scope, and objects local to that scope will be destructed upon exit.
Re:Some Obscure C Features by codesushi42 on Thursday August 22, 2019 @05:41PM (#20772648)
... is this a joke? It's called lexical scoping, and it's supported by more languages than not.
Re:Some Obscure C Features by scott_s on Thursday August 22, 2019 @05:23PM (#20772487)
Re:Some Obscure C Features by wahern on Thursday August 22, 2019 @05:30PM (#20772563)
Off the top of my head, I can't think of any language that doesn't permit this. See https://en.wikipedia.org/wiki/Variable_shadowing
Re:Some Obscure C Features by saagarjha on Thursday August 22, 2019 @05:34PM (#20772597)
Your link mentions CoffeeScript as not allowing shadowing.
Re:Some Obscure C Features by jcranmer on Thursday August 22, 2019 @05:31PM (#20772579)
If you want to tell if your compiler is C or C++, run this program:

   #include <stdio.h>
   int main() {
     printf("%d\n", sizeof('a'));
   }
It's the second thing C++ mentions in its list of incompatibility with C (the first is "new keywords").

A more obscure difference is this program:

   int i;
   int i;
   int main() {
     return i;
   }
It's legal C but not legal C++.
Re:Some Obscure C Features by _kst_ on Thursday August 22, 2019 @05:48PM (#20772697)
sizeof ('a') doesn't reliably tell you whether you're compiling C or C++. It yields the same result in an implementation where sizeof (int) == 1 (which requires CHAR_BIT >= 16). (The difference is that character constants are of type int in C, and of type char in C++.)

So if sizeof ('a') == 1, then either you're compiling as C++ or you're compiling as C under a rather odd implementation.

Both POSIX and Windows require CHAR_BIT==8. The only systems I know of with CHAR_BIT>8 are for digital signal processors (DSPs).

If you want to tell whether you're compiling C or C++:

    #ifdef __cplusplus
    ...
    #else
    ...
    #fi
Re:Some Obscure C Features by ksherlock on Thursday August 22, 2019 @05:37PM (#20772612)

    int main() {
      return 4//**/2
      ;
    }
Re:Some Obscure C Features by eMSF on Thursday August 22, 2019 @05:44PM (#20772669)
Line comments have been part of the C language for a long, long time (added in C99); so much so that, especially when discussing the subject on the internet, more and more often they predate some of the younger participants.
Re:Some Obscure C Features by saagarjha on Thursday August 22, 2019 @06:29PM (#20772969)
Personally, I didn't know that C lacked line comments until I ran into a project that compiled with -std=c89 -pedantic.
Re:Some Obscure C Features by _kst_ on Thursday August 22, 2019 @09:56PM (#20773986)
C doesn't lack line comments. The obsolete 1989/1990/1995 version(s) of C did lack line comments.
Re:Some Obscure C Features by unwind on Thursday August 22, 2019 @06:24PM (#20772948)
You need to use %zu to print values of type size_t, otherwise you get undefined behavior. So in C, don't run that program. :)
Re:Some Obscure C Features by truncate on Thursday August 22, 2019 @05:29PM (#20772559)
I don't think that's obscure at all. That's standard lexical scoping. Do it all the time in functional languages.
Re:Some Obscure C Features by IanS5 on Thursday August 22, 2019 @05:35PM (#20772605)
Blocks are a common feature