![]() |
|
#1
|
||||
|
||||
Best way to use integer constantsI've been thinking about this ever since I finished my C programming class a few weeks ago.
There's 2 ways I'm aware of for creating integer constansts: 1) Use #define INT 1 or 2) const int=1 Is one way better than the other? I'm aware that using the #define way allows you to change the value for the entire program when needed, but couldn't you do that the other way also? Please correct me if I'm wrong, but I believe that using the #define way allows the entire program (including functions, methods, etc. ) to see the int constant. Using the other way, the int is only visible w/in it's function aspect, i.e. if it's defined in main(), you can't see/use it in function foo(), right? __________________
Start Programming with Python-A beginner's guide to programming and the Python language. ------------- Common Sense v2.0-Striving to make the world a little bit smarter. |
|
#2
|
|||
|
|||
|
Your title has the phrase "Best way". That's kind of like asking what is the best flavor of ice cream. Opinions vary, and even my answer may be different depending on circumstances and my mood at the time.
You have the right idea about the differences in application. The line #define X 1234 Will cause 1234 to be substituted for the name X anywhere in the file after the #define statement. You are not supposed to use any other #define X statement anywhere else in the file. "Normally", #define statements are placed at the beginning of a file. This type of definition is a simple text substitution macro. For example, after the #define statement if you write Code:
it is as if you had written Code:
Code:
Now, this has global scope in the file, but there is nothing to prevent you from using X for something else inside a function. In this simple example, there is no difference in program behavior, except for scope. I think that controlling scope is a Good Thing, and I think most experienced programmers tend not to use global variables (whether with #define or with const), unless absolutely necessary. Now, #define statements can be much more complicated, and expertise with macros can be a valuable adjunct to your programming toolkit. (It can also lead to unreadable code, since you can almost define your own new programming language using #defines. I once read a book where #define statements were used to make a C program look almost like FORTRAN. The book was called something like "C for FORTRAN programmers". The result was programs that couldn't be understood by C programmers or by FORTRAN programmers who hadn't digested the book.) Now back to our simple case. What's the advantage of const int, versus #define? Well, #define causes the preprocessor to substitute the defined text in the file, so that the compiler can't do its normal type-checking. This has been known to let certain types of bugs get through the compile process. C programmers tend to use #define, because, "That's the we we have always done it." "Enlightened" C++ programmers tend to prefer const. (I have heard it said that Bjarne Stroustrop recommends that you avoid preprocessor commands --- like #define --- wherever possible. That's why he put const into the language. It was back-ported into C.) Which is better? Your Mileage May Vary. Dave |
|
#3
|
||||
|
||||
|
Perhaps a better title would have been "Difference between #define and const". Thanks for the info. I have a better understanding now.
I wasn't aware that #define is frowned upon. I also appreciate the fact that "const" allows overriding to occur when needed; it's good to know I can use the same name if needed. __________________
Start Programming with Python-A beginner's guide to programming and the Python language. ------------- Common Sense v2.0-Striving to make the world a little bit smarter. |
|
#4
|
|||
|
|||
|
Quote:
Well, first of all, I didn't point out the fact that the const case is handled differently in C and C++. The following gives compiler errors when compiled as a C program with Borland bcc32 and Microsoft Visual C++, but compiles and executes OK with gnu gcc: CPP / C++ / C Code:
If you comment out the const line and uncomment the #define line, it compiles and executes OK as C and it compiles and executes OK as C++ for all of these compilers. Therefore they are not completely equivalent for C programs. Apparently the Borland and Visual C++ C compilers treat const like any other variables, and don't make their values known to the compiler at compile time (whereas since #define is handled by the preprocessor, the compiler knows its value and can allocate storage for the array). I guess that's why C programmers tend to use #define (even when it would be used as a constant "variable", and not a size parameter used by the compiler). Dave |
|
#5
|
||||
|
||||
InterestingI wonder if that means gcc (& maybe other OSS compilers) is "better" than the commercial offerings. I know it's just how they are designed, but it's the small things that make a difference.
I've been wondering. If you can use C++ as a "better C", meaning you don't have to use OOP but just use the better features of C++, then why bother w/ standard C? I know Unix et al. were written w/ C, but does it really matter nowadays? I know my C++ textbook talks about the same things I learned in C, but it's so much easier to use (no "&d" for one thing). Plus you can use OOP if you really desire. __________________
Start Programming with Python-A beginner's guide to programming and the Python language. ------------- Common Sense v2.0-Striving to make the world a little bit smarter. |
|
#6
|
|||
|
|||
|
Quote:
Gnu gcc has made progress toward implementing the later standard, known as C99, which specifies behavior for such things (and lots of other things). The free version of bcc32, and Microsoft Visual C++ (version 6.0, which is what I have) have not done so. If you continue to program like old-fashioned C programmers have been doing, I think you will be able to do anything you want to. (Using #define instead of const int, for example). Why do I say that you can do pretty much anything in C that you can do in C++? Well, originally at least, C++ was written in C. (Here we observe the sound of the audience gasping ... an actual "aha!" was heard.) C++ has lots of things that make it worth while, and can really improve productivity in many cases. Is C++ better than C for C? Your Mileage May Vary. Regards, Dave |
Recent GIDBlog
Stupid Management Policies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| problems typecasting an int to a string | holbrook | C++ Forum | 9 | 07-May-2004 19:59 |
| converting binary to integer... | tru504187211 | C++ Forum | 3 | 10-Feb-2004 08:11 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The