Variables in Computer Memory

Variable

A variable is a named memory location or memory cell. It is used to store program's input data and its computational results during execution. The value of a variable may change during the execution of a program. However, the name of a variable cannot be changed.

The Variables are created in RAM. RAM is a temporary memory. That is why data stored in variables is also temporary. The data stored in a variable is automatically removed when the program is ends.

Computer Memory

  • Address of a variable
  • Name of a Variable Value of a variable
  • Variable in memory

Name of a Variable: It is the name of memory which we reserve in RAM.

Value of a variable: It is representing which value we given to a variable.

Address of a variable: It is referring to the memory location of a variable.

Variable Declaration

The process of specifying the variable name and its type is known as variable declaration.

Syntax:

data_type variable_name;

Examples:

  • int Marks;
  • float _average;
  • char grade;
  • double mySalary;

Rules of declaration of a variable

  1. Variables may include Alphabets, Numbers & underscore(_) only.
    Get quality help now
    Writer Lyla
    Writer Lyla
    checked Verified writer

    Proficient in: Computers

    star star star star 5 (876)

    “ Have been using her for a while and please believe when I tell you, she never fail. Thanks Writer Lyla you are indeed awesome ”

    avatar avatar avatar
    +84 relevant experts are online
    Hire writer

  2. 1st character of a variable must be Alphabet or underscore(_).
  3. Blank spaces are not allowed in a variable name.
  4. Both upper and lower alphabets are allowed.
  5. Special characters except underscore(_) are not allowed.
  6. We cannot use Keywords as variables.
  7. A variable name must be less than or equal to 31 characters.
  8. A variable must be declared with one data type only.

Examples:

  • int Room;
  • char myName_is;
  • float _Numbers;

Some examples of declare wrong variables

  • int 9words;
  • float No#;
  • char My name is;
  • int float;

Variable Initializing

The process of assigning a value of a variable at the time of its declaration is known as variable initialization.

Get to Know The Price Estimate For Your Paper
Topic
Number of pages
Email Invalid email

By clicking “Check Writers’ Offers”, you agree to our terms of service and privacy policy. We’ll occasionally send you promo and account related email

"You must agree to out terms of services and privacy policy"
Write my paper

You won’t be charged yet!

Syntax:

data_type variable_name = value;

Examples:

  • int Marks = 75;
  • float _average = 89.34;
  • char grade = A;
  • double mySalary = 49700;

Data Type

The data type defines set of values and the set of operation on those values. The data is process according to its data type and return the output. The data and its type are define before the designing of actual program. A C++ program has many data types. Each data type require different amount of memory. C++ language provides the following data types:

Data Type Purpose

  • Int To store Numeric values
  • Float To store Real values
  • Double To store Very large values
  • Char To store Character values

Integer Data Type

Integer data type is store only numeric values with no decimal and fraction point. It includes both positive and negative values.

Examples:

  • int x = 10;
  • int a = -78;

Types of Integer

C++ provides these types of integer values:

  • Data Type Size in Bytes Description
  • int 2 Range from -32,768 to 32767
  • Short 2 Range from -32,768 to 32767
  • unsigned int 2 Range from 0 to 65,535
  • Long 4 Range from-2,147483648 to 2,147483648
  • unsigned long 4 Range from 0 to 4,294,967,295

Float Data Type

Floating data type is numeric value with decimal and fractional points. It is also known as Real data type.

Examples:

  • float m = 12.93;
  • float b = 89.804;

Types of Float

Data Type Size in Bytes Description

  • Float 4 Range from 3.4x10-38 to 3.4x10+38
  • Double 8 Range from 1.7x10-308 to 1.7x10+308
  • long double 10 Range from 1.7x10-4932 to 1.7x10+4932

Escape Sequence

The backslash is used as a marker character to tell the compiler that the next character has some special meaning. What that next character means is, up to the implementation. For Example, C-style languages use n to mean newline and t to mean tab. The use of the word "escape" really means to temporarily escape out of parsing the text and into another mode where the subsequent character is treated differently.

Types of Escape Sequences:

Below is the list of some usually used escape sequences in C++;

Escape Sequences Description

  • n Newline. Position the screen cursor to the beginning of the next line.
  • t Horizontal tab. Move the screen cursor to the next tab stop.
  • a Alert. Sound the system bell.
  • Backslash. Used to print a backslash character.
  • Single quote. Used to print a single quote character.
  • Double quote. Used to print a double quote character.

Example:

To understand the concept of escape sequence more precisely, consider the following sample program as an example.

  • using namespace std;
  • int main()
  • {
  • cout
Updated: May 19, 2021
Cite this page

Variables in Computer Memory. (2019, Dec 09). Retrieved from https://studymoose.com/variables-in-computer-memory-essay

Variables in Computer Memory essay
Live chat  with support 24/7

👋 Hi! I’m your smart assistant Amy!

Don’t know where to start? Type your requirements and I’ll connect you to an academic expert within 3 minutes.

get help with your assignment