Note : As you know size of data types is compiler depended in C. Answer of all question is based upon that compilers whose word sixe is two byte. Just to know you, size of data type in 2 bytes compilers is as follows:
char : 1 byte
int : 2 byte
float : 4 byte
double: 8 byte
Please adjust the answers according to size of data types in your compiler.
1. What will be output when you will execute following C code?
#include<stdio.h>
int main()
{
printf("%d\t",sizeof(6.5));
printf("%d\t",sizeof(90000));
printf("%d",sizeof('A'));
return 0
}
Output :
Turbo C++ 3.0 = 8 4 2
Turbo C++ 4.5 = 8 4 2
Linux GCC = 8 4 4
Visual C ++ = 8 4 4
Explanation :
By default data type of numeric constants is :
6.5 = double
90000 = long int
'A' = char
In C size of data type varies from compiler to compiler.
In TURBO C 3.0 (16 bit compilers) size of :
double is 8 byte
long int is 4 byte
character constant is 2 byte (size of char data type is one byte)
In TURBO C $.5 or Linux GCC compilers (32 bit compilers ) size of :
double is 8 byte
long int is 8 byte
character constant is 2 byte
2. What will be output when you will execute following C code ?
#include<stdio.h>
int main()
{
double num = 5.2;
int var = 5;
printf("%d\t",sizeof(!num));
printf("%d\t",sizeof(var = 15/2));
printf("%d",var);
return 0;
}
Output :
Turbo C++ 3.0 = 2 2 5
Turbo C++ 4.5 = 2 2 5
Linux GCC = 4 4 5
Visual C ++ = 4 4 5
Explanation:
sizeof(Expr) operator always returns the integer value which represents the size of the final value of the expression expr.
Consider on the following expression:
!num
=!5.2
=0
0 is int type integer constant and it size is 2 by in TURBO C 3.0 compiler and 4 in the TURBO C 4.5 and Linux GCC compilers.
Consider on the following expression:
var = 15/2
= var = 7
= 7
7 is int type integer constant.
Any expression which is evaluated inside the sizeof operator. So value of variable 'var' will remain 5 in the printf statement.
3. What will be output when you will execute following C code?
#include<stdio.h>
int main()
{
signed x;
unsigned y;
x = 10 +-10u + 10u +-10;
y = x;
if(x==y)
printf("%d %d",x,y);
else if(x!=y)
printf("%u %u",x,y);
return 0;
}
Output :
Turbo C++ 3.0 = 0 0
Turbo C++ 4.5 = 0 0
Linux GCC = 0 0
Visual C ++ = 0 0
Explanation :
Consider on the expression:
x = 10 +- 10u + 10u +- 10;
10 = it is signed integer constant
10u = it is unsigned integer constant
X = it is signed integer variable.
In nay binary operation of dissimilar data type operand always automatically type casted into the operand of higher data type before performing the operation and result will be higher data type.
As we know operators enjoy higher precedence than binary operators. So our expression is:
x = 10 + (-10u) + 10u + (-10);
= 10 + -10 + 10 + (-10);
=0
Note : Signed is higher data type than unsigned int. So, Corresponding signed value of unsigned 10u is +10
4. What will be output when you will execute following C code?
#include<stdio.h>
int main()
{
volatile int a = 11;
printf("%d",a);
return 0;
}
Output : Connot predict
Explanation:
We cannot predict the value of volatile vriavle because it's value can be changed by nay microprocessor interrupt.
0 comments:
Post a Comment