Assigning Values to Variables in C++ Programming

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Variables don't do much good if they can't store actual values. In this lesson, you will learn how to assign values to variables, and how to use typecasting to convert types.

Variables in C++

Recall that a variable is an element that stores a value, and it can change over time. Consider that you are writing a card game in C++ and you need to keep track of the number of decks a user selects. In this game, they can play with 1 to 4 decks. Therefore, you create a variable called decks and declare it in your compiler:

int decks;

So, what do you think the value of decks is right now? Is it 0? Is it NULL?

The answer is that it could be either or both! It depends on the compiler. Some will default to 0 for integer, but others might see the empty declaration and leave it null or empty. That is, no memory has been assigned to the variable yet. After all, you have told C++ that you want some decks of cards, but not how many yet.

Declaring and Assigning Values to Variables

Let's try our code again and this time give it a value. To save space, the most common practice is to declare the variable and give it a value, all on the same line of code. Use the equal sign (=) to set the starting value:

int decks = 1;


Typecasting might sound like something from the movie business, but it's really a way to convert one variable type to another. Whenever one variable is assigned a type other than its original type, it is called a typecast. You can convert an int to a float or a short int to an int. Likewise, you can typecast a float to an int.

There are two methods of typecasting, implicit and explicit. Let's look at these now.


In an implicit type conversion, you let the compiler figure out what to do. That is, you can assign a value to a variable that doesn't match the type and C++ will cast it to that type.

As an example, enter the following code after declaring and assigning the value for the decks variable:

decks = 3.5;
cout << " After type conversion = " << decks << endl;

What is your result? Do you get 3? Why?

The reason you get 3 is that you declared decks as an integer. It can only hold an integer, even if you try to override and set the value as a floating-point number. This is where typecasting can cost you data. What if you really do have 3.5 decks of cards? Because you set the original variable as integer you are locked into an integer value.

The problem with this approach is that there's no indication to you or other programmers of the intent. The compiler is going to perform the typecast to what fits the data. If it thought double was better, it would typecast to double. Further, this action will happen whether it's a good idea or not. If you want to be clear in your code, convey exactly what you want to have happen.


Now, let's talk about explicit casting. This time we are going to tell C++ what we want it to do. To make this more clear, let's look at a different dataset. We'll use days worked and pay rate for a fictional employee that gets paid per day. Here are the two variables:

int daysWorked = 4;
float payRate = 255.36;
float total;

Let's perform a calculation and convert the integer value UP to a float so we can get the right value after calculating:

total = (float)daysWorked * payRate;
cout << total << endl;

You should get 1021.44 as a value, not a rounded integer value. In this case, we told the compiler what to do and this avoided any potential compiler or run-time errors.

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account