Tuesday, April 18, 2006

Preprocessor Operators

Stringizing Operator:
#define Strings(X) cout<<#x

calling:
Strings( hai );

Replaces as:
"Hai"

output:
Hai

Charizing Operator:
#define Chars(X) #@X
calling:
a = Chars( b );
Replaces as:
'b'

Token-Pasting Operator:
#define tokens(X) cout< a##x
calling:
int ab=10;
tokens( b );
Replaces as:
ab

Can a structure have a fn pointer?

Yes..lookout the following example,


/*
Question> Can a structure have a fn pointer?
*/

typedef struct tagEMP
{
int no1;
int no2;
int ( *FuncPtr )( int, int );
int result;

}stEMP;

static int Add( int a, int b );
static int Sub( int a, int b );

int main( void )
{
stEMP stEmps;

stEmps.no1 = 10;
stEmps.no2 = 5;

stEmps.FuncPtr = &Add;
stEmps.result = stEmps.FuncPtr( stEmps.no1, stEmps.no2 );
clrscr( );
printf( "Result is %d\n", stEmps.result );

return 0;
}

static int Add( int a, int b )
{
return ( a+ b );
}


static int Sub( int a, int b )
{
return ( a - b );
}

Thursday, April 06, 2006

Storage Classes

C has a concept of 'Storage classes' which are used to define the scope (visability) and life time of variables and/or functions.

auto - storage class
auto is the default storage class for local variables.
{
int Count;
auto int Month;
}
The example above defines two variables with the same storage class. auto can only be used within functions, i.e. local variables.

register - Storage Class
register is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and cant have the unary '&' operator applied to it (as it does not have a memory location).

{
register int Miles;
}

Register should only be used for variables that require quick access - such as counters. It should also be noted that defining 'register' goes not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register - depending on hardware and implimentation restrictions.

static - Storage Class
static is the default storage class for global variables. The two variables below (count and road) both have a static storage class.

'static' can also be defined within a function. If this is done, the variable is initalised at compilation time and retains its value between calls. Because it is initialsed at compilation time, the initalistation value must be a constant. This is serious stuff - tread with care.

extern - storage Class
extern defines a global variable that is visable to ALL object modules. When you use 'extern' the variable cannot be initalized as all it does is point the variable name at a storage location that has been previously defined.

Monday, March 27, 2006

Void Pointer

In C, Void pointer( void * ) is an generic data type. There are times when you write a function but do not know the datatype of the returned value. When this is the case, you can use a void pointer.

int func(void *Ptr);
main()
{
char *Str = "abc";
func(Str);
}

int func(void *Ptr)
{
printf("%s\n", Ptr);
}

Note:
You cant do pointer arithmatic on void pointers.

Volatile

The compilation system tries to reduce code size and execution time on all machines, by optimizing code. It is programmer responsibility to inform compilation system that certain code ( or variable ) should not be optimized.

A variable should be declared volatile whenever its value can be changed by something beyond the control of the program in which it appears, such as a concurrently executing thread.

Usage of volatile
1. An object that is a memory-mapped I/O port
2. An object variable that is shared between multiple concurrent processes
3. An object that is modified by an interrupt service routine
4. An automatic object declared in a function that calls jmpfunc and whose
value is-changed between the call to jmpfunc and a corresponding call to
topjmp

Thursday, March 16, 2006

Solve This

if( X )
{
printf("Hello");
}
else
{
printf("World");
}

Question: What should be the value of "X" so that the output is HelloWorld?

Wednesday, March 15, 2006

How to Change 0x1234 to 0x12

Program:
void main( )
{
int a = 0x1234;

clrscr( );
printf( "Value is:%x", a>>8 );
getch( );

}

Answer:
Value is:12

Swapping Two Numbers

Program:
void main( )
{
int a=10, b=20;

a^=b^=a^=b;
clrscr( );
printf( "Value of a %d, Value of b %d", a, b );
getch();
}

Ans: Value of a 20, Value of b 10

Function Pointer

Function Pointers are pointers, i.e. variables, which point to the address of a function. You can use them to replace switch/if-statements, to realize your own late-binding or to implement callbacks.

When you want to call a function Do() at a certain point called label in your program, you just put the call of the function Do() at the point label in your source code. Then you compile your code and every time your program comes up to the point label, your function is called. Everything is ok.

But what can you do, if you don't know at build-time which function has got to be called? What do you do, when you want to decide it at runtime? Maybe you want to use a Callback-Function or you want to select one function out of a pool of possible functions. However you can also solve the latter problem using a switch-statement, where you call the functions just like you want it, in the different branches.

Sizeof in C

Program:
#include
#include
main( )
{
char a[]="hello";
char b[]={ 'h', 'e', 'l','l','o' };

printf( "Size a=%d\t b=%d ", sizeof( a ), sizeof( b ) );
getch( );
}

What would be the output of the above program?

Ans:
Size a=6 b=5
yes, because the array a contains a '\0'( NULL character ) at the end of the string. So it returns 6. But the array b is formed character by character. So there is noway for NULL. SO it returns 5.