constant datatypes

M

mallikaarjuna24

I am not restricted from doing an assignment operation on a constant
datatype ( say, an int ) but just warned of the consequence. why?
 
H

Harald van =?UTF-8?B?RMSzaw==?=

I am not restricted from doing an assignment operation on a constant
datatype ( say, an int ) but just warned of the consequence. why?

Do you mean that your compiler accepts

int main(void) {
const int i;
i = 0;
}

? If so, that, in my opinion, is either a bug or a pretty horrible
extension. Regardless, though, for the C language, the _only_ case in which
an error is required is if you use the #error directive. In all other
cases, if the code is invalid the way it is here, compilers are allowed to
accept it so long as they give notice of the problem. This is to allow
compilers to provide extensions to standard C.

The compilers I've tested don't accept this code; they give an unconditional
error message. If yours doesn't, and you want to know why, you should
consult the documentation and if you cannot find it, ask on a newsgroup
which deals with your particular implementation. As far as standard C is
concerned, the answer is that the code is invalid, but so long as compilers
complain, they can then do what they want with it.
 
S

santosh

I am not restricted from doing an assignment operation on a constant
datatype ( say, an int ) but just warned of the consequence. why?

You've already posted a question similar to this. Wait a few hours for
responses, since Usenet is slower than web forums.

Coming to your question, int is not a constant data type. It is an integer
type whose specific instances, i.e., objects of the type, can be
*qualified* with the 'const' qualifier, to indicate to the compiler that
the program should not modify the value of the object after it's
initialisation. It's mainly to prevent inadvertent modifications by the
programmer, and to allow the compiler to optionally perform optimisations
on the object, like storing it in read-only memory.

An attempt to modify a const qualified object invokes implementation
dependent behaviour. The standard mandates a compiler to diagnose attempts
to modify a const qualified object after it's initialisation, but places no
other restrictions upon it, with regard to this erroneous construct.

In summary, don't try to pull such apparently clever tricks. It's just
likely to get you into trouble.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top