public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* use of const keyword
@ 2014-12-07 14:43 bostjanv
  2014-12-07 14:50 ` Yu Sheng Oh
  0 siblings, 1 reply; 3+ messages in thread
From: bostjanv @ 2014-12-07 14:43 UTC (permalink / raw)
  To: gcc-help

Hello,
I am verifying a simple fact concerning the use of the const keyword.

I wrote the following simple program 


============================

const int a=10;
const int b=a;

int main(int argc, char *argv[]) {
}
============================

I tried it out with gcc, version 4.7.2 on Debian 7, and the result was the
following:

============================
bostjan@wwpecker-5-deb:~/workspace/OB2/src$ cc   -c -o test.o test.c
test.c:3:1: error: initializer element is not constant
b
============================

QUESTION: Is there a way to initialize a const to the value of another
const?
Regards,
bostjanv



--
View this message in context: http://gcc.1065356.n5.nabble.com/use-of-const-keyword-tp1098889.html
Sent from the gcc - Help mailing list archive at Nabble.com.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: use of const keyword
  2014-12-07 14:43 use of const keyword bostjanv
@ 2014-12-07 14:50 ` Yu Sheng Oh
  2014-12-07 14:55   ` Yu Sheng Oh
  0 siblings, 1 reply; 3+ messages in thread
From: Yu Sheng Oh @ 2014-12-07 14:50 UTC (permalink / raw)
  To: bostjanv; +Cc: gcc-help

const int is not a constant in c, although it is in c++.
to initialize a const int, you have to use literal values.

On Sun, Dec 7, 2014 at 10:43 PM, bostjanv <bostjanv@alum.mit.edu> wrote:
> Hello,
> I am verifying a simple fact concerning the use of the const keyword.
>
> I wrote the following simple program
>
>
> ============================
>
> const int a=10;
> const int b=a;
>
> int main(int argc, char *argv[]) {
> }
> ============================
>
> I tried it out with gcc, version 4.7.2 on Debian 7, and the result was the
> following:
>
> ============================
> bostjan@wwpecker-5-deb:~/workspace/OB2/src$ cc   -c -o test.o test.c
> test.c:3:1: error: initializer element is not constant
> b
> ============================
>
> QUESTION: Is there a way to initialize a const to the value of another
> const?
> Regards,
> bostjanv
>
>
>
> --
> View this message in context: http://gcc.1065356.n5.nabble.com/use-of-const-keyword-tp1098889.html
> Sent from the gcc - Help mailing list archive at Nabble.com.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: use of const keyword
  2014-12-07 14:50 ` Yu Sheng Oh
@ 2014-12-07 14:55   ` Yu Sheng Oh
  0 siblings, 0 replies; 3+ messages in thread
From: Yu Sheng Oh @ 2014-12-07 14:55 UTC (permalink / raw)
  To: bostjanv; +Cc: gcc-help

Please refer http://stackoverflow.com/questions/3025050/error-initializer-element-is-not-constant-when-trying-to-initialize-variable-w

"All the expressions in an initializer for an object that has static
storage duration shall be constant expressions or string literals."

On Sun, Dec 7, 2014 at 10:50 PM, Yu Sheng Oh <humbleoh@gmail.com> wrote:
> const int is not a constant in c, although it is in c++.
> to initialize a const int, you have to use literal values.
>
> On Sun, Dec 7, 2014 at 10:43 PM, bostjanv <bostjanv@alum.mit.edu> wrote:
>> Hello,
>> I am verifying a simple fact concerning the use of the const keyword.
>>
>> I wrote the following simple program
>>
>>
>> ============================
>>
>> const int a=10;
>> const int b=a;
>>
>> int main(int argc, char *argv[]) {
>> }
>> ============================
>>
>> I tried it out with gcc, version 4.7.2 on Debian 7, and the result was the
>> following:
>>
>> ============================
>> bostjan@wwpecker-5-deb:~/workspace/OB2/src$ cc   -c -o test.o test.c
>> test.c:3:1: error: initializer element is not constant
>> b
>> ============================
>>
>> QUESTION: Is there a way to initialize a const to the value of another
>> const?
>> Regards,
>> bostjanv
>>
>>
>>
>> --
>> View this message in context: http://gcc.1065356.n5.nabble.com/use-of-const-keyword-tp1098889.html
>> Sent from the gcc - Help mailing list archive at Nabble.com.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-12-07 14:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-12-07 14:43 use of const keyword bostjanv
2014-12-07 14:50 ` Yu Sheng Oh
2014-12-07 14:55   ` Yu Sheng Oh

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 read-only IMAP folder(s) and NNTP newsgroup(s).