Lab 2 Data Types, Variable, Literals Objective
Lab 2 Data Types, Variable, Literals Objective
Data Types
A data type is an amount of storage allocated to variables. The Data type determines the permissible
operations on variables. A program usually contains different types of data types like integer, float, character
etc. and need to store the values being used in the program. The variables should be declared by specifying
the data type.
Format Specifiers
These are the operators used in printf() function to print the data which is referred by an object or a
variable. The Format specifier is the sequence passed as the formatting string argument.
Following are the most common used format specifiers.
Escape Sequence
The character combinations, which comprise a backslash (\) followed by some character. They are also
called white spaces because they are not displayed on the screen.
Keywords
Keywords are reserved words that have a specific meaning for the compiler. They cannot be used as
identifiers. For Example int, float, main, void etc.
Separators
Separators are symbols that indicate the division and arrangement of groups of code. The structure and
function of code is generally defined by the separators. The separators used in C are as follows:
parentheses ( )
braces { }
brackets [ ]
semicolon ;
Identifiers
Identifiers are names provided by you. These can be assigned to variables, methods, functions etc. to
uniquely identify them to the compiler.
Variables
A variable is a meaningful name of data storage location in computer memory. Or it can be considered as
a box that can hold a single value. When using a variable, refer to memory address of computer.
Naming variables:
The name of variable can be called identifier or variable name. The name can contain letters, digits and the
underscore. The length of name can be up to 247 characters long but 31 characters are usually adequate,
keywords cannot be used as a variable name.
Declaration of variables:
In order to use a variable first declare it by specifying which data type is it to be. The syntax to declare a
new variable is to write the specifier of the desired data type like int, float followed by a valid variable
identifier. For example: int a; float nfc; int a,b,c;
Initializing of variables:
In declaration of a variable, its value is by default undetermined. To store a concrete value to a variable
is called the initialization of a variable.
Example: int a = 10; fload nfc=1.9; char ch = 'NFCIET';
In the example the declaration statement int qty; selects a piece of memory with the name qtycalled
variable which is of integer type.
In the above example qty is a variable, changes its value as the execution of program progresses.
Line 1: The keyword int selected a piece of memory with a name qty to store an integer value
Line 3: An integer value 45 is assigned (stored) to the variable qty using assigning operator (=)
Line 4: The value of qty is incremented by 10 and assigned to the same variable, which will be
overwriting on the previous value.
LITERALS (Constant)
A constant is a value of any type that has the same value and can never change. The constants can be
any of the basic data types i.e. they can be int, float or char. Constants are also known as literals i.e.
constants and literals are one and the same. The Character constants are enclosed between single
quotes.
Example: 1, 2, 3, 4 OR ’a'
Declaration and Initialization of variable with data type
Integer :
int is used to define integer numbers
Example:
{
int a;
a = 5;
}
Float :
float is used to define floating point numbers.
{
float nfc;
nfc = 5.6;
}
Double :
double is used to define BIG floating point numbers. It reserves twice the storage for the number. On
PCs this is likely to be 8 bytes.
{
double Atoms;
Atoms = 2500000;
}
Character :
char defines characters.
{
char Letter;
Letter = 'x';
}
Void :
Void type has no values therefore it cannot be declare as variable as we did in case of integer and float.
It is usually used with function to specify its type.
The Address Operator (&) and scanf
An address of operator returns the memory address of a variable. These addresses returned by the
address-of operator are known as pointers, because they point to the variable in memory. The & operator
tells the scanf ( ) function where to find each variable into which it is to store a new value. If the & were
omitted, the scanf ( ) would know only a variable’s current value, not its location in the memory.
Example:
{
int i;
scanf("%d", &i);
printf("number is: %d", i);
}
#include <stdio.h>
#include <conio.h>
void main( )
{
int a;
clrscr( );
printf("Enter a number\n");
scanf("%d", &a);
printf("You entered %d\n", a);
getch();
}
Type Casting
A type cast is basically a conversion from one type to another. There are two types of type conversion: