Sunday 29 August 2010

datatypes Questions on c

(1) How many type of data c?
Ans:
In c, there are three types of data.



(2) What is qualifier or modifier of data type?
Ans:
It qualify the primary data type.There are five group of qualifier in C.

(There is another type qualifier near, far, huge, which qualify only pointer type data type ,interrupt is also qualifier of data)
We can write all five (one for each group) qualifiers for same data type. If we will not write then it will take its default quantifier.We can write quantifier in any order.We cannot write two qualifier of the same group.
E.g short unsigned volatile const int a=5;
It is right. Qualifier of each group is:

E.g static auto int a=5;
It is wrong, because we cannot write two qualifier of the same group.Both static and auto comes under group 1 i.e storage class.
Note. There is not any keyword for the opposite const and volatile .So if does not write qualifier const it means the default qualifier is opposite of const.Same this happen for volatile.Default qualifier of group 3 has also not any keyword .It is between the short and long i.e normal data type.
It not necessary that each primary data type support all five group of quantifier.

g
(3) What is size of each data type?


Ans:
Size of data type depends upon microprocessor.
Size of int is word length
Size of short int can be >= word length/2 but <=word length Size of long int can be <= 2*word length but >=word length
Size of char, float, double, long double is always fix.
Size of enum is size of int.
Turbo C is based on 8086 microprocessor and its word length is two byte

For TURBO C
Data type size (in byte)
char 1
Short int 2
int 2
long int or long 4
enum 2
float 4
double 8
long double 10
(4) What is const and volatile qualifier?
Ans:
Value of any variable can be changed either by program or external device.const. and volatile is not opposite to each other.
Const : when any variable has qualified with const keyword in declaration statement then later it is not possible to assigne any value or modify by the program.But indirectly with the help of pointer its value can be changed.
When any variable is not qualified by const variable then it’s default qualifier is not const .There is not any keyword for not const.It’s meaning is that value of variable can be changed after the declaration statement by the program.
e.g
What will be output?
void main()
{
const int a=5;
a++;


printf(“%d”,a);
}
Ans: compiler error, we cannot modify const variable.

Volatile: when any variable has qualified by volatile keyword in declaration statement then value of variable can be changed by any external device or hardware interrupt.
If any variable has not qualified by volatile keyword in declaration statement, then then compiler will take not volatile as default quantifier .There is not any special keyword for not volatile.Not volatile means when any variable has qualified by volatile keyword in declaration statement then value of variable cannot be changed by any external device or hardware interrupt.
(5) What is meaning of the declaration:
const volatile int a=6;
Ans:
Value of variable cannot be changed by program (due to const) but its value can be changed by external device or hardware interrupt (due to volatile).
(9) How char data type is represented in the memory?
Ans:
char data type may be signed or unsigned .Both has different memory representation.Both are 8 bit data type.
unsigned char:
All 8 bit is used as data bit.
e.g memory representation of unsigned char a= 7;
Binary equivalent of 7 is 111
For 8 bit we will add 5 zero in the left side i.e 00000111
In the memory:




Here MSD is most significant digit and LSD is list significant digit.
signed char:
1 bit: signed bit
7 bit: data bit
Note: In C, negative number is stored in the 2’s complement format.
Signed bit is 0: Number is positive.
Signed bit is 1: Number is negative.

e.g memory representation of char a=7;
Binary equivalent of 7 is 111
For 8 bit we will add 5 zero in the left side i.e 00000111
Memory representation:

e.g memory representation of char a= -7;
Binary equivalent of 7 is 111
For 8 bit we will add 5 zero in the left side i.e 00000111
Since a is negative number so it will store in the memory in the 2’s complement format

Memory representation:

.
(10) What is endianness of processor?
Ans:
If the size of data type is more than one byte then endianness decides the memory representation of data type.
There are two type of endianness.
Little-endian: The processor which follow the following memory representation of data is known as little-endian processor.


First A will fill then B then C then D then E and so on from right to left.
Example of processor: 8085,8086,8088,80286,80386,p1,p2 etc.
Big-endian:
The processor which follow the following memory representation of data is known as big-endian processor.

First A will fill then B then C then D then E from right to left.
Example of processor:
Motorola, IBM PC etc.

No comments:

Post a Comment