473,325 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

const keyword problem.

Hi All,
If I declare an identifier with a const keyword and not initilize
it.Then later on I assign it value of an expression.Is it fine?
I feel yes.But if I modify the value of that constant like say an
operation like,
const int i;
int a,b;
i = a + b;
.....
i++;
Then my compiler(gcc 2.95.1) issues a warning.
If I neglect the warning and ontinue the operation,what will happen?
The value gets modified?
will it result in following run time error?
"segmentation fault"?
-Siliconwafer

Nov 15 '05 #1
1 1138
On 1 Aug 2005 05:02:40 -0700, "siliconwafer" <sp*********@yahoo.com>
wrote in comp.lang.c:
Hi All,
If I declare an identifier with a const keyword and not initilize
it.Then later on I assign it value of an expression.Is it fine?
No, it's not. If the const object has static storage duration, it
will be initialized to 0. If it has automatic storage it will remain
uninitialized forever.
I feel yes.But if I modify the value of that constant like say an
Fortunately for the rest of us, the C language is not defined by what
you feel. It is defined by an International Standard.
operation like,
const int i;
int a,b;
i = a + b;
....
i++;
Then my compiler(gcc 2.95.1) issues a warning.
And it is quite right to do so, since the C language standard requires
the compiler to issue a diagnostic.
If I neglect the warning and ontinue the operation,what will happen?
The C standard doesn't know and doesn't care. Attempting to modify a
const object produces undefined behavior. C places no requirements at
all on what happens when you produce undefined behavior. You have
left the planet C and you are lost in space on your own.
The value gets modified?
will it result in following run time error?
"segmentation fault"?


Reformat your hard disk? Knock tiles off the space shuttle? On the
DeathStation 9000, a mechanical arm comes out and punches the
programmer to discourage him from doing it again.

What happens when you produce undefined behavior is not a language
issue.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pavel | last post by:
I am writing software for an embedded application and here is the question. GCC would emit data declared like const char text = "abc"; to .rodata (i.e. "read only data") section. I can put this...
9
by: siliconwafer | last post by:
Hi All, Where is an identifier with a 'const' keyword given physical space in RAM ? Is it on stack or heap or data segment depending on storage class or is it treated as a part of code ,so kept...
2
by: Lorenzo Castelli | last post by:
This is an old problem of mine. Basically I have an abstract base class which represents a generic iterator over a collection of elements, and various derived classes that implement the...
20
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
17
by: Adrian Hawryluk | last post by:
Hi all, What is everyone's opinion of const inheriting? Should the object that a pointer is pointing at inherit the constness of the pointer? Such as in the case of a class having a pointer...
23
by: Kira Yamato | last post by:
It is erroneous to think that const objects will have constant behaviors too. Consider the following snip of code: class Person { public: Person(); string get_name() const
29
by: Rohit kumar Chandel | last post by:
Hi all, I have a doubt in const keyword. My understanding says that a variable declared const can not be modified by the module it is defined in. Then consider the following code segment:...
6
by: Gestorm | last post by:
Hi everyone, I have a problem. If I declare a struct with a const member, what will happen?For example: if I declared a struct like following: struct{ const int a; char c; }aStruct; then such...
9
by: raylopez99 | last post by:
I'm posting this fragment from another thread to frame the issue clearer. How to pass an object to a function/method call in C# that will guarantee not to change the object?* In C++, as seen...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.