![]() |
| | #1 |
| Registered User Join Date: Aug 2006
Posts: 7
| Why are #define macros commonly used in C? [29] Newbie Questions / Answers ..Updated!.., C++ FAQ Lite Last edited by noerrorsfound; 09-08-2009 at 01:06 PM. |
| noerrorsfound is offline | |
| | #2 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| C != C++, that's the simple answer. const integers aren't considered constant expressions in C, so using it, for example, to set the size of an array may not work (it won't work under C90, but it would work in C99). They also take up memory, unless the compiler somehow optimizes them away (though I don't think it's allowed to).
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #3 | |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| Quote:
The simple answer is what Elysia has already said: in C, "const" variables are not constant expressions. Seems strange, but that's the way it is.
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, etc. New project: nort | |
| dwks is offline | |
| | #4 | ||
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| No so sure I could see this as a significant "reason" either: Quote:
Quote:
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS Last edited by MK27; 09-08-2009 at 01:26 PM. | ||
| MK27 is online now | |
| | #5 | |||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,365
| Quote:
Quote:
Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |||
| laserlight is online now | |
| | #6 | |
| Registered User Join Date: Sep 2007
Posts: 372
| Quote:
Code: const size_t BLAH = 50; Code: #define BLAH ((size_t)50) Plus if you're doing C99, you don't even need constant expressions for (local) array sizes anymore. | |
| cas is offline | |
| | #7 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Is the C compiler allowed to make an assumption that the value of a const variable won't change and hence replace occurrences of that variable with its value (even though the variable itself isn't eliminated)?
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #8 | ||
| Registered User Join Date: Sep 2007
Posts: 372
| Quote:
Quote:
Code: #include <stdio.h>
static const int a = 3;
int main(void)
{
int *b;
b = (int *)&a;
*b = 10;
printf("%d\n", a);
return 0;
}
| ||
| cas is offline | |
| | #9 |
| Registered User Join Date: Aug 2009
Posts: 135
| Then why not use a macro in the first place?
__________________ My Platform: Fedora 11 GNU/Linux, GNU GCC Compiler |
| MTK is offline | |
| | #10 |
| Registered User Join Date: Sep 2007
Posts: 372
| |
| cas is offline | |
| | #11 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| Incompatibilities Between ISO C and ISO C++ const in C and C++ mean very different things. A const in C is just a variable with an attribute of "warn me if I try to modify this". For one thing, you can't do this in C. Code: const int size = 10; int myarray[size]; This of course is perfectly legal (and encouraged) in C++. Oh, and the link is required reading for anyone thinking that C++ is a simple superset of C.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #12 |
| Registered User Join Date: Oct 2001
Posts: 2,110
| > A const in C is just a variable with an attribute of "warn me if I try to modify this". While that's true for type "pointer to const something", it's not true for objects that have const, like "const int" or "const pointer to something". In that case, if you modify it directly, that would be an error, and if you try to modify it through a pointer, it's undefined behavior. Last edited by robwhit; 09-09-2009 at 02:42 PM. |
| robwhit is offline | |
| | #13 |
| Registered User Join Date: Aug 2006
Posts: 7
| |
| noerrorsfound is offline | |
| | #14 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| You can't do it in C90, but you can do it in C99, which GCC conveniently supports.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #15 | |
| Registered User Join Date: Aug 2006
Posts: 7
| I understand now. Gcc's default mode is "gnu89" which supports C99 features. In c89 mode: Quote:
| |
| noerrorsfound is offline | |
![]() |
| Tags |
| const, define, macros |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Order of execution of preprocessor macros | DL1 | C Programming | 2 | 04-02-2009 06:52 PM |
| parameterized macros vs. functions | Tbrez | C Programming | 3 | 04-02-2009 12:33 PM |
| Macros inside of macros | Chewie8 | C Programming | 2 | 02-24-2008 03:51 AM |
| VS2003 Macros: Shortcut key bindings? | ahluka | General Discussions | 2 | 11-27-2006 11:38 AM |
| template fn replacements for msg macros | Ken Fitlike | Windows Programming | 17 | 10-30-2002 07:55 AM |