- 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
One previous employer had coding standards that may be imagined from this excerpt from their "standard" header:
#define BEGIN { #define END }
Admin
#define FRIST 1 #define SECNOD 0
Admin
Makes sense, if you want to write Pascal in C.
Admin
Easy! Include the mandatory header and stipulate the correct behavior! Just use #undef!
Admin
That would be TRWTF. Other programmers looking at / working with your code would have the logic reversed...
Admin
Just another reason to utterly despise c.
Admin
That's like saying knife crime is a reason to despise knives.
Admin
So near, and yet so far.
C99 added an actual type
_Bool
that behaves like C++'sbool
type (squashes all non-zero values received into true/1, and converts all forms of zero into false/0), andstdbool.h
added three macros,bool
==_Bool
,true
==1
, andfalse
==0
.But the key is that it regards "true" as being 1 and "false" as being zero. Combined with the submitter's problem, and you have the conundrum that
true != TRUE
andfalse != FALSE
...Admin
#define TRUE 0 is too obvious. I prefer enum {TRUE, FALSE};
Admin
The only way to mess it up is to make TRUE 0? No, I don't think so. #define TRUE 1 #define FALSE 2
Admin
Trumpian programming - alternative TRUEs indeed ...
Admin
That’s exactly what RiskSpectrum’s RSA format does. Parts of the format spec use values such as Normal=0, True=1, False=2. However, this is just some nearly-deprecated proprietary file format, not something you are supposed to paste into a C program. And it also misses a FileNotFound value.
Admin
There's prior art.
The original Bourne shell had a header in it called
mac.h
. Here's an excerptYes, that leads to code exactly as horrific as you might guess.
Here's a small excerpt from
main.c
Addendum 2022-06-01 08:00: Here's a link to the source code https://www.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd/sh
Admin
But what if it cannot find a file instead?
Admin
More on a par with: if you let lots of people run around with guns, you're going to get a whole heap of mass murders committed by the possessors of said guns. In this context, the #define construct is analogous to the god-given right to wave weapons of easy and convenient life termination around.
Admin
Another more sneaky variant would be to define TRUE to be something non-1:
Then, if statements would still work normally, UNLESS you try to explicitly compare with TRUE:
Admin
#1) The second rule: Never compare to TRUE
#2) 0 for "success", non-zero for error (typically negative) is a competing paradigm where it is often deirable to decay into a dual state (aka boolean) but if ""true" == "success" then you end up in exactly the position of todays article.
Admin
It's not very common to see C employed offensively against others, but it's well known that if you ever need a footgun, C will hand you a loaded one.
Admin
One word: bash
Admin
Actually, I usually define SUCCESS as 0 if given the chance. The reason for this is that Success is Unique; Failure can have many values. 0 is unique also.
Admin
#define GOOD MURDER
Admin
Murders happen because of people, not guns or knives. Bad people do bad things. Why are there so many bad people? That's a huge philosophical question - which probably doesn't belong on TDWTF.
Speaking of which ... you are welcome to take your political musings to any of the gajillion political forums.
Admin
POOL is closed!
Admin
#define true false
Admin
https://www.theonion.com/no-way-to-prevent-this-says-only-nation-where-this-r-1848971668
Admin
America's gun laws are insane. But in Ukraine running around with guns is an entirely different story. It sounds like you're saying there is no valid use of #def.
Admin
No one here is interested in your far right propaganda. If anyone's out of place here...
Admin
Well, it was a fun site while it lasted. So long.
Admin
I am trying to remember if I submitted this WTF, but I worked for a company and had a manager who was a BASIC programmer. But management declared that future work was to be done in C on U**x. (A good decision made in ignorance.) The manager created a header file with all his comfortable BASIC keywords and insisted everyone use it. But upper management was pulling in a sizable group of C programmers fresh from college and the manager just couldn't get everyone to "behave." I saw him, head in hands one day, with the look of surrender. He had clearly lost control of his own department. He was gone shortly after.
Admin
Between yesterday's and today's WTF boolean code snippets, I'm thrilled to present my own.
#define TRUE 3.14159
#define FALSE 2.71828
That should be clear enough
Admin
You didn't understand what I wrote. My point is, this is not a political website. Let's talk about software. If you want to debate guns or anything else political, there are plenty of websites to do that.
Neither me nor you are out of place here because of the position we hold on any of these issues. If you think I'm not allowed to say what I think because you disagree with it (as opposed to because this is a software website), well... I'm not surprised, really.
Admin
Back in the day (mid 90s) we were taught to use:
#define TRUE -1 #define FALSE 0
That way, if you had code that for some reason used the ~ operator (bitwise negation) instead of ! (logical negation) it still would work.
Because ~1 is actually 2, which is still true.
Admin
The attitude you're describing here gets people killed.
Admin
Ok, #defining TRUE as 0 in a C header should be the #1 WTF on this site.
Admin
Oh my... Good luck finding every occurrence of if (boolean) and deciding if is intentional or not.
Admin
Return codes (where there can be only one success, but many types of failure) are not the opposite of the standard boolean, but they are a different way of looking at things. If you treat the return code as "ERROR LEVEL", then it works perfectly with a boolean false being 0 and true being anything else. If the process succeeded, the ERROR is false: that is, "it is false that there was an error".
This is the one thing I actually like about DOS batch files - after running a program, you can call "if" on %ERRORLEVEL% to do something. (Note: it has been many years since I worked with DOS batch files, and may have gotten the parameter name slightly wrong, but the behaviour is pretty much spot on.)
Admin
Does keeping a software website focused on software, get people killed? That is interesting. :)
Seriously though, if anybody wants to debate politics - no problem, post a link to another platform and we can continue there.
Admin
If (and only if) the code always compared with TRUE and FALSE ( like if(value == TRUE) ) and also returnden consistently TRUE and FALSE then one should just chage the defines and recompile everything. And afetr that remove the eyesore (== TRUE etc). (The order of those two is not important, they can be reversed).
Admin
You're welcome to say that this isn't the place for politics. But what you did was make a political argument while at the same time complaining that this isn't the place for politics. So if everyone is annoyed at you, it's because you aren't fooling anyone.
Admin
I can only guess that somebody was planning ahead to make sure they kept their job for life, or it's military, to confuse the enemies.
Either way: I'm attributing this to maliciousness, not stupidity.
Admin
S_FALSE
would like to speak to you.Admin
Only one success, in my day we had it tough. there was no such thing as one success, we used to DREAM of only one success !!
I give you Microsoft Robocopy, useful copy/archive/backup/mirror utility often used in scripts for automated copying of newer files only in backup mode across networks etc.. Run from Powershell/DOS batch files examples below (codes below 8 are various levels of success)
ROBOCOPY Exit Codes The return code from Robocopy is a bitmap, defined as follows: Hex Decimal Meaning if set 0×00 0 No errors occurred, and no copying was done. The source and destination directory trees are completely synchronized. 0×01 1 One or more files were copied successfully (that is, new files have arrived). 0×02 2 Some Extra files or directories were detected. No files were copied Examine the output log for details. 0×04 4 Some Mismatched files or directories were detected. Examine the output log. Housekeeping might be required. 0×08 8 Some files or directories could not be copied (copy errors occurred and the retry limit was exceeded). Check these errors further. 0×10 16 Serious error. Robocopy did not copy any files. Either a usage error or an error due to insufficient access privileges on the source or destination directories.
These can be combined, giving a few extra exit codes:
.....
Admin
Damn the formatting try again
ROBOCOPY Exit Codes
The return code from Robocopy is a bitmap, defined as follows:
Admin
Also I once removed lots of warnings from an open source package, because someone had decided to redfine true and false, amongst many other faux pas.
Of course the original developers, had never compiled with ALL warnings and errors levels.
Admin
I wasn't the first one to start. Scroll up and you will see yourself.
Admin
int relevant = (topic == SOFTWARE); if (relevant == TRUE) post();
Admin
Years ago I worked with a guy who really liked PASCAL. And so he created a bunch of #define's for C to let his C look a lot like PASCAL.
Admin
Show me a C programmer who hasn't worked with that guy, and I'll show you the guy.
Admin
That's also the reason Microsoft uses
#define ERROR_SUCCESS 0
(with positive error values) and#define NO_ERROR 0
(I don't know what values this one is opposed to) and#define S_OK 0
(with negative error values)Admin
"I wasn't the first one to start."
That's a kindergartner's argument. You discussed politics and then immediately complained that this isn't the place to discuss politics. You can't have it both ways. If you want to rebut a political argument you disagree with, go ahead, but don't then complain that nobody else should be doing that. If you want to object to politics on thedailywtf, have at it, but do it without arguing about politics. Otherwise you're a hypocrite.