Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #104383
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: modifying local const variable run time |
| Date | 2017-02-22 09:17 -0800 |
| Organization | None to speak of |
| Message-ID | <ln60k2kuvu.fsf@kst-u.example.com> (permalink) |
| References | <c5e88b2a-20a5-4083-a40a-a17d57aa42f7@googlegroups.com> |
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 <http://www.ghoti.net/~kst>
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"
Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread
modifying local const variable run time rahul.dev.agg@gmail.com - 2017-02-22 06:39 -0800
Re: modifying local const variable run time GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-22 16:06 +0100
Re: modifying local const variable run time rahul.dev.agg@gmail.com - 2017-02-22 07:26 -0800
Re: modifying local const variable run time David Brown <david.brown@hesbynett.no> - 2017-02-22 16:31 +0100
Re: modifying local const variable run time rahul.dev.agg@gmail.com - 2017-02-22 07:43 -0800
Re: modifying local const variable run time Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-22 16:06 +0000
Re: modifying local const variable run time James Kuyper <jameskuyper@verizon.net> - 2017-02-22 11:13 -0500
Re: modifying local const variable run time supercat@casperkitty.com - 2017-02-22 08:34 -0800
Re: modifying local const variable run time David Brown <david.brown@hesbynett.no> - 2017-02-22 23:03 +0100
Re: modifying local const variable run time GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-22 17:34 +0100
Re: modifying local const variable run time Robert Wessel <robertwessel2@yahoo.com> - 2017-02-22 10:52 -0600
Re: modifying local const variable run time GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-22 17:52 +0100
Re: modifying local const variable run time Keith Thompson <kst-u@mib.org> - 2017-02-22 09:33 -0800
Re: modifying local const variable run time James Kuyper <jameskuyper@verizon.net> - 2017-02-22 15:50 -0500
Re: modifying local const variable run time supercat@casperkitty.com - 2017-02-22 13:13 -0800
Re: modifying local const variable run time Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-22 21:55 +0000
Re: modifying local const variable run time supercat@casperkitty.com - 2017-02-22 14:43 -0800
Re: modifying local const variable run time Keith Thompson <kst-u@mib.org> - 2017-02-22 09:23 -0800
Re: modifying local const variable run time GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-22 18:32 +0100
Re: modifying local const variable run time Keith Thompson <kst-u@mib.org> - 2017-02-22 09:50 -0800
Re: modifying local const variable run time David Brown <david.brown@hesbynett.no> - 2017-02-22 23:07 +0100
Re: modifying local const variable run time Keith Thompson <kst-u@mib.org> - 2017-02-22 15:20 -0800
Re: modifying local const variable run time David Brown <david.brown@hesbynett.no> - 2017-02-23 09:50 +0100
Re: modifying local const variable run time James Kuyper <jameskuyper@verizon.net> - 2017-02-22 10:16 -0500
Re: modifying local const variable run time Keith Thompson <kst-u@mib.org> - 2017-02-22 09:17 -0800
csiph-web