Sign in to follow this  

Eight, Nine, Ten...

This topic is 1762 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

why someone (sane) would use #define for numbers is beyond me... blink.png

C tradition, mostly.


things like (in the past):
static const int FOO=...;

tending to result in the compilers creating actual variables, and loading the values from them, to access their constants, as well as hindering compile-time expression evaluation, ... (newer compilers have generally at least gotten a little smarter here). (and, if you forgot the 'static', the linker would complain about multiple definitions, ...)

...

Share this post


Link to post
Share on other sites

Yeah that thing was always annoying in C. It wont even let you do this:

static const int size=16;
static const int buffer[size];

That const makes it really a constant and not a variable with a constant value is one of those little things you dont even notice until they are not working which C++ added, together with inline functions, function name overloading, new and delete that avoid the manual NULL check, references, ...

Btw., did this start with someone seeing

#define NULL (0)

and then adding

#define ONE (1)
#define TWO (2)
...

?tongue.png

Share this post


Link to post
Share on other sites


why someone (sane) would use #define for numbers is beyond me... blink.png

C tradition, mostly.

things like (in the past):
static const int FOO=...;
...
#define FOO _SIZE 42
is one thing, but
#define FOURTY_TWO 42
is a whole different kettle of fish.

Share this post


Link to post
Share on other sites

Fun fact: We use octal a lot more than we realize.

From the C/C++ parser’s viewpoint a number beginning with a 0 is octal.

 

What is the most common number that begins with 0?

 

0!

 

Every time you use a 0 in your code it is seen by the compiler as an octal number.

 

And macro’ing names of numbers is just silly.

 

 

L. Spiro

Share this post


Link to post
Share on other sites


What is the most common number that begins with 0?



0!

 
I almost questioned why you are using 0! (aka. zero-bang, aka. 0 factorial). biggrin.png


but 0!=!0...


random trivia...
not being content with just having NULL, I have a number of other values in the same area:
UNDEFINED //like NULL but not NULL (*3)
BGBDY_TRUE //when you need a boolean TRUE value as a pointer (*3)
BGBDY_FALSE //likewise, but FALSE
AUXNULL //for when you need to express a NULL but NULL and UNDEFINED are already busy (*1)
BGBGC_NULLEND //for when your NULL should really be a 'void' (and a thread has just terminated, *2)
...

*1: AUXNULL==NULL except when AUXNULL!=NULL... yes, sometimes actually useful...
*2: generally to save the cost of keeping around the state to hold a NULL that wont actually be referenced (basically, because you can't "join()" a thread which returns 'void').

*3: UNDEFINED is TRUE for "if()" whereas NULL is FALSE, but sometimes also needed is a FALSE/TRUE that are distinct from NULL/UNDEFINED.


as for naming numbers:
actually, surprisingly, there are people who seem to think this practice is a good idea.

apparently especially off in the land of a certain language unnamed here...

Share this post


Link to post
Share on other sites

Could be binary coded decimal... [snip]

 

That sounds so convoluted it requires its own thread. 

Unfortunately that wasn't the case here - this code, according to a colleague who originally fixed it - caused a fatal crash in the software. 

 

BCD was pretty common once, there are even x86 assembler instructions that work with BCD numbers!

But I guess that wasn't what was intended here :)

Share this post


Link to post
Share on other sites

When I was around 18 or so I used to hack around the memory of my PSX games with a gameshark (the kind that plugged into the back and allowed memory/debug access - now considered illegal). I remember coming across BCD in some game when I was searching for the address of the HP values or something similar and when I finally realized what was happening I laughed for a solid five minutes at how crazy it seemed. I wish I could remember what game it was.

Share this post


Link to post
Share on other sites
Sign in to follow this