473,799 Members | 3,111 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

changing const variables

guys,

If I declare a const variable and then try to change it as follows;
const int i=5;
i = 10;

What would be the behaviour? Should compiler give compilation error or
Warning ?

Or, would I get a run time error (some exception as I am trying to
change a
const variable) ?

can compiler put const variables in read only memory ?

I tried this on multiple platforms using different compiler and I am
getting
different results, that is why I am asking this here.

I want to know the correct behaviour as per the standard C.

Again, thanks a lot for any help ...
Feb 22 '08 #1
2 2470
ju**********@ya hoo.co.in wrote:
guys,

If I declare a const variable and then try to change it as follows;
const int i=5;
i = 10;

What would be the behaviour? Should compiler give compilation error or
Warning ?
Haven't you been learning C long enough to know this? You have almost
certainly asked similar questions before and got answered.
Or, would I get a run time error (some exception as I am trying to
change a const variable) ?
C has no exceptions. However a runtime error is possible.
can compiler put const variables in read only memory ?
What does your common sense tell you? What is the reason for the
existence of read-only memory? To store read-only values right? And
const in C indicates read-only values right? Can you connect the dots
now?

But be aware that read-only memory may not be readily available to all C
implementations and some implementations may not take advantage of such
storage, even if they are available.
I tried this on multiple platforms using different compiler and I am
getting different results, that is why I am asking this here.
The const qualifier indicates that the object's value should not be
modified after initialisation. Doing so is undefined behaviour. This is
what the Standard says. The actual behaviour under different systems is
certainly likely to vary from seeming to work to terminating
immediately. Regardless of the program's behaviour *after* a const
object is written to, (which can be virtually anything since it's
undefined by the Standard), the fact remains that *you* , the
programmer, *should* *not* modify a const qualified object.
I want to know the correct behaviour as per the standard C.
It's formally undefined and almost certainly a programming mistake.
Again, thanks a lot for any help ...
How long do you plan to keep asking FAQs?

Feb 22 '08 #2
santosh <sa*********@gm ail.comwrote:
ju**********@ya hoo.co.in wrote:
What would be the behaviour? Should compiler give compilation error or
Warning ?

Haven't you been learning C long enough to know this? You have almost
certainly asked similar questions before and got answered.
He has been asking simple questions for years. He has not been learning.

Richard
Feb 22 '08 #3

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

Similar topics

4
1642
by: Pelle Beckman | last post by:
Hi, I saw this code in an earlier post (not that there's anything wrong with it) #include <iostream> using std::cout; const int hour = 3600; const int min = 60;
16
41778
by: herbertF | last post by:
Hi guys, In a program (not my own) I encountered the declaration of a constant pointer to an array consisting of two other const pointers to arrays. Not quite sure why they do it so complicated, but is it legal? Most compilers accept it, but one doesn't recognize the rhs as a constant. What are the requirements for the rhs in the declaration of a const pointer? Is the following program legal C? int main(int argc, char *argv) {
20
17362
by: Chris | last post by:
Hi, can you define const-functions in C# as in C++ ? for example (C++-code) : int Cube :: GetSide() const { return m_side; }
1
4108
by: Firewalker | last post by:
I am attempting to change the backColor property on the previously instantiated buttons FROM a listbox_doubleClick event. I thought it would be something like this: If Me.Controls.Item(iSeatNumber).BackColor.Equals(Color.White) Then Me.Controls.Item(iSeatNumber).BackColor.Equals(Color.CornflowerBlue) 'End If
6
2682
by: Darian | last post by:
I am wondering how (if it is possible of course) I can change a users screen resolution to a certain size when running my application and then change it back when they exit my application. For instance, if the users screen size is 800x600, I want the resolution to change to 1260x780 when running my program and then change back to 800x600 when exiting my program. Thanks, Darian
1
1732
by: Stabiplan BV | last post by:
Hi, I am trying to find const variables outside a class. Thur far I did not succeed. Without const, the variable is found but with const not. So, how can I get hold on global const variables using reflection? Regards, Rob
41
2561
by: Serve Laurijssen | last post by:
Theres a certain style of coding that uses const as much as possible. Like const int foo(const int a, const int b) { const int retval = pow(a, b); return retval; } one argument to use code like this is that compilers can theoretically
4
3313
by: Rui.Hu719 | last post by:
Hi, All: I read the following passage from a book: "There are three exceptions to the rule that headers should not contain definitions: classes, const objects whose value is known at compile time, and inline functions are all defined in headers. " Can someone explain to me why some of the const objects must be defined in the header file?
23
2338
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
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9546
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10490
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9078
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5590
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.