News for hackers. Stuff that matters.
Re:Having fun abusing the C language by bluetomcat on Tuesday October 25, 2016 @10:44AM (#12788412)
A lesser-known quirk about typedef is that syntactically, it is treated like any other storage-class specifier (static, extern, register, auto). Since the ordering of storage-class specifiers is insignificant to the rest of the specifiers, it means that the following statements have the same effect:

    typedef int myint;
    int typedef myint;
The only difference here compared to a normal declaration is that the name "myint" defines a type and not an "object" as per the standard.
Re:Having fun abusing the C language by nneonneo on Tuesday October 25, 2016 @12:34PM (#12789335)
I've been programming C for the better part of my life now, and I've _never_ seen this. Neat!
Re:Having fun abusing the C language by beeforpork on Tuesday October 25, 2016 @04:35PM (#12791436)
Wow!
Re:Having fun abusing the C language by jasode on Tuesday October 25, 2016 @09:43AM (#12787870)
>, subscripts are just adding to pointers, and since addition is commutative, then so is the subscript operator. [...] A lot to unpack from such little code, and plenty to learn.

It was over 25 years ago that I first came across the C language FAQ from the Usenet group comp.lang.c and it explained the "commutative property" of array subscripts. (Online version here: http://c-faq.com/aryptr/joke.html). Another similar stackoverflow Q&A has also been done: http://stackoverflow.com/questions/381542/with-c-arrays-why-...

Over the last 2 decades, I have never needed the knowledge that the C language lets you put a number outside the brackets instead of inside it. Other than deciphering obfuscated C puzzles, I've never run across any real world source code that took advantage of that syntactic quirk. So yes, I did learn that trivia about array subscripts -- but it was useless knowledge.

Re:Having fun abusing the C language by bodyfour on Tuesday October 25, 2016 @11:17AM (#12788654)
The only time I've found it useful is making macros that expect an array a little safer in C++. For example:

    #define count(array) (sizeof(array) / sizeof(0[array]))
...will conveniently cause a compile error if you accidentally pass in a std::vector instead of an array.
Re:Having fun abusing the C language by jasode on Tuesday October 25, 2016 @11:29AM (#12788769)
Thanks for the heads up. Not suprisingly, a related stackoverflow q&a mentions that macro and also another more convoluted one from Chromium that protects against accidental pointers being passed: http://stackoverflow.com/a/1598827
Re:Having fun abusing the C language by nicky0 on Tuesday October 25, 2016 @11:34AM (#12788807)
I have been C programming on and off for 20 years, and thought I know every trick. But no.
Re:Having fun abusing the C language by beeforpork on Tuesday October 25, 2016 @04:33PM (#12791412)
20 years it is for me, too. And the exact rules of integer constants and their exact type have puzzled me lately. So I was not surprised to find more tricks for countof() here, to make it more safe to use. Nice!

Also, typechecking macro arguments with ?: is quite interesting, check it out. :-)

Re:Having fun abusing the C language by adamnemecek on Tuesday October 25, 2016 @12:03PM (#12789037)
> but it was useless knowledge.

welcome to C programming!

Re:Having fun abusing the C language by nneonneo on Tuesday October 25, 2016 @06:31PM (#12792346)
The IOCCC (http://www.ioccc.org) is probably one of my favorite rabbit holes, since I program very frequently in C. I once fully explained one of the entries (2006-sykes2) on StackOverflow, which was quite educational for me: http://stackoverflow.com/questions/15393441/obfuscated-c-cod...

It's hard for me to pick a favorite, but perhaps one of the most memorable is 2004-gavare: http://www.ioccc.org/2004/gavare.c. This little gem implements a fairly sophisticated ray tracer using no C keywords or preprocessor directives at all: no "int", "char", "if", "return", "#define", etc. I learned a great deal about computer graphics (and, yes, C programming) from reverse-engineering that program.

Re:Having fun abusing the C language by 0xmohit on Tuesday October 25, 2016 @01:20PM (#12789752)
Thankfully trigraphs [0] aren't enabled in compilers such as gcc by default else the following would print "oops":

  #include <stdio.h>
  int main() {
      if ('|' != '??!') {
          printf("ok");
      }
      else {
          printf("oops");
      }
      return 0;
  }
[0] https://en.wikipedia.org/wiki/Digraphs_and_trigraphs
Re:Having fun abusing the C language by ufo on Tuesday October 25, 2016 @01:43PM (#12789937)
Didn't you mean != instead of == ?
Re:Having fun abusing the C language by 0xmohit on Tuesday October 25, 2016 @02:05PM (#12790107)
Yes, indeed. Thanks, fixed.
Re:Having fun abusing the C language by stormbrew on Tuesday October 25, 2016 @11:26AM (#12788742)
If you ever want to give a c programmer a hard time, somehow sneak this into one of their headers:

    #define struct union
Re:Having fun abusing the C language by naasking on Tuesday October 25, 2016 @12:41PM (#12789391)

    #define NULL rand()
Re:Having fun abusing the C language by CJefferson on Tuesday October 25, 2016 @03:08PM (#12790718)
better,

    #ifdef NULL
    #undef NULL
    #endif
    #define NULL (void*)((size_t)!rand())
* Doesn't trigger errors * Will almost always return 0 (every time rand() returns a non-0 value), so code will almost never crash.
Re:Having fun abusing the C language by danieltillett on Wednesday October 26, 2016 @05:55AM (#12795178)
This is so evil. Now hide this in some macro and the fun will never end.
Re:Having fun abusing the C language by hairy_man674 on Tuesday October 25, 2016 @12:54PM (#12789511)
Any preprocessor definition that changes the language like that example makes C programmers rage. As a rule, typedef and macros and friends should be avoided for clarity and debugging (opinion). An actual example of this: The programmer who created Bourne Shell, at Bell Labs in 1970s, intentionally did this to make C more like Algol. I think BSD still uses some of the original code. Understandably, not everyone liked the idea.
Re:Having fun abusing the C language by chc on Tuesday October 25, 2016 @02:47PM (#12790515)
What's the problem with typedef? It just names a new type rather than making things work differently than you'd expect.
Re:Having fun abusing the C language by hairy_man674 on Tuesday October 25, 2016 @03:18PM (#12790817)
The main misuse, again this is opinion based, is that it hides (obscures) the type of variable. If you have (say) a very large struct containing several member variables and functions, a programmer can be misled into returning it from a function or declaring it on the stack.

Of course there are reasonable uses for it. Contrary, to the above you might use it to intentionally make an object opaque as a way of abstracting its details from users or when declaring use of int/long in code.

Re:Having fun abusing the C language by gefh on Tuesday October 25, 2016 @02:21PM (#12790242)
Legit usage: #define private public #include "some old thing I need the internals of but can't change.h"
Re:Having fun abusing the C language by to3m on Tuesday October 25, 2016 @03:37PM (#12790982)
There's at least one compiler that adds visibility into the name mangling (is it gcc? Seems like the kind of thing they'd do), so if you unhide functions in this way you still won't necessarily be able to call them.

(The compiler is also free to change the object layout when you do this, too, though I've no idea whether any of them do.)

Re:Having fun abusing the C language by hairy_man674 on Tuesday October 25, 2016 @03:55PM (#12791119)
> Seems like the kind of thing they'd do

Forced to do by Microsoft C and C++ that mangle names.

Re:Having fun abusing the C language by hairy_man674 on Tuesday October 25, 2016 @03:29PM (#12790921)
C has no such keywords. Why the need for this hackery? Curious also if putting that in a header and including it in a C++ program even compiles without leading to undefined behaviour...
Re:Having fun abusing the C language by raverbashing on Tuesday October 25, 2016 @11:34AM (#12788809)
Ah there are funnier, less obvious ways
Re:Having fun abusing the C language by stormbrew on Tuesday October 25, 2016 @02:07PM (#12790125)
I mean, as the sibling notes, there's redefining true as false, but I think making all structs unions is much funnier, especially in that it's a uniquely C proposition. There are other languages that allow redefinition of true and false.

Also, I'm not sure how you think it's particularly obvious (as opposed to any other #define trick). It does only work if the project isn't warning-safe, as there are various ways this'll trigger ugly warnings (eg. using designated initializer syntax causes re-initialization warnings), but even then the warnings don't tend to actually tell you it's a union all of a sudden.

If you have others, I'd like to hear them.

Re:Having fun abusing the C language by raverbashing on Wednesday October 26, 2016 @05:07AM (#12794979)
- Change the alignment of structs

- Remember the order of includes change how they are applied. Have a #define that changes something in a .h that's included in some files but not in others

- (Depending on the software) redefine rand() to be almost predictable or with a short loop

Re:Having fun abusing the C language by qwertyuiop924 on Tuesday October 25, 2016 @03:36PM (#12790975)

  #define if while
  #define else while(!true){}
That should be interesting.
Re:Having fun abusing the C language by qwertyuiop924 on Tuesday October 25, 2016 @11:45AM (#12788891)
The classic is:

  #define TRUE FALSE
or the even more pernicious:

  #define 1 0
Re:Having fun abusing the C language by ctdonath on Tuesday October 25, 2016 @12:28PM (#12789283)
I once chased a bug which turned out to be: someone had #defined the letter 'T' as something else, in a chain of #include files nested 19 deep. Completely screwed up a bunch of C++ templates.
Re:Having fun abusing the C language by digi_owl on Tuesday October 25, 2016 @12:25PM (#12789244)
Straight from the Ministry of Truth that one...
Re:Having fun abusing the C language by therealidiot on Tuesday October 25, 2016 @02:57PM (#12790619)
I read somewhere about someone redefining goto as goat, but I can't remember where.

Might have been the Lua Programming Gems book...

Re:Having fun abusing the C language by bcheung on Tuesday October 25, 2016 @04:37PM (#12791452)
You forgot

    char *main = [... assembly hex codes here ...]
I pissed off a CS teaching in high school doing that.
Re:Having fun abusing the C language by jejones3141 on Tuesday October 25, 2016 @10:07AM (#12788092)
It should be

#define ELSE ;} else {

analogous to FI, to keep the appearance of semicolon as separator in the then clause.

Re:Having fun abusing the C language by sigjuice on Tuesday October 25, 2016 @05:52PM (#12792025)
What do '\0', '\n', and '\9' mean?
Re:Having fun abusing the C language by ekimekim on Tuesday October 25, 2016 @11:24PM (#12793658)
These are escape sequences for non-printing characters. Well, the first two are. '\9' would most likely cause a compile error, since that format '\<digit>' is only defined for octal numbers, ie. digits 0-7.

'\0' means the NUL character, the value 0. Most of the time in C, this character is a special terminator value at the end of strings.

'\n' means the newline character, aka the line feed character, ascii value 10, which marks where one line ends and the next begins.

Re:Having fun abusing the C language by nercht12 on Tuesday October 25, 2016 @05:58PM (#12792086)
\wat?
Re:Having fun abusing the C language by 0xmohit on Tuesday October 25, 2016 @01:15PM (#12789702)
The following might be fun too:

  #define if while
Re:Having fun abusing the C language by mfukar on Tuesday October 25, 2016 @11:18AM (#12788664)

    subscripts are just adding to pointers, and since addition is commutative, then so is the subscript operator.
Only pointer arithmetic has nothing to do with integer arithmetic, a fact which has bitten countless people in the ass over the years.
Re:Having fun abusing the C language by qwertyuiop924 on Tuesday October 25, 2016 @11:44AM (#12788883)
No, pointer arithmetic has a lot to do with int arithmetic. And as in int arithmetic, pointer arithmetic is commutative.
Re:Having fun abusing the C language by mfukar on Wednesday October 26, 2016 @03:24AM (#12794568)
Alright, yes, they share a property and the syntax. I forgot we were in a programming courthouse.

In the real world, treating their semantics the same misleads newbies, and leads to bugs.

Re:Having fun abusing the C language by tinus_hn on Wednesday October 26, 2016 @08:19AM (#12795825)
Yeah, who would do such a thing! Especially in a piece of code written for the International Obfuscated C Code Contest! I demand that all code submitted for that contest be readable and unobfuscated!
Re:Having fun abusing the C language by mfukar on Thursday October 27, 2016 @02:48AM (#12803312)
Go vent someplace else.