Friday, December 26, 2008

C Programming Coding Question with solution

Q(1) How many type of data c?
Ans:
In c, there are three types of data.
Q(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

Q(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
Q(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
Q.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.
Q(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).

Q(6) 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:
Q(7) 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.


Q(8) 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 :

(8) 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 :

Q(9) What will be output :

void main()
{
float a= -3.3f;
int i;
unsigned char *p=&a;
for(int i=0;i<4;i++)>

No comments:

Blog List