PDA

View Full Version : Multiple Defines on One Line


unknown
2005.07.30, 11:30 PM
Can that be done? This is just to make my code neater because I do 16 defines and my code is spagetti elsewhere.

Steven
2005.07.31, 12:07 AM
I'm pretty sure that this is not possible. You can go the other way, however (one define spanning multiple lines)...

Put them in a separate header file if you don't want it cluttering your other files.

Josh
2005.07.31, 12:15 AM
That wouldn't really make your code any neater, anyways.

phydeaux
2005.07.31, 12:21 AM
I assume this is c/c++, in which case if you're relatively new with the language it's also a good idea for constants not do #define them, but to use a const variable instead. That way you get type-checking for free.

Steven
2005.07.31, 12:22 AM
It however, would have some interesting applications in the IOCCC... :D

But yeah, Josh is right - whitespace is good! If your code is spaghetti to the point where you dread reading it, it may be better to reformat it now (possibly even recode some parts) before you get too far away from it. If you can't read it, you generally can't debug it.

unknown
2005.07.31, 01:09 AM
what is IOCCC, my codes spagetti because I thought it would be funny to program the entire game mechancs in one procedure, I think of wierd things when I dont sleep! But its kind of cool looking that way, its crazy and I like it. :p

phydeaux: Its ObjectiveC, Im not relatively new to C but im always learning :D (ive just never read a book on it). Type checking is not necassary here because all im defining is integers describing the position of graphics in a file.

I think ill move the def's to a seperate file.
thanks a lot

Steven
2005.07.31, 01:47 AM
GIYF: http://www.google.com/search?q=IOCCC

It's going to be funny up to the point where you want to change something. Trust me, I've been there. It will be painful later.

If you wish to learn that on your own, I will not stop you ;)

unknown
2005.07.31, 01:54 AM
hahaha, ive heard about that before its pretty cool, Check the obfuscated Quines (self replicators) though!!! I dont know if my codes up to that standard yet! Yeah ive started breaking it up slightly and making it more legible.

OneSadCookie
2005.07.31, 04:20 AM
phydeaux: the "constant variable" approach doesn't work well for C, due to differences in the meaning of "const" between C and C++.

Fenris
2005.07.31, 03:34 PM
Keith: care to elaborate on that?

OneSadCookie
2005.07.31, 05:21 PM
If you put "const int FOO = 3;" in a header file in C, you'll get "multiply defined" link errors for every source file that includes the header. In C++, that'll work correctly.

In order to make it work at all in C, you need to put "static const in FOO = 3;", which will cause a copy of the variable to be emitted for each source file that includes the header, but will avoid the link errors. I'm not sure if it's true any more, but it used to cause GCC to treat it as if it were a global variable rather than an integer constant (much, much slower). If I had to guess, I'd suspect it will be true until GCC 4.1.