3 Replies - 9007 Views - Last Post: 29 June 2014 - 06:06 PM Rate Topic: -----

#1 soundlesskitty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-June 14

What is the difference between literal and symbolic constants?

Posted 29 June 2014 - 05:04 PM

Hi, I'm Phillip, and I've recently begun learning how to code in C++. I went to my local bookstore and picked up a book entitled "Sam's Teach Yourself C++." It has been a good read so far, but when I got to the point where the author tried to define the difference between literal and symbolic constants, I found myself beating my head because I couldn't understand. Here is the exact text from the section:

"A constant, like a variable, is a memory location where a value can be stored. Unlike variables, constants never change in value. You must initialize a constant when it is created. C++ has two types of constants: literal and symbolic.

A literal constant is a value typed directly into your program wherever it is needed. For example, consider the following statement:

 long width = 5 


This statement assigns the integer variable width the value 5. The 5 in the statement is a literal constant. You can't assign a value to 5, and its value can't be changed.

The values true and false, which are stored in bool variables, also are literal constants.

A symbolic constant is a constant represented by a name, just like a variable. The const keyword precedes the type, name, and initialization. Here's a statement that sets the point reward for killing a zombie:

 const int KILL_BONUS = 5000; 


Whenever a zombie is dispatched, the player's score is increased by the reward:

 playerScore = playerScore + KILL_BONUS;


If you decide later to increase the reward to 10,000 points, you can change the constant KILL_BONUS and it will be reflected throughout the program. If you were to use the literal constant 5000 instead, it would be more difficult to find all the places it is used and change the value. This reduces the potential for error."

It goes on further, but that's all I needed to show for the sake of my problem. What I'm having trouble seeing is, what's the difference? I read what he wrote and all, but it just doesn't click in my head for some reason. Here is a program to demonstrate what I'm having trouble conceptualizing.

 #include <iostream>
using namespace std;

int main()
{
  int width = 10, length = 10;
  int area = width * length;
  cout << "Width:  " << width << endl;
  cout << "Length:  " << length << endl;
  cout << "Area:  " << area << endl;
  return 0;
} 


Now, why would it be harder to go in and changed a regularly defined integer than one defined with the 'const' keyword proceeding it? For example, the width and length variables. My confusion comes from the point that they seem to both simply be variables with a value assigned to them. I feel as if the process of having to change a literal constant's value is synonymous to the process of having to change a symbolic constant's. I understand that I am wrong, but I don't know why.

Thanks for everyone's answers in advance. Also, this is still my first day attempting to learn C++, so bear with me on any displays of blatant programming ignorance in the contents of this post.

Is This A Good Question/Topic? 0
  • +

Replies To: What is the difference between literal and symbolic constants?

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 5333
  • View blog
  • Posts: 16,602
  • Joined: 25-December 09

Re: What is the difference between literal and symbolic constants?

Posted 29 June 2014 - 05:18 PM

A const qualified variable can't be changed once it has been defined. A "normal" variable can be modified many times.

const int value = 5;

value = 10; // Error can't modify a const.

int newValue = 10;

newValue = 5; // Okay, not a const.
newValue = newValue + value; // Okay using the constant, not modifying it.



By using meaningful variable names your constant tells you something about the variable, where the constant may not mean something. For example if I used the name Constant_Adjustment instead of value it might impart actual meaning to the calculation.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 soundlesskitty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-June 14

Re: What is the difference between literal and symbolic constants?

Posted 29 June 2014 - 05:25 PM

View Postjimblumberg, on 29 June 2014 - 05:18 PM, said:

A const qualified variable can't be changed once it has been defined. A "normal" variable can be modified many times.

const int value = 5;

value = 10; // Error can't modify a const.

int newValue = 10;

newValue = 5; // Okay, not a const.
newValue = newValue + value; // Okay using the constant, not modifying it.



By using meaningful variable names your constant tells you something about the variable, where the constant may not mean something. For example if I used the name Constant_Adjustment instead of value it might impart actual meaning to the calculation.

Jim


Thanks! So if I understand correctly, I can define something such as "int storageSpace = 2500" and then simply give it a different value later in the program, since it isn't a constant?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon

Reputation: 5333
  • View blog
  • Posts: 16,602
  • Joined: 25-December 09

Re: What is the difference between literal and symbolic constants?

Posted 29 June 2014 - 06:06 PM

Correct.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1