What is the use of qualifier const

I

istillshine

It seems I never need to use it. It only made a long line longer, and
made you type five more characters. I feel so strange why people are
talking about it.
 
I

Ian Collins

It seems I never need to use it. It only made a long line longer, and
made you type five more characters. I feel so strange why people are
talking about it.

Don't you ever create read only variables or string literals or pass
pointers to read only data to functions?
 
I

istillshine

Don't you ever create read only variables or string literals or pass
pointers to read only data to functions?

I do. But I never used const. I just took care not to modify them. In
fact, I never needed to modify them.

Who will put a qualifier before something and modify this thing
later? And if he dose not modify this thing later, why dose he put a
qualifier before it?

I could not under why people do that. When I saw some old C code, I
found people never used const. Their programs are still robust enough
without const.
 
I

Ian Collins

I do. But I never used const. I just took care not to modify them. In
fact, I never needed to modify them.
Then you enjoy skating on thin ice, or never make mistakes.
Who will put a qualifier before something and modify this thing
later? And if he dose not modify this thing later, why dose he put a
qualifier before it?
So the compiler will tell him if he accidentally does attempt to modify it.
I could not under why people do that.

Because they want to write safe, well behaved programs. They want their
constant data stored somewhere it can't be modified. Many embedded
systems have more FLASH than RAM, so it makes sense to store constant
data in FLASH.
When I saw some old C code, I
found people never used const. Their programs are still robust enough
without const.

That's because cost was introduced later. It wouldn't have been
introduced if there wasn't some thing to fix or a perceived need to
differentiate between constant and volatile data.
 
U

user923005

I do. But I never used const. I just took care not to modify them.  In
fact, I never needed to modify them.

It's a great way to do it. Just like grenades. Don't bother with
putting pins in them, just hold the handles down and they are safe.
Who will put a qualifier before something and modify this thing
later?  

The second programmer, who never saw the qualifier put there.
And if he dose not modify this thing later, why dose he put a
qualifier before it?

To give him an error message to tell him he did something stupid.
I could not under why people do that.  When I saw some old C code, I
found people never used const.  Their programs are still robust enough
without const.

If you substitute the word "fragile" with "robust" then I can agree
with you.
Old code often won't even compile with modern compilers, and the older
it is the less likely it will compile.
If it does succeed in compiling, chances are good it won't run.
If it does run, it is likely to be packed with exploits like gets()
because old time programmers did not have to deal with a hostile
audience.
Old numerical stuff is sometimes villianously bad.
 
I

istillshine

Those old programs were also robust enough without `void'
and without function prototypes, so what's your point?

I think void and function prototypes are useful additions. But for
const, I rarely use it largely because I don't want to type five
extra characters,
'c', 'o', 'n', 's', and 't'. Between pounding five more characters
and taking care not to modify the constant variable, I prefer the
latter.
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top