Friday, December 26, 2008

Data Type Problem with solution


(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 qulaify the primary data type.There are five group of qualifier in C.
Group Qualifier
1 storage class: auto (default), register, static, extern
2 for signe: signed (default), unsgined.
3 short, long
4 const
5 volatile
(There is another type qualifier near, far, huge, which qualify only pointer type data type)
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:
Group Qualifier Comment
1 auto since here not storage class quatifier has writtent, so compiler has
used its default value.
2 unsigned
3 short
4 const
5 volatile
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.
Data type Qualifier (which has meaning)
char all qualifiers except short, long
int all qualifier
float all qualifiers except short, long, signed, unsigned
double all qualifier except short, signed, unsigned
void not any qualifier

(3) What is size of each data type?
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).
(6) Explain enum data type ?
Ans: enum is keyword with the help of enum we can make large number of constant of type int
Syntax: enum [.]{ [=], …}[]
Note. [ ] indicate that they are optional.
= +1
Default initial value of first constant is zero.
Value of must be within range of integer.
e.g void main()
{
enum colour { green,red=5,blue,white,yellow=10,pink };
printf(“%d %d %d %d %d %d,green,red,blue,white,yellow,pink}
};

Output: 0 5 6 7 8 10 11
(7) What is use of typedef keyword ?
Ans:
typedef keyword assigns the symbol name of data type definition.Where symbol name is valid identifier.
e.g
void main()
{
typedef unsigned long int world;
world a=45;
printf(“%d”,a);
}
Output : 45
typedef never creates a new data type .It only produce a new name of existing data type.

(8) What is constant? How many type of constant in C?
Ans: Constant is fixed value which can not be changed by the program during the execution.
Constants are classified as

We can make any identifier constant by
1. const keyword:
e.g const int total=5;
now total is constant.We cannot change the value of total.
2. define directive :
e.g #define fraction 1.5
Preprocessor are processed before the actual compilation. So it paste will paste the 1.5 in place of fraction in the program before actual compilation.
3. enum keyword :
e.g enum size { a=8,b=7,c,d,e};
now a,b,c,d and e are constant we cannot change the value of a by program.


(9) How char data type is repersented in the memory?
Ans:
char data type may be signed or unsigned .Both has different memory repersentation.Both are 8 bit data type.
unsigned char:
All 8 bit is used as data bit.
e.g memory repersentation 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 repersentation 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 repersentation:

e.g memory repersentation 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 repersentation:
(10) What is endianness of processor?
Ans:
If the size of data type is more than one byte then endianness decides the memory repersentation of data type. There are two type of endianness.

Little-endian: The processor which follow the following memory repersentation 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 repersentation 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.


(11) How int data type is repersented in the memory?
Ans:
int may be signed or unsigned both has different memory repersentation.
1. Memory repersentation of: unsigned int a=7;
It is 16-bit data type and all 16 bit is data bit.
Binary equivalent of 7 is 111
For 16 bit we will add 13 zero in the left side i.e 00000000 00000111
Since Turbo C is based on 8085 microprocessor which follow little-endian.
Here A is 00000111 and B is 00000000
Memory repersentation :

Note: same memory repersentation will be of : usigned short int a=7;
2. Memory repersentation of: unsigned long int a=8888855555;
It is 32-bit data type and all 32 bit is data bit.
Binary equivalent of 888885555 is 110100 11111011 01010001 00110011
For 32 bit we will add 2 zero in the left side i.e 00110100 11111011 01010001 00110011
Here
A is 00110100
B is 11111011
C is 01010001
D is 00110011

Memory repersentation :

3.
(a) Memory repersentation of: int a=7 or signed int a=7;
It is 16 bit data type .
15 bit: data bit
1 bit: signed bit
Binary equivalent of 7 is 111
For 16 bit we will add 13 zero in the left side i.e 00000000 00000111
Here
A is 00000111
B is 00000000
Memory repersentation :

Note: same memory repersentation will be of: short int a=7 or signed short int a=7;


(b) Memory repersentation of : int a= -7 or signed int a= -7;
It is 16 bit data type .
Binary equivalent of 7 is 111
For 16 bit we will add 13 zero in the left side i.e 00000000 00000111
Since a is negative number so it will first convert in the 2’s comlement format before stored in the memory.

Memory repersentation :

Note: same memory repersentation will be of : short int a=-7 or signed short int a=-7;

(4)
(a) Memory repersentation of : long int a=8888855555 or signed long int a=8888855555;
It is 32 bit data type .
31 bit : data bit
1 bit : signed bit
Binary equivalent of 8888855555 is 110100 11111011 01010001 00110011
For 16 bit we will add 2 zero in the left side i.e 00110100 11111011 01010001 00110011
Here
A is 00110011
B is 01010001
C is 11111011
D is 00110100
Memory repersentation :

(b) Memory repersentation of : int a= -8888855555 or signed int a= -8888855555;
It is 32 bit data type .
Bit no 31 is signed bit.
Binary equivalent of 8888855555 is 110100 11111011 01010001 00110011
For 32 bit we will add 2 zero in the left side i.e 00110100 11111011 01010001 00110011

Since a is negative number so it will first convert in the 2’s comlement format before stored in the memory.
Memory repersentation :

(12) How float,double data type is repersented in the memory ?
Ans:
Float : It is 32 bit data type.
23 bit : for mantissa
8 bit : for exponent ( including one signed bit of exponent )
1 bit : for signed bit of mantissa

