You are on page 1of 10

Variable (computer science)

From Wikipedia, the free encyclopedia

This article needs additional citations for verification. Please help improve this
articleby adding citations to reliable sources. Unsourced material may be challenged and
removed. (November 2009)
In computer programming, a variable or scalar is a storage location paired with an
associated symbolic name (an identifier), which contains some known or unknown quantity or
information referred to as a value. The variable name is the usual way toreference the stored value;
this separation of name and content allows the name to be used independently of the exact
information it represents. The identifier in computer source code can be bound to a value during run
time, and the value of the variable may thus change during the course of program execution.
Variables in programming may not directly correspond to the concept of variables in mathematics.
The value of a computing variable is not necessarily part of an equation or formula as in
mathematics. In computing, a variable may be employed in a repetitive process assigned a value
in one place, then used elsewhere, then reassigned a new value and used again in the same way
(see iteration). Variables in computer programming are frequently given long names to make them
relatively descriptive of their use, whereas variables in mathematics often have terse, one- or twocharacter names for brevity in transcription and manipulation.
A variable storage location may be referred by several different identifiers, a situation known
as aliasing. Assigning a value to the variable using one of the identifiers will change the value that
can be accessed through the other identifiers.
Compilers have to replace variables' symbolic names with the actual locations of the data. While a
variable's name, type, and location often remain fixed, the data stored in the location may be
changed during program execution.
Brief history of computer programiing

Code is the foundation of computing. Whether you are using a social


media app on your smartphone or working with a cloud servers API, the
task relies heavily on a programming language.
What you may not know about computer programming is that most
historians recognize Ada Lovelace as the worlds first programmer. She
wrote an algorithm for Charles Babbages Analytical Engine. Although
this computer was never completed, Lovelace noted that Mr. Babbage
believes he can, by his engine, form the product of two numbers, each
containing twenty figures, in three minutes. While that is relatively slow
even by punch-card standards, Babbage and Lovelace were about 60
years ahead of their time. Digital, programmable computers didnt
emerge again until the 1940s.
The Guardian provided this brief overview of Lovelaces work in
December 2012, the 197th anniversary of her birth:

Often described as the world's first computer programmer, Lovelace


showed a keen interest in mathematical studies from an early age and
was taught by her mother, Annabella, who was also a gifted
mathematician.
Her notes include what is recognised as the first algorithm intended to
be processed by a machine, while she also speculated on its future
ability to create graphics and complex music.
Lovelaces contributions to computing marked the beginning of a rich
history in programming. In 1970, Niklaus Wirth created the language
known as Pascal, which is still used to make Skype desktop applications;
in 1983, Bjarne Stroustrup created the object-oriented language C++,
which today powers Googles Chrome web browser, among others; and in
1991, Guido Van Rossum contributed the incredibly useful and powerful
Python language, named for the British comedy group Monty Python. As
a result, Google, Yahoo and Spotify are reaping the benefits.
The infographic below outlines the history of programming languages in
greater detail.
In the C programming language, data types refer to an extensive system used for declaring
variables or functions of different types. The type of a variable determines how much space it
occupies in storage and how the bit pattern stored is interpreted.
The types in C can be classified as follows:
S.N
.

Types and Description

Basic Types:
They are arithmetic types and consists of the two types: (a) integer
types and (b) floating-point types.

Enumerated types:

They are again arithmetic types and they are used to define variables
that can only be assigned certain discrete integer values throughout
the program.

The type void:


The type specifier void indicates that no value is available.

Derived types:
They include (a) Pointer types, (b) Array types, (c) Structure types, (d)
Union types and (e) Function types.

The array types and structure types are referred to collectively as the aggregate types. The type of
a function specifies the type of the function's return value. We will see basic types in the following
section, whereas, other types will be covered in the upcoming chapters.

Integer Types
Following table gives you details about standard integer types with its storage sizes and value
ranges:
Type

Storage
size

Value range

char

1 byte

-128 to 127 or 0 to 255

unsigned
char

1 byte

0 to 255

signed char

1 byte

-128 to 127

int

2 or 4 bytes

-32,768 to 32,767 or -2,147,483,648 to

2,147,483,647

unsigned int

2 or 4 bytes

0 to 65,535 or 0 to 4,294,967,295

short

2 bytes

-32,768 to 32,767

unsigned
short

2 bytes

0 to 65,535

long

4 bytes

-2,147,483,648 to 2,147,483,647

unsigned
long

4 bytes

0 to 4,294,967,295

To get the exact size of a type or a variable on a particular platform, you can use
the sizeof operator. The expressions sizeof(type) yields the storage size of the object or type in
bytes. Following is an example to get the size of int type on any machine:
#include <stdio.h>
#include <limits.h>

int main()
{
printf("Storage size for int : %d \n", sizeof(int));

return 0;
}

When you compile and execute the above program it produces the following result on Linux:
Storage size for int : 4

