C tips and Recursion


typedef unsigned char byte;
typedef char str40[41];
typedef struct {float re, im;} complex;
typedef unsigned char byte;
typedef int (*fncptr)(int);

Creating a until(condition) loop in C as we seen in other languages


#define until(x)   while(!x)

/* on using until(x)  it will convert to  while(!x) */

#define NULL ((void*)0)


/* is the definition of NULL that points to a 0*/

#define EOF (-1)

/* the EOF macro constant */
A randum charector generating function



#include <stdlib.h>

#define randomChar() (rand() % 26 + 'a')
/* generages a randum lower case charecter */




#define FillCharArray(carray, arraySize) \
for(i=0; i < arraySize; i++) \

array[i]=randomChar();
/*A Macro to Print arrays of any primitive types..Macros will not do type checking, the control string for the printf() is given as argument */



#define printArray(array, elemntCount,controlString)
for(i=0; i < elemntCount; i++) \
printf(controlString,array[i]); \
putchar('\n')


/*We coud call this macro as : */


printarray(intArray, 10, "%-6d");


/* function to print bits of an int*/



void bitPrint(int x){
int i;

int n=sizeof(int) * CHAR_BIT; /* limits.h*/

int mask=1 << (n-1);

//putchar('\n');

for(i=1; i <=n;i++){

putchar( ((x & mask) == 0 )? 48 : 49);

x <<=1;

if(i % CHAR_BIT == 0)

   
putchar(' ');

}
/* to pack 4 charecters into an int and to unpack */



int pack(char a,char b,char c,char d){
int p=a;

p=(p << CHAR_BIT) | b;

p=(p << CHAR_BIT) | c;

p=(p << CHAR_BIT) | d;

return p;
}




char unpack(int p,int k){
int n=k*CHAR_BIT;

unsigned mask=255;

mask <<= n;

return ((p & mask ) >> n);

}


Resursive Functions


void printBack(char *str){
if(*str != 0)
printBack(str+1);
putchar(*str);
}



int fibonacci(int n){
if (n <= 1 )
return n;
else
return (fibonacci(n-1)+ fibonacci(n-2));
}