Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: modifying local const variable run time Date: Wed, 22 Feb 2017 09:17:25 -0800 Organization: None to speak of Lines: 63 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="49aa0a1b7995c9119124e44e55d9580d"; logging-data="26730"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18gg2m6ZjKqqJt68ohD0pST" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:kwQl/zUl/+kijoqf4/rAAWoYI10= sha1:HKRH6dO4sm4yYy1nleOTtnBduFk= Xref: csiph.com comp.lang.c:104383 rahul.dev.agg@gmail.com writes: > int func(void) > { > const int i = getc(); > > } > > Is this correct ? No, getc() requires an argument of type FILE*. But if we change it to const int i = getc(stdin); it's perfectly valid (though "i" is a bad name for an object meant to hold a character read from input; "c" would be better). > Can we modify const local variable run time ? I think the source of your confusion is the meaning of the keyword "const". Though it's obviously derived from the English word "constant", the words "const" and "constant" mean very different things in C. A "constant" is a literal, like 42 or 1.5. A "constant expression" is an expression that can (and in many cases must) be evaluated at compile time, like 2+2. There are no constants or constant expressions in your function. The keyword "const" really means "read-only". A const-qualified object is not a constant, it's just an object that you're not allowed to modify after it's been initialized. Initializing it (exactly once) is permitted because it wouldn't be particularly useful if you couldn't specify a value for it. Defining an object as "const" doesn't necessarily have any effect on how it's implemented. It could be, and in this case very likely is, stored in memory in exactly the same way that it would be without the "const". The only difference is that the compiler will diagnose any attempts to modify the object, or to store its address in an int* pointer (which would allow it to be modified via the pointer). If the initial value is known at compile time, either because it's a constant expression or because the compiler is able to figure out what its value must be, then there could be opportunities for optimization. The compiler might generate code to store the object in read-only memory (probably not physical ROM, but memory that's marked by the OS as read-only when the program is first loaded), and/or it might not bother to load its value from memory when it's referred to. On the other hand, a compiler could do exactly the same thing with a non-const object if it can prove that it's never actually modified. Bottom line: "const" doesn't mean "constant"; "const" means "read-only". (There are other programming languages in which the word "constant" has a different meaning than it has in C.) -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"