Floating-Point Types
Following table gives you details about standard floating-point types with storage sizes and value
ranges and their precision:
Type

Storage size

Value range

Precision

float

4 byte

1.2E-38 to 3.4E+38

6 decimal places

double

8 byte

2.3E-308 to 1.7E+308

15 decimal places

long double

10 byte

3.4E-4932 to 1.1E+4932

19 decimal places

The header file float.h defines macros that allow you to use these values and other details about
the binary representation of real numbers in your programs. Following example will print storage
space taken by a float type and its range values:
#include <stdio.h>
#include <float.h>

int main()
{
printf("Storage size for float : %d \n", sizeof(float));
printf("Minimum float positive value: %E\n", FLT_MIN );
printf("Maximum float positive value: %E\n", FLT_MAX );
printf("Precision value: %d\n", FLT_DIG );

return 0;
}

When you compile and execute the above program, it produces the following result on Linux:
Storage size for float : 4

Minimum float positive value: 1.175494E-38


Maximum float positive value: 3.402823E+38
Precision value: 6

The void Type


The void type specifies that no value is available. It is used in three kinds of situations:
S.N
.

Types and Description

Function returns as void


There are various functions in C which do not return value or you can
say they return void. A function with no return value has the return
type as void. For example void exit (int status);

Function arguments as void


There are various functions in C which do not accept any parameter.
A function with no parameter can accept as a void. For example, int
rand(void);

Pointers to void
A pointer of type void * represents the address of an object, but not its
type. For example a memory allocation function void *malloc( size_t
size ); returns a pointer to void which can be casted to any data type.

The void type may not be understood to you at this point, so let us proceed and we will cover these
concepts in the upcoming chapters.

A variable is nothing but a name given to a storage area 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.
The name of a variable can be composed of letters, digits, and the underscore character. It must
begin with either a letter or an underscore. Upper and lowercase letters are distinct because C is
case-sensitive. Based on the basic types explained in previous chapter, there will be the following
basic variable types:
Type

Description

char

Typically a single octet(one byte). This is an integer type.

int

The most natural size of integer for the machine.

float

A single-precision floating point value.

double

A double-precision floating point value.

void

Represents the absence of type.

C programming language also allows to define various other types of variables, which we will cover
in subsequent chapters like Enumeration, Pointer, Array, Structure, Union, etc. For this chapter, let
us study only basic variable types.

5.2. Interpreter and Compiler


Let's introduce at this point some concepts of execution of programs written in high
level programming languages. As we have already seen, the only language that a
computer can understand is the so called machine language. These languages are
composed of a set of basic operations whose execution is implemented in the
hardware of the processor. We have also seen that high level programming languages
provide a machine-independent level of abstraction that is higher than the machine
language. Therefore, they are more adapted to a human-machine interaction. But this
also implies that there is a sort of translator between the high level programming
language and the machine languages. There exists two sorts of translators:

Interpreter
An Interpreter is a program that implements or simulates a virtual machine using the base
set of instructions of a programming language as its machine language.
You can also think of an Interpreter as a program that implements a library containing the
implementation of the basic instruction set of a programming language in machine
language.
An Interpreter reads the statements of a program, analyzes them and then executes them on
the virtual machine by calling the corresponding instructions of the library.
Interactive interpreter session
During an interactive interpreter session the statements are not only read, analyzed and
executed but the result of the evaluation of an expression is alsoprinted. This is also called
a READ - EVAL - PRINT loop.
Important

Pay attention, the READ - EVAL - PRINT loop is only entered in an interactive session.
If you ask the interpreter to execute code in a file, results of expression evaluations are
not printed. You have to do this by yourself.
Compiler
A Compiler is a program that translates code of a programming language in machine code,
also called object code. The object code can be executed directly on the machine where it
was compiled.

Figure 5.1 compares the usage of interpreters and compilers.


Figure 5.1. Comparison of compiled and interpreted code

So using a compiler separates translation and execution of a program. In contrast of


an interpreted program the source code is translated only once.
The object code is machine-dependent meaning that the compiled program can only
be executed on a machine for which it has been compiled, whereas
aninterpreted program is not machine-dependent because the machine-dependent part
is in the interpreter itself.
Figure 5.2 illustrates another concept of program execution that tries to combine the
advantage of more effective execution of compiled code and the advantage of
machine-independence of interpreted code. This concept is used by
the JAVA programming language for example and in a more subtle way by Python.
Figure 5.2. Execution of byte compiled code

In this case the source code is translated by a compiler in a sort of object code, also
called byte code that is then executed by an interpreter implementing avirtual
machine using this byte code. The execution of the byte code is faster than the
interpretation of the source code, because the major part of the analysis and
verification of the source code is done during the compilation step. But the byte
code is still machine-independent because the machine-dependent part is implemented
in the virtual machine. We will see later how this concept is used in Python (Section
14.1).

You might also like