What are Variables in Programming?

Last updated on July 13th, 2020 at 01:30 pm.

Variables in programming

What are Variables in programming ?

A variable is a reference to some information. This could be information you may need to use later on in your program .

For instance, assume you want to store two values 1001 and 12020 in your program so that later on you may use these values. The way that you will achieve this is by using variables.

The steps involved in storing and later using these values include:

  • Create the variables with appropriate identifiers (names).
  • Store your values of 1001 and 12020  in those two variables you’ve created . This is called assigning a value to a variable.
  • Retrieve and use the stored values from the variables.
  • Note that with variables you can change the values they store. For instance if you no longer need 1001 and 12020, you will just assign new values to your stored variable names.

Unlike constants in most programming languages, variables are changeable values.

A variable must have an identifier. Some programming languages are statically typed while others are dynamically typed. This is in reference to the data type the programming language can hold and execute in the variable once it is declared.

Statically typed

In strict programming languages such as C++ and Java, you must declare your variables before you can use them.In these programming languages once you declare a variable you cannot change the data type it can hold.  For instance once you declare a variable as an integer, it can not be used to hold a string or a character because an integer is a number value. This is called static typing . These are statically typed programming languages.

Examples of statically typed programming languages:  Java, C, C++, Haskell, Scala, Kotlin , COBOL,Ada, Rust, Pascal, C# etc .

Dynamically Typed programming languages

In dynamic typing , data types of your variables can be changed at run time. If you declare a variable as a number. Later on you could store a different data type in that variable and your program would still execute without any run time errors.

Certain programs such as Javascript, allows you to use a variable even before it has been declared. However in ecmascript 2015, there are limitations to this depending on the keyword definitions you use for defining your variables,

Examples of dynamically typed programming languages:  Ruby, Javascript, Python, PHP, Clojure, erlang ,Lisp, Perl etc .

Examples of Variable declarations

Variable declaration in C

The following are some of the Variable data types in C . You need to use the correct one to declare your variable .For example if you want to store absolute negative or positive digits with no decimals you may  use int .

TypeDescription
charTypically a single octet(one byte).
intInteger value
floatA single-precision floating point value.
doubleA double-precision floating point value.
voidRepresents the absence of type.

How to declare variables in C – The syntax

data-type variable-identifier;

eg :

int tax;

If you want to declare a list of variables :

data-type variable-identifier0,variable-identifier1,variable-identifier2;
float price, profit,cost;
Initializing a variable in C :

Initializing a variable means , giving it an initial value when it is declared.

int tax = 16;

float price=199.99, profit= 57.2, cost;

Variable declaration in C++

Here is the complete list of fundamental types in C++:

GroupType names*Notes on size / precision
Character typescharExactly one byte in size. At least 8 bits.
char16_tNot smaller than char. At least 16 bits.
char32_tNot smaller than char16_t. At least 32 bits.
wchar_tCan represent the largest supported character set.
Integer types (signed)signed charSame size as char. At least 8 bits.
signed short intNot smaller than char. At least 16 bits.
signed intNot smaller than short. At least 16 bits.
signed long intNot smaller than int. At least 32 bits.
signed long long intNot smaller than long. At least 64 bits.
Integer types (unsigned)unsigned char(same size as their signed counterparts)
unsigned short int
unsigned int
unsigned long int
unsigned long long int
Floating-point typesfloat 
doublePrecision not less than float
long doublePrecision not less than double
Boolean typebool 
Void typevoidno storage
Null pointerdecltype(nullptr) 

Source :  http://www.cplusplus.com/doc/tutorial/variables/

How to declare variables in C++ – The syntax

C++ is a direct child of the C programming language. This means that variable declaration in C++ is just like it is in C.

data-type variable-identifier;

eg :

int tax;

If you want to declare a list of variables :

data-type variable-identifier0,variable-identifier1,variable-identifier2;
float price, profit,cost;
Initializing a variable in C :

Initializing a variable means , giving it an initial value when it is declared.

int tax = 16;

float price=199.99, profit= 57.2, cost;

Example code in CPP: 

CPP code to calculate Gross profit per unit

// operating with variables

#include <iostream>
using namespace std;

int main ()
{
 // declaring variables:
 float cost, sellingPrice, profit;


// process and initialization:
 cost = 55.50;
 sellingPrice = 119.99;
 profit = sellingPrice - cost ;

// print out the result:
 cout << "The profit is : " << profit;

// end program:
 return 0;
}

To run the above code, copy it and paste it here : http://cpp.sh

Read More about CPP variables .

Variable declaration in C#

Things to note :
– variables can be declared with or without modifiers. A modifier defines the scope of a variable, i.e. is it a public variable or a private variable. You will use these access modifiers to secure your variables appropriately.
– Variables defined without a modifier are only available within the methods in which they are defined. (Instance variables)

How to declare variables in C# – The syntax

Variable with a modifier

modifier dataType variableIdentifier;

example:

/*...public variable declaration...*/
 private float cost, sellingPrice;

/*...private variable declaration...*/
 public float profit;

Assign initial value to variable

modifier dataType variableIdentifier = value;

Example:

/*...public variable declaration...*/
 private float cost = 99.99, sellingPrice = 199.90;

Variable without a modifier

type variableName = value;  // Usable and accessible within a method / object .

Example :

int age = 5;

Read more about types and variables .

Variable declaration in java

The following are acceptable and valid Java variables declaration

int x, y, z;         // Declares three integers
int x = 10, y = 10;  // Example of variable initialization in Java
byte B = 22;         // initializes a byte type variable B.
double pi = 3.14159; // declares and assigns a value of PI.
char c = 'c';        // the char variable a is initialized with value 'c'

Variable declaration in Python

The following is valid code with variables in python

sales = 235 # An integer assignment
sellingPrice = 99.90 # A floating point variable
product = "Microphone" # A string variable assignment
revenue = sellingPrice * sales  # calculates revenue from the given variables

# The Output via print

print sales # outputs 235
print revenue # prints total revenue
print product # Outputs Microphone


You can run the above code in ideone

Variable declaration in Javascript

8 JS Variables - JavaScript Tutorial 2018

Variable declaration in PHP

Points to note about PHP variables

  • Variables are denoted with a leading dollar sign ($)  e.g. $profit, $loss , $sellingPrice.
  • The variable value is its most recent assigned value.
  • Variables are assigned with the = operator, i.e.  $profit = 20; .
  • Variables can be declared before assignment or not.
  • Variables in PHP are dynamic. refer up there for dynamically typed programming languages.

The following code is taken from the php manual here . It shows a sample of variable code in PHP

<?php
class foo {
    var $bar = 'I am bar.';
    var $arr = array('I am A.', 'I am B.', 'I am C.');
    var $r   = 'I am r.';
}

$foo = new foo();
$bar = 'bar';
$baz = array('foo', 'bar', 'baz', 'quux');
echo $foo->$bar . "\n";
echo $foo->{$baz[1]} . "\n";

$start = 'b';
$end   = 'ar';
echo $foo->{$start . $end} . "\n";

$arr = 'arr';
echo $foo->{$arr[1]} . "\n";

?>

To learn more about Programming, click the button below.

Comment Here

Need WordPress help? Linux Server help? Talk to us.

  • We are your own WordPress customer service.
  • We set up Linux servers and install or migrate WordPress. Learn more here.
  • We support WooCommerce too.
  • Check out our WordPress customer support plans here or contact us below .

If you have any questions regarding WordPress support, Linux server support or any of our services, feel free to reach out or read more on our services page.

Join this free course:

How to host multiple WordPress
websites on a VPS

Close me!