473,385 Members | 1,409 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,385 software developers and data experts.

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 2444
ju**********@yahoo.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*********@gmail.comwrote:
ju**********@yahoo.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
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
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,...
20
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
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...
6
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...
1
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...
41
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...
4
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...
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
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...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.