Data Types Revisited Flashcards
Can 16-bit program work on 32-bit processor?
It would if it supports it, act like 16 bit processor. But some like Intel Itanium processors have withdrawn support for 16 bit code.
Storage of an integer?
Out of 2/4 bytes used to store the integer , the highest bit (16th/32nd) is used to store sign of the integer.
1 : -ve
0 : +ve
int data type :
and their diff and uses:
what about constants?
short int :
2 byte long.
shorts are never bigger than ints.
helps speed up, less memory.
Long Int:
4 byte long.
Declaration: long int i; OR long i; short int a; OT short a; longs are never shorter than ints. Gives Big range, but program runs slower.
Sometimes, constant is small enough to be an int, but still we want to give it more storage. For that we add suffix ‘L’ or ‘l’.
Unsigned Integer:
When we are sure an int will be positive in that case:
unsigned int i; unsigned i;
This gives double range, as highest bit (sign bit) is now free to be part of magnitude. 0 - 65535.
there exists:
short unsigned int and long unsigned int
both are signed by default.
Format specifiers for the data type learnt:
%c : char, unsigned char %d : int, short int %u : unsigned int, unsigned short int. %ld : long int %lu : unsigned long int %f : float %lf : double %Lf : long double.
Chars, signed and unsigned:
Signed char, ordinary range: -128 to 127. a char in both occupy 1 byte. Unsigned char ch; from 0 to 255.
Floats and doubles:
Float:
occupies 4 byte, -3.4e38 to 3.4e38
Double:
occupies 8 bytes, -1.7e308 to 1.7e308
Long double:
10 bytes, -1.7e4932 to 1.7e4932
Accuracy of floating-point operation on diff compilers:
22.0/7.0
More accurate by VC++ compiler as compared to TC/TC++ compilers as TCs target compiled code to 8088/8086 (16bit) microprocessors which do not offer floating point support, TCs need a Floating point emulator which has limitations and thus less accurate.
This emulator becomes a part of EXE file, hence its size increase.
Increased size - performance penalty, since bigger code take more time to execute.
how are negative numbers stored?
2’s complement
= 1’s complement + 1
so 1000 0000 = - 128
129 = -127
128 will have nine bit value : 0 1000 0000
If we exceed the range from positive side we end up on the negative side.
Storage Classes In C:
What do they tell us?
there are 4 types:
- Automatic - Register -Static -External
To fully define a var this should also be defined, it does have a default value though.
Compiler assumes it depending on context in which var is used.
It tells us about:
- Where var is stored.
- It’s initial value if not declared.
- Scope of var
- Life of var.
Where are variables value stored:
- Memory
- CPU registers
Automatic Storage Class
And declare it
- Storage : Memory
- Initial value : garbage
- Scope : Local, in the block where declared only.
- Life : Till control remains in the block
auto int i;
Register Storage Class:
- Storage : CPU registers.
- Garbage Initial Value.
- Local block scope.
- life till control in block.
loops using counter vars can be these for fast access.
register int i;
If no space on register available then, its considered as auto by the compiler.
If 16-bit microprocessor then they can’t hold float, double which req 4 bytes n 8 bytes so even if declared as registered compiler treats them as auto.
What is the benefit of storing var on CPU register?
but what hinders it?
We can access it faster.
So if a var is used frequently, its preferred to declare them as register.
Hindrance being, register has limited space not always available.
Static Storage class:
Memory.
Zero default initial value.
Scope is local to the block.
Value persists between different function calls.
static int i;
The initialization of var is done only once, for multiple function calls.
Avoid their use, as they take up memory since their value persists.