• Frank Wilhoit (unregistered)

    One previous employer had coding standards that may be imagined from this excerpt from their "standard" header:

    #define BEGIN { #define END }

  • (nodebb)

    #define FRIST 1 #define SECNOD 0

  • (nodebb) in reply to Frank Wilhoit

    Makes sense, if you want to write Pascal in C.

  • Sole Purpose Of Visit (unregistered)

    Easy! Include the mandatory header and stipulate the correct behavior! Just use #undef!

  • DQ (unregistered) in reply to Sole Purpose Of Visit

    That would be TRWTF. Other programmers looking at / working with your code would have the logic reversed...

  • Prime Mover (unregistered)

    Just another reason to utterly despise c.

  • Dave (unregistered) in reply to Prime Mover

    That's like saying knife crime is a reason to despise knives.

  • (nodebb)

    This is, with some caveats, essentially what stdbool.h added in C99.

    So near, and yet so far.

    C99 added an actual type _Bool that behaves like C++'s bool type (squashes all non-zero values received into true/1, and converts all forms of zero into false/0), and stdbool.h added three macros, bool==_Bool, true==1, and false==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 and false != FALSE...

  • Ulli (unregistered)

    #define TRUE 0 is too obvious. I prefer enum {TRUE, FALSE};

  • (nodebb)

    The only way to mess it up is to make TRUE 0? No, I don't think so. #define TRUE 1 #define FALSE 2

  • Steve (unregistered)

    Trumpian programming - alternative TRUEs indeed ...

  • Gaetan (unregistered) in reply to Robert_Morson

    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.

  • (nodebb) in reply to Frank Wilhoit

    There's prior art.

    The original Bourne shell had a header in it called mac.h. Here's an excerpt

    #define IF	if(
    #define THEN	){
    #define ELSE	} else {
    #define ELIF	} else if (
    #define FI	;}
    

    Yes, that leads to code exactly as horrific as you might guess.

    Here's a small excerpt from main.c

    	/* command loop */
    	LOOP	tdystak(0);
    		stakchk(); /* may reduce sbrk */
    		exitset();
    		IF (flags&prompt) ANDF standin->fstak==0 ANDF !eof
    		THEN	IF mailnod.namval
    			    ANDF stat(mailnod.namval,&statb)>=0 ANDF statb.st_size
    			    ANDF (statb.st_mtime != mailtime)
    			    ANDF mailtime
    			THEN	prs(mailmsg)
    			FI
    			mailtime=statb.st_mtime;
    			prs(ps1nod.namval); alarm(TIMEOUT); flags |= waiting;
    		FI
    
    		trapnote=0; peekc=readc();
    		IF eof
    		THEN	return;
    		FI
    		alarm(0); flags &= ~waiting;
    		execute(cmd(NL,MTFLG),0);
    		eof |= (flags&oneflg);
    	POOL
    

    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

  • JP (unregistered)

    But what if it cannot find a file instead?

  • Prime Mover (unregistered) in reply to Dave

    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.

  • Dru (unregistered)

    Another more sneaky variant would be to define TRUE to be something non-1:

    #define TRUE (-1) #define FALSE 0

    Then, if statements would still work normally, UNLESS you try to explicitly compare with TRUE:

    int is_true() { return TRUE; } int is_one() { return 1; }

    if (is_true()) {} // OK if ((is_one()) == TRUE) {} // Nope

  • (nodebb)

    #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.

  • Dru (unregistered) in reply to Prime Mover

    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.

  • Rusz (unregistered)

    One word: bash

  • Charles H (unregistered)

    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.

  • (nodebb)

    #define GOOD MURDER

  • (nodebb) in reply to Prime Mover

    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.

  • I dunno LOL ¯\(°_o)/¯ (unregistered) in reply to Jeremy Pereira

    POOL is closed!

  • Brad (unregistered)

    #define true false

  • Naomi (unregistered) in reply to Mr. TA

    https://www.theonion.com/no-way-to-prevent-this-says-only-nation-where-this-r-1848971668

  • Dave (unregistered) in reply to Prime Mover

    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.

  • Dave (unregistered) in reply to Mr. TA

    No one here is interested in your far right propaganda. If anyone's out of place here...

  • Kurt Duncan (unregistered)

    Well, it was a fun site while it lasted. So long.

  • Argle (unregistered) in reply to Jeremy Pereira

    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.

  • (nodebb)

    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

  • (nodebb) in reply to Dave

    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.

  • Anon (unregistered)

    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.

  • Naomi (unregistered) in reply to Mr. TA

    The attitude you're describing here gets people killed.

  • Yikes (unregistered)

    Ok, #defining TRUE as 0 in a C header should be the #1 WTF on this site.

  • Giulio (unregistered)

    Oh my... Good luck finding every occurrence of if (boolean) and deciding if is intentional or not.

  • enkorvaks (unregistered) in reply to TheCPUWizard

    #2) 0 for "success", non-zero for error (typically negative) is a competing paradigm where it is often desirable to decay into a dual state (aka boolean) but if ""true" == "success" then you end up in exactly the position of todays article.

    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.)

  • (nodebb) in reply to Naomi

    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.

  • ismo (unregistered)

    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).

  • diogenes (unregistered) in reply to Mr. TA

    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.

  • FTB (unregistered)

    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.

  • (nodebb) in reply to enkorvaks

    Return codes (where there can be only one success, but many types of failure)

    S_FALSE would like to speak to you.

  • Just another Embedded Designer (unregistered) in reply to enkorvaks

    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:

    0×03   3       (2+1) Some files were copied. Additional files were present. 
    	   No failure was encountered.
    

    .....

  • Just another Embedded Designer (unregistered)

    Damn the formatting try again

    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.
    
  • Just another Embedded Designer (unregistered)

    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.

  • (nodebb) in reply to diogenes

    I wasn't the first one to start. Scroll up and you will see yourself.

  • Duke of New York (unregistered) in reply to Naomi

    int relevant = (topic == SOFTWARE); if (relevant == TRUE) post();

  • jay (unregistered) in reply to Jeremy Pereira

    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.

  • Duke of New York (unregistered) in reply to jay

    Show me a C programmer who hasn't worked with that guy, and I'll show you the guy.

  • (nodebb) in reply to Charles H

    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)

  • nasch (unregistered) in reply to Mr. TA

    "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.

Leave a comment on “True Enough”

Log In or post as a guest

Replying to comment #:

« Return to Article