Friday, December 24, 2010

C Post

This is my compilation of some C questions I have googled and gathered from the web.



(1) unsigned int x = -1; x >>= 2 .What will the value of X?

Value of X will be --> UINT_MAX ( Maximum int Value in the System)
UINT_MAX is defined in the <limits.h>


Therefore :
 x >>= 2 = Integral part of (UINT_MAX/4 (a>>b = a /2^b))

UINT_MAX is also equal to the 0xFFFFFFFF ( that is size of Intezer on a system*2 times F ) 
For example if the size of Intezer is 4 bytes(32 bit) the value of Max int = (2^32) -1
This solution can also be made to find out the size of an intezer on a machine.But for that you have to store the bigger values like 4294967295((2^32) -1)
Alternate way to find out the Size of Intezer is  to find the difference between the &(x) and &(x++) .


(2) Accessing a function Pointer via Structure - Hidden OOPS ?


#include<stdio.h>
void (*funcPtr)();
typedef struct test
{
int x;
char y;
void (*ptr)();

}tstruct;
void show()
{
printf("Demoi \n");
}
void main()
{
tstruct z;
z.x = 100;
z.y = 900;
z.ptr = show;
z.ptr();


}

This programm shows some hidden feature of how OOPs can be implemented using the C.If we assign x as n boolean flag for accessing the function pointed to by the ptr variable , I am sure we can do some OOP stuff here with this code.

(3) What is the structure of the programme in the memory?



More ...to be contniued..!





No comments:

Post a Comment