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 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.
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 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 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. :-)
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 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 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 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: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 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.
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 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 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 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!
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.
Also, typechecking macro arguments with ?: is quite interesting, check it out. :-)
welcome to C programming!
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.
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.
(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.)
Forced to do by Microsoft C and C++ that mangle names.
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.
- 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
Might have been the Lua Programming Gems book...
#define ELSE ;} else {
analogous to FI, to keep the appearance of semicolon as separator in the then clause.
'\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.
In the real world, treating their semantics the same misleads newbies, and leads to bugs.