e.g Memory repersentation of float a = -3.3f;
step1 : convert the number (3.3) into binary form
binary value of 3.3 is 11.0100110011001100110011001100110011…
step2 : convert the binary number in the scientific form
11.0100110011001100110011001100110011… = 1.10100110011001100110011001100110011…*10^1
Step4 : find exponent and mantissa and signed bit
Mantissa = .1010011 00110011 001100110 (only first 23 bit )
Exponent= 1
Signed bit =1 ( Since a is negative number )
Step5 : stotre 1 in the signed bit (green colour in the figure )
Step 6: Add 127 in the exponent and convert in the binary number form ( since in 7 bit of exponent except signed bit maximum possible number is 1111111 )
Exponent= 127+1=128
Binary form of 128 is 1000000 0
Store first bit i.e 0 at the 0 th position of exponent. (in figure red colour zero)
Store rest 7 bit at 1 to 7 bit of exponent from right to left (in figure red color 1 to 7)
Step 7: store the 23 bit mantissa at 1 to 23 bit position in as shown in figure .Store 1st bit of mantissa (from right to left ) i.e 1 at the position 1 of mantissa as in figure ,2nd bit of mantissa i.e 0 at the position 2 of mantissa as in figure and so on.
In the memory -3.3 is repersente as :

double:
It is 64 bit data type.
52 bit : for mantissa
11 bit : for exponent (including one signed bit of exponent )
1 bit : for signed bit of mantissa

e.g Memory repersentation of double a = -3.3 ;
step1 : convert the number (3.3) into binary form
binary value of 3.3 is 11.0100110011001100110011001100110011…
step2 : convert the binary number in the scientific form
11.0100110011001100110011001100110011… = 1.10100110011001100110011001100110011…*10^1
Step4 : find exponent and mantissa and signed bit
Mantissa = .1010 01100110 01100110 01100110 01100110 01100110 01100110 (only first 52 bit )
Exponent= 1
Signed bit =1 ( Since a is negative number )
Step5 : stotre 1 in the signed bit (green colour in the figure )
Step 6: Add 1023 in the exponent and convert in the binary number form ( since in 10 bit of exponent except signed bit maximum possible number is 1111111111 )
Exponent= 1023+1=1024
Binary form of 1024 is 10000000000
Store first 4 bit i.e 0000 at the 0 to 3 position of exponent. (in figure blue colour digit)
Store rest 7 bit at 4 to 10 bit of exponent from right to left (in figure blue color 4 to 10)
Step 7: store the 52 bit mantissa at 1 to 52 bit position in as shown in figure .Store 1st bit of mantissa (from right to left ) i.e 1 at the position 1 of mantissa as in figure ,2nd bit of mantissa i.e 0 at the position 2 of mantissa as in figure and so on.
In the memory -3.3 is repersente as :

(13) What will be output :

void main()
{
float a= -3.3f;
int i;
unsigned char *p=&a;
for(int i=0;i<4;i++)
{
printf(“%d “,*p);
p++;
}
}
Ans :
Output : 52 52 83 192
Explanation :
p is char pointer which is holding the intial address variable a. Let p store address 500 .
memory repersentation of -3.3f is
first output :
*p mean content at the location 500 which is 00110011 .Its decimal value is 52
Second output:
P+1 mean next memory location i.e 501 (since p is char pointer which is 8 bit data type so it will incrses only 8 bit)
*(p+1) mean contentt at the location 501 which is 00110011 .Its decimal value is 52
Third output:
P+2 mean next memory location i.e 502
*(p+2) mean content at the location 502 which is 01010011 .Its decimal value is 83
Fourth output:
P+3 mean next memory location i.e 503
*(p+3) mean content at the location 503 which is 11000000 .Its decimal value is 192


(14) what is meaning of cyclic nature of the data type ?
Ans:
In the data type char ,int,long int if we assign the value beyond range of of data type instead of giving compiler error it repeat the same value in cyclic order.
unsigned char : Range of unsigned char is 0 to 255 .

If we will assign a value greater than 255 then value of variable will be changed to a value if we will move clockwise direction as shown in the figure according to number.If number is less than 0 then move anti clockwise direction.
If number is X where X is greater than 255 then
New value = X % 256
If number is Y where Y is less than 0 then
New value = 256 – ( Y% 256 )
signed char : Range of unsigned char is -128 to 127 .

If we will assign a value greater than 127 then value of variable will be changed to a value if we will move clockwise direction as shown in the figure according to number. If number is less than -128 then move anti clockwise direction.
If number is X where X is greater than 127 then
p = X % 256
if p <=127
new value = p
else
new value = p - 256
If number is Y where Y is less than -128 then
p = Y % 256
If p <= 127
New value = -p
else
New value = 256 -p
unsigned int : Range of unsigned int is 0 to 653535 .

If we will assign a value greater than 65535 then value of variable will be changed to a value if we will move clockwise direction as shown in the figure according to number.If number is less than 0 then move anti clockwise direction.
If number is X where X is greater than 65535 then
New value = X % 65536
If number is Y where Y is less than 0 then
New value = 65536– ( Y% 65536)
signed int : Range of unsigned int is -32768 to 32767 .

If we will assign a value greater than 32767 then value of variable will be changed to a value if we will move clockwise direction as shown in the figure according to number. If number is less than -32768 then move anti clockwise direction.
If number is X where X is greater than 32767 then
p = X % 65536
if p <=32767
new value = p
else
new value = p - 65536
If number is Y where Y is less than -32768 then
p = Y % 65536
If p <= 32767
New value = -p
else
New value = 65536 -p
Same thing is occured in case of signed long int and unsigned long int.

No comments:

Blog List