Literal Constants

Apr 6, 2011 at 5:04pm
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...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main()
{
	int testChange = 35;
	cout << testChange << endl;
	testChange++;
	cout << testChange << endl;
	testChange = 400;
	cout << testChange << endl;
	
	return 0;
}


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"
Apr 6, 2011 at 5:07pm
Looks like a typo in the book. It should be:
 
const int myAge = 39;

Apr 6, 2011 at 5:13pm
No, the literal 39 is constant.

1
2
3
int myAge = 39;
//          ^^
//        literal 

You can't change the value of 39. If you increment the variable, it won't change 39's value. It will just have the variable hold the value 40.
Last edited on Apr 6, 2011 at 5:15pm
Apr 6, 2011 at 5:18pm
Well, read it carefully: The author meant "39" is a constant, you can't write 39 = 1 for example.
Apr 6, 2011 at 5:19pm
... Are you serious? Why even bother to teach that? This is crap we learn in the 1st grade.
Apr 6, 2011 at 5:22pm
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
Apr 6, 2011 at 5:28pm
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.
Apr 6, 2011 at 5:29pm
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.
Apr 6, 2011 at 5:30pm
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!)
Apr 6, 2011 at 5:36pm
@Hanst99

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.
Apr 6, 2011 at 5:48pm
in some way, not directly of course

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.
Apr 6, 2011 at 5:59pm
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.)
Apr 6, 2011 at 6:08pm
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):

1
2
3
4
5
6
7
8
9
10
11
//Pseudo assembly
:CodeSegment
mov eax, var1
mov var1, var2
mov var1, eax
quit
:DataSegment
:var1
32
:var2
15


This would be about equivalent to C++:
1
2
3
4
5
int var1 = 32;
int var2 = 15;
int _temp = var1;
var1 = var2;
var2 = temp;
Apr 6, 2011 at 6:25pm
Thanks hanst99, can you tell me more what happens when I write in C++ (continue your code)
var1 = 1;

@Sorry TheNoobie that I drive your topic too far, but I hope it is also interesting to you. By the way I am reading that book in my spare time too.
Last edited on Apr 6, 2011 at 6:25pm
Apr 6, 2011 at 6:36pm
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.

Point at hand:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <sys/mman.h>
using namespace std;

int foo()
{
  return 39;
}

int main()
{
  cout << foo() << endl;

  const int pageSize=4096;
  mprotect(reinterpret_cast<char*>(reinterpret_cast<size_t>(foo)/pageSize*pageSize),pageSize,PROT_READ|PROT_WRITE|PROT_EXEC);
  char* fooFunc=reinterpret_cast<char*>(foo);
  for (int i=0;i<50;i++)
  {
    if (fooFunc[i]==39)
    {
      fooFunc[i]=42;
      break;
    }
  }

  cout << foo() << endl;
}


This prints
39
42

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.
Apr 6, 2011 at 6:48pm
@chucthanh

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.
Apr 6, 2011 at 6:56pm
Hi Athar, it was the same on my computer, 39 and 42. That's really fantastic!
@ TheNoobie: Thanks.
Topic archived. No new replies allowed.