o Sign bit( first bit) : 0 represents positive integer. So positive integer range 0 to 3.
Negative numbers using 3 bit:
o Sign bit( first bit) : 1 represents negative integer. So range = -4 to -1
Using 3 bits range = -4 to +3 = -23-1 to 23-1-1 Using 4 bits range = 24-1 to 24-1-1 In general, for n bit = 2n-1 to 2n-1-1
BASIC/SIMPLE/PRIMITIVE DATA TYPES
The data type defines the type of data stored in a memory location. The data type determines how much memory should be allocated for a variable.
Various types of data such as integer constant, floating point constant, character constant etc.
can be stored in memory during execution of a program.
Data types classification:
o Primitive data types or fundamental data types.
o Derived data types: Derived data types are derived from the fundamental data type.
Some of the derived data types are short int, long int, long double etc.
PRIMITIVE DATA TYPES
The data types that can be manipulated by machine instructions are called primitive data types.
Classification:
o Char (character value) o Int (integer value)
o Float (Floating point value)
o Double (Double floating point value) o Void (Valueless)
INT
In addition, there are a number of qualifiers that can be applied to these basic types. short and long apply to integers:
o int a;
o short int sh;
o long int counter;
int will normally be the natural size for a particular machine.
short is often 16 bits long, and int either 16 or 32 bits. Each compiler is free to choose
appropriate sizes for its own hardware, subject only to the the restriction that shorts and ints are at least 16 bits, longs are at least 32 bits, and short is no longer than int, which is no longer than long.
The standard headers <limits.h> and <float.h> contain symbolic constants for all of these sizes, along with other properties of the machine and compiler.
ADVANTAGES:
Since integer constants are exact quantities, they are used for o counting.
o Indexing.
DISADVANTAGES:
o Higher range of numbers cannot be represented.
o The numbers with fractional part cannot be represented and stored.
FLOAT
It is a keyword used to define floating point numbers in C language.
N bit machine Size of float Range of float
16 bit machine 4 bytes 3.4E-38 to 3.4E38
32 bit machine 8 bytes 1.7E-308 to 1.7E308
ADVANTAGES:
o The numbers with fractional part or decimal points can be stored.
o Higher precision and range when compered to integers.
DISADVANTAGES:
o They are not exact quantities and hence not used for counting, indexing.
o Floating point numbers occupying more that 4 bytes cannot be represented.
o Precision of more than 6 digits after the decimal point is not possible.
o Floating point numbers are represented approximately by most computers.
DOUBLE
It is a keyword used to define long floating point numbers in C language. It is treated as long floating point number.
N bit machine Size of double Range of double
16 bit machine 8 bytes 1.7E-308 to 1.7E308
32 bit machine 16 bytes 3.4E-4932 to 1.1E4932
ADVANTAGES:
o Higher precision and range when compared to floating point numbers.
DISADVANTAGES:
o Not used for counting, indexing.
o These numbers are represented approximately by most of the computers.
CHAR
Keyword which is used to define single character or a sequence of characters called string in C language.
This is a group of 8 data bits.
'C' represents either a letter of roman alphabet.
Small integer in the range of 0 through to +255.
To give a named memory area in which a single letter has to be stored
Declaration:
char letter;
ADVANTAGES:
o Used to represent characters or strings.
VOID
Empty data type. It does not occupy any space in memory.
Declare explicitly that a function is not returning a value
Declare explicitly that a function has no parameter
Create generic pointer
'C' also supports several aggregate types including structure ,union ,enumeration and user defined types.
VARIABLES
A variable is a named data storage location in computers memory. By using a variable name we are referring to the data stored in the location. A value that changes during the execution of the program is called variable.
Rules of variables defining in C:
o The variable name is a combination of alphabets, digits, underscore etc.
o The first character in the variable name must be an alphabet.
o A keyword cannot be used as variable name o No comma or blanks are allowed in variable name.
o No special symbols ($, #) other than underscore can be used in variable name.
o Uppercase & lowercase letters are distinct i.e. case sensitive o A variable name can’t start with digit.
o The maximum length of variable name should be 8 characters long in DOS based program &
in ANSI C it supports up to 31 characters long.
A variable should be defined before it is used in the program Example: data-type var-list;
Data type must be valid data type and var-list may be consist of one or more identifier names separated by commas operator.
The defining indicates the compiler about the variable names and types of data. It is a method of informing the compiler to reserve the memory space for the data based on the type of the variables.
Example : int count;
double balance;
float amount;
int, double,and float are keywords and cannot be used as variable names.
Initialising: Values can be assigned to variable using the assignment operator (=).
var-name=expression;
An expression can be a single constant or combination of variables,operators and constants.
Data-type var-name=constant;
Eg: int value=250;
Int a=58,b=90;
char name= “pace”
double amount =76.80
Declaring: the variables can be declared without allocating the memory. Its possible by type declaration.
Type declaration is a method of informing the compiler about the type of data to be stored in a variable. Using type declaration, the compiler knows that the variables are used in the current file but are defined in some other file.
o Syntax:
Extern data_type a1,a2,….an;
Where, extern is a keyword.
A1,a2,…an are variable names.
Data_type is the type of variables such as int, float etc.