Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!sdd.hp.com!samsung!rex!uflorida!mailer.cc.fsu.edu!prism!cc100aa From: cc100aa@prism.gatech.EDU (Ray Spalding) Newsgroups: comp.lang.c Subject: Re: When do you use const Keywords: const, #define, C Message-ID: <21031@hydra.gatech.EDU> Date: 1 Feb 91 21:00:45 GMT References: <1220@tredysvr.Tredydev.Unisys.COM> Organization: Georgia Institute of Technology Lines: 22 In article <1220@tredysvr.Tredydev.Unisys.COM> paul@tredysvr.Tredydev.Unisys.COM (Paul Siu) writes: >However, what is the advantage of using const over #define? Why was the >reason for its addition to ANSI C. I believe "const" was added to (a) announce objects that may be placed in read-only memory, (b) provide possible opportunities for optimization, and (c) provide possible opportunities for diagnostics. These would all seem to be possible with #defines of simple constants like 3.14 or "abc" as well. But with "const", you can have read-only pointers, pointers to read-only objects, and read-only structs, arrays, etc. You can also take the address of a "const" variable (with &), but not of a literal constant. Of course, the "const" keyword is also used to denote that a function will not alter an array argument; a different usage but one that can be checked for optimizations and violations in a similar way by a compiler. -- Ray Spalding, Technical Services, Office of Information Technology Georgia Institute of Technology, Atlanta Georgia, 30332-0715 uucp: ...!{allegra,amd,hplabs,ut-ngp}!gatech!prism!cc100aa Internet: cc100aa@prism.gatech.edu