Skip to main content

Building Blocks in C++

Variables in C++

What are variables?

Think of a variable as a box or a container that can hold different things, like toys. Each box has a label, so you know what's inside. In C++, we use variables to store different kinds of information, like numbers, words, or other stuff.

Variables are like the cookie jars of code - they hold all the sweet data, and if you're not careful, they'll crumble your code to pieces!😄

As in our previous lectures, we learned Literals they are just constant or fixed values, but what if we want to use them later on then there is no way out. So we have to make sure we have stored them somewhere we can access in future.

Storing data allows us to use the same data later in the program. For this, we use something called variables.


How to make a variable?

To create a variable in C++, you need to follow a few steps. Imagine you have a new box to store some numbers.

  1. Pick a name: Choose a name for your box. This is like writing a label on it. Let's say you want to store your age, so you can name your box "age."
  2. Choose a type: Decide what kind of thing you want to put in the box. Is it a whole number (like your age), a decimal number, or a word? In C++, we call this the "data type." For your age, you'd choose an "integer" data type, which is like a whole number box.
  3. Put something in the box: You can put your age, like 10, inside the "age" box.

Syntax :

type VariableName = value;

Here's what it looks like in C++ code:

int age = 10;

This line of code creates a variable named "age," which holds an integer value of 10.


How to assign value to variables?

There are two ways to assign some value to a variable :

1 . Create a variable and assign a value later.

// A variable by the name price is declared
int price;

// Assign the value 100 to the variable price
price = 100;

2 . Create and assign value simultaneously.

/* 
  Variable price is declared and directly 
  the value 100 is assigned to it
*/

int price = 100;

Quiz

Which of the following is the correct value for the marksvariable?

int marks;

Printing Variables

We use the same cout statement to print variables in C++. Let's see an example.

#include <iostream>

using namespace std;

int main(){

  // creating a variable
  int price = 99;

  // printing
  cout << price;


  return 0;
}

Output :

99

Here, a variable, names as price is given a value and then printed. If you are dissatisfied with the result and are anticipating a different outcome, let's understand this with an example.

#include <iostream>

using namespace std;

int main(){

  int price = 99;

  cout << price << endl;

  cout << "price" << endl;

  return 0;
}
Guess the output
99
price

Here ,

cout << price , statement prints the actual value stored in price variable

cout << "price" , prints price as it is because it act's as a string literal.


Quiz

What's the output of this code?

#include <iostream>
using namespace std;

int main() {

    int power_level = 9000;

    cout << "power_level";
    cout << " over ";
    cout << power_level;

    return 0;
}

Practice Problem

Create a program to assign value and print variables, make an int variable salary with a value of 50,000.

Expected Output :

Salary : 50000
Solution Code :
#include <iostream>
using namespace std;

int main(){

  int salary = 50000;

  cout << "Salary" << " " << salary;

  return 0;
}

Up till now, we have only discussed only single type of variables which are integers. In the next lesson, we will explore all other possible different types of variables like float, char, string etc.