Вы находитесь на странице: 1из 4

void main( )

{
unsigned int a = 6 ;
int b = -20 ;
( a+b > 6 ) ? puts( "> 6" ) : puts( "<= 6" ) ;
}
Ans: The output of this code would be > 6 . The reason for this is that expressi
ons involving signed and unsigned types have all operands promoted to unsigned t
ypes. Thus the value of expression a + b comes out to be a positive integer grea
ter than 6. Hence the output is > 6.

What is wrong in the following code?


typedef struct
{
int data ;
NODEPTR link ;
} *NODEPTR ;
Ans: A typedef defines a new name for a type, and in simpler cases like the one
shown below we can define a new structure type and a typedef for it at the same
time.
typedefr stuct
{
char name[20] ;
int age ;
} emp ;
However, in the structure NODEPTR there is an error because a typedef declaratio
n cannot be used until it is defined. In the given code fragment the typedef dec
laration is not yet defined at the point where the link field is declared.

--------------------------------------------------------------------------------
C Tip
--------------------------------------------------------------------------------

What is wrong in following code?


#include "stdio.h"
main( )
{
unsigned char ch ;
FILE *fp ;
fp = fopen ( "trial.txt", "r" ) ;
while ( ( ch = getc ( ( fp ) ) != EOF )
printf ( "%c", ch ) ;
fclose ( fp ) ;
}
Ans: EOF has been defined as #define EOF -1 in the file "stdio.h" and an unsigne
d char ranges from 0 to 255 hence when EOF is read from the file it cannot be ac
comodated in ch. Hence, the solution is to declare ch as an int.

--------------------------------------------------------------------------------
C Tip
--------------------------------------------------------------------------------

How many bytes would be allocated by the following code?


#include "alloc.h"
#define MAXROW 3
#define MAXCOL 4
main( )
{
int ( *p )[MAXCOL] ;
p = ( int(*) [MAXCOL] malloc( MAXROW * sizeof ( *p ) ) ;
}
Ans: This would allocate 24 bytes. Here, p is a pointer to a 1-D array of 4 inte
gers. Hence size of *p is 8 bytes. sizeof ( *p ) * MAXROW i.e. sizeof ( *p ) *
3 gives 24. Hence memory allocated would be 24 bytes.
Top

Considering following code snippet, fun( ) of which class would get called and w
hy?
#include <iostream.h>
class base
{
public :
virtual void fun( )
{
cout << "base class's fun..." << endl ;
}
base( )
{
fun( ) ;
}
} ;
class derived : public base
{
public :
derived( )
{
fun( ) ;
}
void fun( )
{
cout << "derived class's fun..." << endl ;
}
} ;
void main( )
{
derived d ;
}
Ans: First, the fun( ) of base class and then fun( ) of derived class would get
called. While constructing the object d of derived class, first the base class c
onstructor would get called. At this moment, the VTABLE would hold the address o
f base class version of virtual function fun( ). As a result fun( ) of base gets
called. The moment control reaches derived class's constructor the VTABLE would
now hold the address of its version of virtual function fun( ) as a result fun(
) of derived class gets called.
Top

--------------------------------------------------------------------------------
C Tip
--------------------------------------------------------------------------------

How do far pointers differ from huge pointers?


Ans: It may happen that far pointers holding different addresses may refer to th
e same memory location. This deduces from the method of obtaining the 20-bit phy
sical address from the segment:offset pair. This is shown below for three differ
ent addresses:
Consider the three addresses: 0x00000120, 0x00100020 and 0x00120000
00000 segment address left shifted by 4 bits
0120 offset address
----------
00120 resultant 20-bit address
00100 segment address left shifted by 4 bits
0020 offset address
----------
00120 resultant 20-bit address
00120 segment address left shifted by 4 bits
0000 offset address
----------
00120 resultant 20-bit address
Unlike far pointers huge pointers are normalized to avoid these problems. A normal
ized pointer is a 32- bit pointer, which has as much of its value in the segment
address as possible. Since a segment can start every 16 bytes, this means that
the offset will only have a value from 0 to F.
To normalize a pointer, first it is converted to its 20-bit address then the lef
t 16 bits are used for the segment address and the right 4 bits for the offset a
ddress. For example, given the pointer 500D:9407, converting it to a 20-bit abso
lute address gives 594D7, which is then normalized to 594D:0007.
huge pointers are always kept normalized. As a result for any given memory addre
ss there is only one possible huge address segment:offset pair for it. This is more
logical than the result obtained while using far pointers.
But then there is a price to be paid for using huge pointers. Huge pointer arith
metic is done with calls to special subroutines. Because of this, huge pointer a
rithmetic is significantly slower than that of far or near pointers.