Variables in C++

  • Post author:
Download PDF
Advertisement

Variables in C++

A variable provides us with named storage that our programs can manipulate. Each variable in C++ has a specific type, which determines the size and layout of the variable’s memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

Variables in C++

Name of variable: It refers to an identifier that represents a memory location.

Address of variable: It refers to the memory location of the variable.

Contents of variable: It refers to the value stored in memory location referred by variable.

Variables in C++ Declaration

A variable must be defined before you can use it in a program. When you define a variable the type is specified and an appropriate amount of memory reserved. This memory space is addressed by reference to the name of the variable. A simple declaration has the following syntax:

SYNTAX: data_type variable_name;

Data_type: It indicates types of data that can be stored in a variable.

Variable_name: It refers to the memory location of a variable.

Example:

Different types of variables are declared as follows:

      int marks;
      float average;
      char grade; 
      double salary;

 

Rules for declaration Variables

Following are some rules for naming variable in C++ language:

  • Variable may include letters, numbers, and underscores (_).
  • The first character of the variable must be a letter or underscore _. The use of underscores is not recommended. The variable 9minute, #home, and 2kg are invalid.
  • Blank spaces are not allowed in variable names. The variable my var and your car are invalid.
  • Both upper and lower cases are allowed. A user-defined variable is conventionally written in lower c ase. The constants are conventionally written in upper case.
    Advertisement
  • Special symbols cannot be used as the variable names.
  • Reserved words cannot be used as the variable names. the names int, void, and while are invalid variables.
  • A variable can be up to 31 characters long for many compilers. If a variable consists of more than 31 characters, only the first 31 characters will be used. The remaining characters will be ignored.
  • A variable can be declared only for one data type.

The variable must be meaningful and readable. It helps the user understand the purpose of the variable. For example, a variable to store the marks of the student should be capitalized Subjects. If a variable consists of multiple words, the first letter of each word should be capitalized to increases readability. A variable MyCarNo is more readable than mycarno.

Example:

Some example of valid variables:

Subject                        mar_1              _temp              first_name                               Zitoc

Variables in C++ initialization

A variable can be initialized, i.e. value can be assigned to the variable, during its definition. Initialization is achieved by placing the following immediately after the name of
the variable:

  • an equals sign (=) and an initial value for the variable or
  • round brackets containing the value of the variable.

SYNTAX:

type_name variable= value;

type_name It indicates the data type of the variable to be initialized.
variable It is the name of the variable to be initialized.
= It is the assignment operator used to initialize a variable.
value It is the value to initialize a variable.

Example:

      int n= 100;
      int x=50, y = 30;
      char grade = `A`;  
      float average = 50.66;

 

C++ Program Introduction

Advertisement

This Post Has 5 Comments

  1. Carmella Onusko

    some really interesting points you have written.

  2. Jesse Lobaugh

    its fantastic as your other content : D, appreciate it for posting.

  3. Renaldo Tozier

    Hi! I’m at work surfing around your blog from my new iphone! Just wanted to say I love reading your blog and look forward to all your posts! Carry on the great work!

  4. Hello to all, how is everything, I think every one is getting more from this
    site, and your views are nice in support of new people.

  5. 10.0.0.1

    I simply couldn’t depart your site prior to suggesting that
    I extremely loved the usual information a person supply on your visitors?
    Is gonna be again often to investigate cross-check new posts

Comments are closed.