I am reading a new book to help teach myself C++ (I always learned from toying around with programs and it's been a long time so I would like to learn the correct way.) Anyway, I'm only on Day 3 of "Sams Teach Yourself C++ in 21 Days" and it's really just all review stuff for me right now. During this chapter they're discussing constants, values, typedef, etc. Let me quote this from the book...
Constants...
Like variables, constants are data storage locations. Unlike variables, and as the name
implies, constants don’t change—they remain constant. You must initialize a constant
when you create it, and you cannot assign a new value later.
C++ has two types of constants: literal and symbolic.
Literal Constants...
A literal constant is a value typed directly into your program wherever it is needed. For
example:
int myAge = 39;
myAge is a variable of type int; 39 is a literal constant. You can’t assign a value to 39,
and its value can’t be changed.
Now this didn't make sense to me because I have worked with "int" before and changed it, so I decided to make a quick little program to test this out. You can see it here...
I simply took an integer and assigned it a value, then incremented it by 1, then changed it to another random value and every time it changed with no problems. No errors, no warnings.. so what exactly does this guy mean when he says "literal constant"
Yea, I have to agree with computergeek. If that's really what he meant and it wasn't a typo, that's not even really necessary to learn in my opinion. All he's saying is 39 will ALWAYS be 39, well yea obviously... lol
I agree it's rather obvious for numeric literals, but I think the author was just trying to teach the terminology. OTOH, people often have difficulties understanding why string literals are constant, and the numeric example helps, IMO.
It's not as obvious as you may think. It may not matter for integer literals, but for string literals the fact they are constant DOES matter.
Anyways, any book that claims to teach you C++ in 21 days is lying right in your face. I am at it since 2 years, with overall 5 years of programming experience, and I literally still learn something new every day.
I don't think that is too much silly. A computer doesn't know 39 or 1, it is just some bits written in the internal memory (in some way, not directly of course), trying to change 39 means trying to change some basic thing in the "CPU memory" ( I do not mean the ROM, maybe more than that!)
I completely understand that learning C++ (or any programming language for that matter) in 21 days is obviously impossible. In fact at the very beginning of this book it states that 21 days is not long enough to teach you everything, or even most, but it will give you a general understanding of C++.
The fact is I looked all over Google (EVERYWHERE) looking for the best books to teach C++ and this one just kept coming up over and over and over again. I'm actually loving it so far, I haven't learned a whole lot because I have coded before and right now it's really just review, but he's great at teaching and this would have saved me a TON of time had I started here rather than just goofing off with pre-made programs to start off.
Actually, usually it's just that - numbers directly written into the executable code. Computers do not make any distinction between a program and data, such distinctions are made by humans because it's convenient.
Hi hanst99, really?
I thought what happens is the value 39 was initiated on some registers by the CPU, then was assigned to the memory myAge requested by the program when it was executed? (const is just for the compiler to check.)
I thought what happens is the value 39 was initiated on some registers by the CPU
CPU registers are used on runtime, and there are only few of them. The program itself is the data saved in the executable files, which consists of executable code as well as binary data used by that code.
Normally you would have something like this (structure wise, of course the words do not appear in the actual executable code):
In most cases, the 39 will be an immediate in a CPU instruction instead of being loaded from the data segment.
So if you make the appropriate page in the text segment writable, you could find its location and change the 39 to something else.
on my system (with inlining turned off). It might or might not get the same result on other unixoids.
Probably the most unportable piece of code I've written in recent history, though.
Don't worry about it, I got my question answered already. Now I have another question, so I'm about to write another topic! ;) I can't wait until I start courses for this, because it's really annoying just reading with no one to directly ask questions to about it.