Variables And Data Types in C


Variables And Data Types

A variable is a temporary storage place for keeping an item of information. This
may then be used or altered later in a program following some calculations.
Each variable holds a specific type of information - for example a whole number, a
decimal number, or a character, or a string (of characters).
Common Types
The most common types of variables are:

Variables And Data Types


Fundamental/Primary Data Types:


C has the following simple data types (16-bit implementation):

Variables And Data Types
The syntax of the simple type specifier:

Variables And Data Types

Currently the three char data types are guaranteed to be 1 byte in length, but the other
data types are machine architecture dependent. Unsigned can be used with all char and
int types.


 Identifier names (Declaring Variables):


The C language defines the names that are used to reference variables, functions,
labels, and various other user-defined objects as identifiers.
An identifier can vary from one to several characters. The first character must be a letter
or an underscore with subsequent characters being letters, numbers or under-score.
Example:

Correct: Count, test23, High_balances, _name
In-correct: 1 count, hil there, high..balance

In turbo C, the first 32 characters of an identifier name are significant. If two variables
have first 32 characters in common and differ only on the 33rd, TC will not be able to tell
then apart. In C++ identifiers may be any length.
In C, upper and lower case characters are treated as different and distinct.
Example:

count, Count, COUNT are three separate identifiers.

To declare a variable in C, do:
var_type list variables;
Example:

int i, j, k;
float x, y, z;
char ch;

Variables And Data Types


 Declaring Variables:


Variables are declared in three basic places: inside functions, in the definition of function
parameters and outside of the functions. If they are declared inside functions, they are
called as local variables, if in the definition of functions then formal parameters and out
side of all functions, then global variables.


Global variables:


Global variables are known throughout the program. The variables hold their values
throughout the programs execution. Global variables are created by declaring them
outside of any function.

Global variables are defined above main () in the following way:
short number, sum;
int bignumber,
bigsum; char letter;
main()
{
}
It is also possible to pre-initialise global variables using the = operator for assignment.
Example:
float sum =
0.0; int bigsum
= 0; char letter
=`A'; main()
{
}
This is the same as:
float sum;
int bigsum;
char letter;
main()
{
sum = 0.0;
bigsum = 0;
letter =`A';
}
is more efficient.
C also allows multiple assignment statements using =, for example:
a = b = c = d = 3;
which is the same as, but more efficient than:
a = 3;
b = 3;
c = 3;
d = 3;
This kind of assignment is only possible if all the variable types in the statement are the
same.


Local variables:


Variable that are declared inside a function are called ―local variables‖. These variables
are referred to as ―automatic variables‖. Local variables may be referenced only by
statements that are inside the block in which the variables are declared.

Local variables exist only while the block of code in which they are declared is executing,
i.e. a local variable is created upon entry into its block & destroyed upon exit.
Example:
Consider the following two functions:
void func1 (void)
{
int x;
x = 10;
}
void func2 (void)
{
int x;
x = -199;
}
The integer variable x is declared twice, once in func1 () & once in func2 (). The x in
func1 () has no bearing on or relationship to the x in func2 (). This is because each x is
only known to the code with in the same block as variable declaration.

Custom Search

%d bloggers like this: