* can't initialize a constant using another constant?
@ 2008-02-26 14:32 Shriramana Sharma
2008-02-27 8:09 ` Kristof Provost
2008-02-27 11:43 ` Glynn Clements
0 siblings, 2 replies; 6+ messages in thread
From: Shriramana Sharma @ 2008-02-26 14:32 UTC (permalink / raw)
To: Linux C Programming List
Dear list,
Quite some time since I wrote. I hope you are all keeping well. :)
A strange thing I ran across this evening. I was not allowed to do:
const double lunarSynodicMonth = 29.53059 ;
const double daysLunarPhaseAngleTakesPerDegree = lunarSynodicMonth / 360.0 ;
I got the following error:
wdf-karanas.c:17: error: initializer element is not constant
make: *** [wdf-karanas.o] Error 1
I would be grateful if anyone could shed some light. TIA.
Shriramana Sharma.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: can't initialize a constant using another constant?
2008-02-26 14:32 can't initialize a constant using another constant? Shriramana Sharma
@ 2008-02-27 8:09 ` Kristof Provost
2008-02-27 8:33 ` Kristof Provost
2008-02-27 11:43 ` Glynn Clements
1 sibling, 1 reply; 6+ messages in thread
From: Kristof Provost @ 2008-02-27 8:09 UTC (permalink / raw)
To: Shriramana Sharma; +Cc: Linux C Programming List
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 2008-02-26 20:02:06 (+0530), Shriramana Sharma <samjnaa@gmail.com> wrote:
> Dear list,
>
> Quite some time since I wrote. I hope you are all keeping well. :)
>
> A strange thing I ran across this evening. I was not allowed to do:
>
> const double lunarSynodicMonth = 29.53059 ;
> const double daysLunarPhaseAngleTakesPerDegree = lunarSynodicMonth / 360.0 ;
>
> I got the following error:
>
> wdf-karanas.c:17: error: initializer element is not constant
> make: *** [wdf-karanas.o] Error 1
>
> I would be grateful if anyone could shed some light. TIA.
What compiler are you using?
I did a quick test on g++ 4.1.2 (x86_64) and it seemed to be accepted
without warning.
Kristof
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
iD8DBQFHxRrVUEZ9DhGwDugRAjZBAJ9pl0mGAJHPGbwePR0DVXoVh4kRzwCgnTn2
zzdeiILqi1JZ9M9VbUBewr8=
=KWWv
-----END PGP SIGNATURE-----
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: can't initialize a constant using another constant?
2008-02-27 8:09 ` Kristof Provost
@ 2008-02-27 8:33 ` Kristof Provost
0 siblings, 0 replies; 6+ messages in thread
From: Kristof Provost @ 2008-02-27 8:33 UTC (permalink / raw)
To: Shriramana Sharma; +Cc: Linux C Programming List
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 2008-02-27 08:09:57 (+0000), Kristof Provost <Kristof@sigsegv.be> wrote:
> On 2008-02-26 20:02:06 (+0530), Shriramana Sharma <samjnaa@gmail.com> wrote:
> > A strange thing I ran across this evening. I was not allowed to do:
> >
> > const double lunarSynodicMonth = 29.53059 ;
> > const double daysLunarPhaseAngleTakesPerDegree = lunarSynodicMonth / 360.0 ;
> >
> > I got the following error:
> >
> > wdf-karanas.c:17: error: initializer element is not constant
> > make: *** [wdf-karanas.o] Error 1
> >
> > I would be grateful if anyone could shed some light. TIA.
> What compiler are you using?
> I did a quick test on g++ 4.1.2 (x86_64) and it seemed to be accepted
> without warning.
I took a better look. I get the same error with gcc.
It looks like this is allowed in C++ and not in plain C (or at least,
g++ allows it and gcc doesn't).
Kristof
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
iD8DBQFHxSBIUEZ9DhGwDugRAkCiAJoCe+3665vkuJCWxdJTNsu/CxAAzgCePlm4
0OTsa8DQNj/CCG2eKiFrXp4=
=9GTz
-----END PGP SIGNATURE-----
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: can't initialize a constant using another constant?
2008-02-26 14:32 can't initialize a constant using another constant? Shriramana Sharma
2008-02-27 8:09 ` Kristof Provost
@ 2008-02-27 11:43 ` Glynn Clements
2008-02-27 23:35 ` Shriramana Sharma
1 sibling, 1 reply; 6+ messages in thread
From: Glynn Clements @ 2008-02-27 11:43 UTC (permalink / raw)
To: Shriramana Sharma; +Cc: Linux C Programming List
Shriramana Sharma wrote:
> Quite some time since I wrote. I hope you are all keeping well. :)
>
> A strange thing I ran across this evening. I was not allowed to do:
>
> const double lunarSynodicMonth = 29.53059 ;
> const double daysLunarPhaseAngleTakesPerDegree = lunarSynodicMonth / 360.0 ;
>
> I got the following error:
>
> wdf-karanas.c:17: error: initializer element is not constant
> make: *** [wdf-karanas.o] Error 1
>
> I would be grateful if anyone could shed some light. TIA.
In C, "const" is only relevant to pointer targets. Adding the "const"
modifier to a variable has no effect.
You need to change lunarSynodicMonth to a macro (#define) if you want
to be able to use it in an initialiser.
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: can't initialize a constant using another constant?
2008-02-27 11:43 ` Glynn Clements
@ 2008-02-27 23:35 ` Shriramana Sharma
2008-02-28 9:52 ` Glynn Clements
0 siblings, 1 reply; 6+ messages in thread
From: Shriramana Sharma @ 2008-02-27 23:35 UTC (permalink / raw)
To: Linux C Programming List
Thanks to all those who replied. I am very sorry I did not specify the
compiler version etc. I should have. It's gcc version 4.1.3 20070929
(prerelease) (Ubuntu 4.1.2-16ubuntu2).
Glynn Clements wrote:
> In C, "const" is only relevant to pointer targets. Adding the "const"
> modifier to a variable has no effect.
I don't understand what you mean. I just tried gcc -o foo foo.c on:
# include <stdio.h>
main () {
const int i = 1 ;
i = 2 ;
printf ( "%d\n", i ) ;
}
and I got:
foo.c: In function ‘main’:
foo.c:6: error: assignment of read-only variable ‘i’
So in what sense are you saying adding const to a variable has no effect?
Shriramana Sharma.
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: can't initialize a constant using another constant?
2008-02-27 23:35 ` Shriramana Sharma
@ 2008-02-28 9:52 ` Glynn Clements
0 siblings, 0 replies; 6+ messages in thread
From: Glynn Clements @ 2008-02-28 9:52 UTC (permalink / raw)
To: Shriramana Sharma; +Cc: Linux C Programming List
Shriramana Sharma wrote:
> Thanks to all those who replied. I am very sorry I did not specify the
> compiler version etc. I should have. It's gcc version 4.1.3 20070929
> (prerelease) (Ubuntu 4.1.2-16ubuntu2).
>
> Glynn Clements wrote:
> > In C, "const" is only relevant to pointer targets. Adding the "const"
> > modifier to a variable has no effect.
>
> I don't understand what you mean. I just tried gcc -o foo foo.c on:
>
> # include <stdio.h>
> main () {
> const int i = 1 ;
> i = 2 ;
> printf ( "%d\n", i ) ;
> }
>
> and I got:
>
> foo.c: In function �main�:
> foo.c:6: error: assignment of read-only variable �i�
>
> So in what sense are you saying adding const to a variable has no effect?
Sorry; my mistake.
However, although the compiler will prevent modification to const
objects (and if the compiler didn't, the CPU will, as they are stored
in the .rodata section, which is mapped read-only), they are still
considered variables rather than (compile-time) constants, and can't
be used in initialisers or in array dimensions.
In C++, const-qualified objects are treated as compile-time constants.
--
Glynn Clements <glynn@gclements.plus.com>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2008-02-28 9:52 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-02-26 14:32 can't initialize a constant using another constant? Shriramana Sharma
2008-02-27 8:09 ` Kristof Provost
2008-02-27 8:33 ` Kristof Provost
2008-02-27 11:43 ` Glynn Clements
2008-02-27 23:35 ` Shriramana Sharma
2008-02-28 9:52 ` Glynn Clements
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).