|Lessons||Lesson 3: Variables and Data Types|
The variable names in C may be started with a character or an underscore ( _ ). But avoid starting a name with underscore ( _ ). C has many libraries which contain variables and function names normally starting with underscore ( _ ). So your variable name starting with underscore ( _ ) may conflict with these variables or function names.
In a program every variable has
The variables having a name, type and size (type and size will be discussed
later) are just empty boxes. They are useless until we put some value
in them. To put some value in these boxes is known as assigning values
to variables. In C language, we use assignment operator for this purpose.
A variable must have a data type associated with it, for example it can have data types like integer, decimal numbers, characters etc. The variable of type Integer stores integer values and a character type variable stores character value. The primary difference between various data types is their size in memory. Different data types have different sizes in memory depending on the machine and compilers. There are very few data types in C language. These data types are reserved words of C language. The reserve words can not be used as a variable name. Let’s take a look into different data types that the C language provides us to deal with whole numbers, real numbers and character data.
The C language provides three data types to handle whole numbers.
The data type int is used to store whole numbers (integers). The integer type has a space of 4 bytes (32 bits for windows operating system) in memory. And it is mentioned as ‘int’ which is a reserved word of C, so we can not use it as a variable name. In programming before using any variable name we have to declare that variable with its data type. If we are using an integer variable named as ‘i’, we have to declare it as
int i ;
The above line is known as declaration statement. The declaration statement int i ; reserves 4 bytes of memory and labels it as ‘i’. This happens at the execution time.
We noted that the integer occupies four bytes in memory. So if we have to store a small integer like 5, 10 or 20, C provides another data type for storing small whole numbers which is called short. The size of short is two bytes and it can store numbers in range of -32768 to 32767. So if we are going to use a variable for which we know that it will not increase from 32767, for example the age of different people, then we use the data type short for age.
short age = 5;
On the other side if we have a very large whole number that cannot be stored in an int then we use the data type long provided by C. So when we are going to deal with very big whole numbers in our program, we use long data type. We use it in program as:
long x = 300500200;
The C language provides two data types to deal with real numbers (numbers
with decimal points e.g. 1.35, 735.251). The real numbers are also known
as floating point numbers.
To store real numbers, float data type is used. The
float data type uses four bytes to store
float f = 325.32501
If we need to store a large real number which cannot be store in four
bytes, then we use double data type. Normally the size
of double is twice the size of float. In program we use it as:
char Data Type
So far we have been looking on data types to store numbers, In programming we do need to store characters like a,b,c etc. For storing the character data C language provides char data type. By using char data type we can store characters in variables. While assigning a character value to a char type variable single quotes are used around the character as ‘a’.
/* This program uses short data
type to store values */
In C language we have the usual arithmetic operators for addition, subtraction,
multiplication and division. C also provides a special arithmetic operator
which is called modulus. All these operators are binary operators which
means they operate on two operands. So we need two values for addition,
subtraction, multiplication, division and modulus.
Precedence of Operators
The arithmetic operators in an expression are evaluated according to
their precedence. The precedence means which operator will be evaluated
first and which will be evaluated after that and so on. In an expression,
the parentheses ( ) are used to force the evaluation order. The operators
in the parentheses ( ) are evaluated first. If there are nested parentheses
then the inner most is evaluated first. The expressions are always evaluated
from left to right. The operators *, / and % have the highest precedence
after parentheses. These operators are evaluated before + and –
NEXT>>>>>Lesson 4. Expressions