- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
... which works until you're trying to compare two variables.
C# will save you by requiring if (...) to have a boolean inside it... which works until you have to test boolean variables.
Admin
An even better solution... Let the compiler point these out to you. That way you can put the variable on either side (especially useful when BOTH sides need a varuable)
Admin
Well Visual Studio has a standard header called iso646.h
#include <iso646.h>
#pragma once
#ifndef _ISO646
#define _ISO646
#if !defined(__cplusplus) || defined(_MSC_EXTENSIONS)
#define and &&
#define and_eq &=
#define bitand &
#define bitor |
#define compl ~
#define not !
#define not_eq !=
#define or ||
#define or_eq |=
#define xor ^
#define xor_eq ^=
#endif /* !__cplusplus || _MSC_EXTENSIONS /
#endif / _ISO646 */
<snip>
Admin
If it wasn't a big deal, everyone would use Lisp.
Admin
#define ENDOFPROGRAMMINGLINE ;
He forgot a define for the semicolon. That would make your code so much more understandable!
Admin
As opposed to.... ....I normally use Python, but it's um, implemented in C (well apart from IronPython and Jython)... so if I want to extend, I have to use C... if I want to write a small fast application, I tend to use C. C++ is overkill 80% of the time.
...when I'm working with the Win32 API I prefer to use C for simplicity's sake. What do you use?
Admin
not to mention far more ambiguous!
Admin
<font color="#999999"> if I want to write a small fast application, I tend to use C. C++ is overkill 80% of the time.</font>
It depends what you call overkill. Most of the time the clarity & convenience offered by C++ outweight the (small) performance lost (if any) compared to C.
Admin
Indeed:
#define N(I,l) s l]=(I?1<<I|I<<10:01776)
#define f(a,t) for(a=0;a<t;a++)
/* cut /
#define s S].s[l
/ cut /
Su(row,col,box) C=l0=0;
f(l,81) if(!(s]>>10&&++l0)) {
o=s]&1022; for(I=0;~o&1&&(o/=2);I++); o-1||(s]|=I<<10,C++);
} if(l0==l) {
if(O&&L&2) { O--; goto l0; } goto O;
} for(l0=1;10>l0;l0++) { Su(,,) }
/ cut */
No, the brackets don't match; it's obviously not WTF-worthy though, since it's deliberate. If you see anything like this in production code, please shoot the programmer ASAP. (No-one would do something like this outside the IOCCC contest - right? Right?)
Admin
Comeon, you aren't thinking hard enough. The future is XML programming language, with all the variable names, function names, and operators as their own tag. The IDE would then be an XML parser, which when opening a file would convert the XML elements into your preselected viewing preference, and convert back to XML on a save. The interpreter can JIT compile the XML into the byte code of your choice by means of a virtual byte code processor, which can then be interpreted by the VM of your choice.
Minimum specs for running any program: Athlon64x8 4000 with 10GB of RAM.
Admin
Sorry, should've been clearer. 80% of the time, C++ would be overkill for the code I'm writing in C. I don't mean to generalise. Normally I'm just stringing together a bunch of Win32 API calls to create some small utility function (like my service turner-on/offer for services I use only occasionally like mysql-nt, because I'm too lazy to open services.msc).
If I ever am compelled to work on a large project, I would prefer C++ over C any day, I'm too used to OO.
Beauty of being a hobbyist programmer rather than a professional is that I get to choose what I do my projects in.
However, afaik, there's some stuff that does require C instead of C++. I've written a couple of kernel side drivers, and C++ is bad there, although I do wish I had the objects to use. Yucky stuff.
Admin
Wow, this is a popular topic. It's a little silly, but something I've done in all my recent C/C++ projects is to use:
#define REP(x,y,z) for((x)=(y);(x)<=(z);(x)++)
#define REPN(x,y,z) for((x)=(y);(x)<((z)-(y));(x)++)
#define REPZ(x,z) for((x)=0;(x)<=(z);(x)++)
#define REPZN(x,z) for((x)=0;(x)<(z);(x)++)
as one of my standard headers. Just because after having used so many other languages (every other language on the planet seems to have a "for each" construct) a codebase full of generic for loops is the ugliest thing on god's green earth besides my neighbor's lawn. And yes, I named them after the asm instructions for very-vague similarity.
So far I've had more "hey, neat" than "wtf is this, are you insane", so I'd say success. =D
Admin
Sadly I've seen this before, but worse. My first job was for a Unix software house in the UK who use CBASIC - yep, C with a ton of macros. Some of the joking posts were closer to the truth. I had to spend a couple of years before I managed to get transferred to the systems team, where we could use C properly.
Admin
Actually, that's not a bad idiom. The construct while(1) { ... } generates better assembly on a lot of compilers. You DO have to remember to perform a test and break, of course, but it really does work better. Sometimes you just need a mid-tested loop.
Yes, I'm sure some C++ book author named Steve will recommend against this, but hey, it's a free country.
Admin
Sorry ... The C preprocessor strips out comments before dealing with macros. So, the definition of COMMENT is in fact blank. Although you can use macros in place of C keywords, you can't use them for preprocessor directives.
So while you could do
#define if while
it would only affectif (something) { do_stuff(); }
and not#if some_macro void do_stuff(void); #endif
Admin
Oh BTW I posted this yesterday in the Side Bar, but it's relevant to the topic at hand:
Can you guess what it does without reading the description in the Side Bar?
Admin
Yeah, who's going to maintain the code anyway? It's perfect the first time, right?
Admin
Isn't that just:
#define BEGIN {
#define END }
-shnar
Admin
Just write a quick script that reads the stupid header file, creates a look-up table and then goes through the source tree replacing the macro tokens. Then delete the header file and pretend "better C" never existed. Should take you less than 2 hours. Plus maybe another 4 hours of heavy drinking to wipe "better C" from your memory.
Or pull your hair out.
Admin
Nah, it just crashes...
Admin
No, that's precisley it. do_nothing ensures that the program actually does nothing. Genius!
Admin
This is funny; args.c nearly looks as if it was a shell script. Kind of recursive thinking.
Admin
<FONT face=Georgia>The horror, oh the horror!</FONT>
<FONT face=Georgia>*Shudders*</FONT>
Admin
#include <ALGORITHM>...
Admin
std::for_each doesn't qualify?
Admin
The secret to Better Better C.
#define #define //
Admin
I'm still waiting for Margarine C: "I can't believe it's not better!"
Admin
This is right out of Paul Conte's book, "Common Sense C".
http://ostg.pricegrabber.com/search_fullinfobk.php/isbn=1882419006
Admin
Hey, look on the bright side! At least he defined his macros properly (unless(p) if(NOT (p)) . It would have been a REAL pain if he had used if (NOT p) and p happened to be something like 1 || 1...
Admin
Is it so horrible to just do this?
if (...) {
...
} else {
// do nothing
}
(where ... represents some number of statements/expressions)
Writing code that deliberately doesn't do anything seems asinine to me and the opposite of clear.
Admin
So how many #defines would it take to achieve true natural language programming with C?
Admin
Listen, just because you people never got bitten by
for (...);
This post not [pi]-approved.
Admin
This is an internationalization thing. A lot of keyboards don't have & on them, and there is a significant percentage out there lacking |, ~, ^ and !, as well. I would have thought they'd also define eq for consistency...
Admin
At least in Perl there is a reason why they have both (gt,lt,eq,ne) and (>,<,==,!=) - the former are for lexical (string) comparison, the latter for numeric comparison; necessary because they use the one-type string-is-a-number-is-a-string approach (which could be considered a WTF in itself, but I digress...)
Admin
I think we all should compassionate the tortures you are up to :'(
Admin
#define REP(x,y,z) for((x)=(y);(x)<=(z);(x)++)
#define REPN(x,y,z) for((x)=(y);(x)<((z)-(y));(x)++)
#define REPZ(x,z) for((x)=0;(x)<=(z);(x)++)
#define REPZN(x,z) for((x)=0;(x)<(z);(x)++)
I for one vote wtf is this are you insane...
you forgot MOVB, MOVW
here are some implementation suggestions:
#define JNE(x, y) if (x) GOTO y;
#define JZ(x,y) if (!x) GOTO y;
Please note: I realise that it should be JNE/JE and JZ/JNZ. However most assemblers do not do it this way so we need to keep backward compatibility.
Also please note: I HATE macros
Admin
Hate me if you want, but these might be used to help constrain functions that need it, and add a little bit of commenting.
If a specific function, perhaps a lookup, wants to be programmed in a specific way, then:
while untilreturn
{
// Tells you that you should return, or that it breaks on a return.
}
Granted, this can also be done in commenting...
while ( 1 ) // exit on return
{
// Tells you that you should return, or that it breaks on a return.
}
And also perhaps the do_nothing also adds some readability? Not sure?
That is, if you defined it by { and } (which still does nothing)
#define do_nothing { }
if ( x == y ) do_nothing
else {
printf( "X and Y are not equal!\n" );
}
Yes, yes... I guess the below tells it enough as it is.
if ( x == y ) { }
else {
printf( "X and Y are not equal!\n" );
}
I have had the pleasure to work with people that could not understand the above { } and would have probably been better off with do_nothing.
Furthermore, I have had instances in class projects where it is easier to dump in fortran syntax and port it with #defines than to actually go through line by line and convert the code. These are instances where it is throw away code, or some internal testing or script code that is not very critical.
Admin
I think you actually answered why I consider this a WTF. A person can sit down to program in a language even though he knows two other ones. So why is it necessary to redefine the standard syntax of the language?
I've always felt there are two types of WTF:
Why the ... would you do that!?!
vs
What the ... !?!
This would be of the 1st kind.
Admin
I am going to guess that you are a student or a hobbiest.
The statement about being able to sit down in an unknown language (and I assume unknown application) and fix a bug in a "couple of minutes" is a telling sign. I believe the average for the program I am currently working on is somewhere around 20 hours to fix a bug. The fastest is probably 20-30 minutes and the longest is close to 3 months (and at least 5 different developers).
As a maintainer, I have seen plently of jackasses do stupid things like this. If you are too stupid/lazy to learn the language you are using, kill yourself, please.
There is no excuse for shit like this and it should be allowed as a defense for murder.
Admin
<FONT face="Courier New">#define 0 1</FONT>
<FONT face="Courier New">#define 1 i</FONT>
<FONT face="Courier New">#define i j</FONT>
Admin
Sounds like .Net
Admin
Well, Edgar Dijkstra, defined a "skip" opetator..
I've done something of the kind when I was bitten by a confusion between = and ==
and things like
#define If if ( #define Then ) { #define Else } else { #define Elsif } else if ( #define End }
...
Pascalesque, I know..
Admin
Yeah, OSX kernal should be rewritten in VB.net.
Seriously I don't see the WTF. You read some C code, see a strange header file, take a look and see that it is just a bunch of defines to change the syntax. No prob, you write a short perl script that changes everything back to normal (does exactly what the c preprocessor does) and charge at least a days work for it.
This guy did a favour for those that followed.
Admin
I've seen that in C code used in multinational corporations more than once, C code created by people with university degrees in computer science and software engineering...
Admin
This is the most surprising fact. A developer like Stephen Bourne is using something like that. [8-)]
Admin
Defining your own boolean type in C (or worse, post-standard C++) isn't much different from this.
Admin
I must confess that I've #defined an "unless" construct for myself before too. It's such a handy construct in perl; it's amazing how much more readable code can be when you have a negative conditional construct rather than having to write "if (! ...)" all the time. It's a shame that it isn't available in more languages.
Admin
It is, but it has nothing to with syntax.
Admin
My first thought would be more along the lines of 'Ohhh no, not again' than WTF.
The real WTF will come when he do something like this:
and tries to print a range of quadratic numbers:
i = 1;
and then tries to fix the problem.
Admin
Oh my God!
I actually remember using these on a teletype back in the early eighties. Did nothing for program readability.