Monday, August 20, 2007

Interesting C Program -16

Can u write a C program to swap two integers that does not use a temporary variable (or that uses only two variables to be swapped)?


Here goes an implementation:


#include "stdio.h"
int main()
{
int a=1,b=3;
printf("BEFORE SWAPPING : a=%d & b=%d\n",a,b);
a^=b^=a^=b;
printf("AFTER SWAPPING : a=%d & b=%d\n",a,b);
}


The output looks something like this:

BEFORE SWAPPING : a=1 & b = 3
AFTER SWAPPING : a=3 & b = 1

Looks good isn't it?

Sunday, August 19, 2007

Interesting C Program -15

Atlast, Here is my implementation of Pascal Triangle!!!

/*PASCAL TRIANGLE*/
#include"stdio.h"
int factorial(int);
int combination(int,int);
int main()
{
int rows,i,j;
printf("ENTER THE NUMBER OF ROWS:\t");
scanf("%d",&rows);
printf("\n---------------------------------------------------------\
------------------\n");
printf(" PASCAL TRIANGLE WITH %d ROWS \ ",rows);
printf("\n-----------------------------------------------------------\
----------------\n");
for(i=1;i<=rows;i++)
{
for(j=0;j<=i;j++)
{
printf("%d\t",combination(i,j));
}
//printf("\n");
printf("\n---------------------------------------------------------------\
------------\n");
}
}
int factorial(int x)
{
if(x<2)
return 1;
else
{
return x*factorial(x-1);
}
}
int combination(int n,int r)
{
int y;
y=factorial(n)/(factorial(r)*factorial(n-r));
return y;
}


The Output looks like this

ENTER THE NUMBER OF ROWS: 5

---------------------------------------------------------------------------
PASCAL TRIANGLE WITH 5 ROWS
---------------------------------------------------------------------------
1 1
---------------------------------------------------------------------------
1 2 1
---------------------------------------------------------------------------
1 3 3 1
---------------------------------------------------------------------------
1 4 6 4 1
---------------------------------------------------------------------------
1 5 10 10 5 1
---------------------------------------------------------------------------

Thursday, August 2, 2007

Interesting C Program -14

Just see the output of the following C program. The output looks bit weird. Isnt it?


#include "stdio.h"
#define DPRINTF(x) printf("%s = %d\n",#x,x);

int main()
{
int x[20];
int *p,*q;
p=&x[5];
q=&x[9];
DPRINTF(p);
DPRINTF(q);
DPRINTF(q-p);
}



The output is


p = 2293580
q = 2293596
q-p = 4

q-p should be 16. But it gives 4. why?



The explanation goes like this.


Lets take a real life example.

Say you took 9 dozens of apples. You returned 5 dozens of apples. Then how many dozens of apples do you have? 4 dozens isnt it? You will not say 48 apples.

Similar is the case here. x[9] is stored in 2293596 address location and x[5] is stored in 2293580. p and q are integer pointers, each occupying 4 bytes. Now when you display the value of p and q, they will be 2293580 and 2293596 respectively. But if you ask for difference, it will be 4.
Inshort, 9 integer pointers - 5 integer pointers = 4 integer pointers.


Now just try this program:



#include "stdio.h"
#define DPRINTF(x) printf("%s = %d\n",#x,x);

int main()
{
int x[20],y,z;
int *p,*q;
p=&x[5];
q=&x[9];
DPRINTF(p);
DPRINTF(q);
DPRINTF(q-p);
y = p;
z = q;
DPRINTF(y);
DPRINTF(z);
DPRINTF(z-y);
}

The output is like this:

p = 2293580
q = 2293596
q-p = 4
y = 2293580
z = 2293596
z-y = 16


If you see the difference between z and y, you will get 16.

I think now no explanation is required now.

Interesting C Program -13

Hey!!! Now u can change the color of the output window (DOS window). How?

Just try the following program:


#include "stdio.h"
#include "string.h"
#include "stdlib.h"
main()
{
int i=0;
for(i = 0;i<3;i++)
{
char x[] = "color ";
(i==0)?strcat(x,"0F"):((i==1)?strcat(x,"1E"):((i==2)?strcat(x,"2D")\
:strcat(x,"3C")));
system(x);
system("pause");
}
}


Your output window looks colorful right?

Interesting C Program -12

Can you write a simple "Hello World" Program that doesnot use semicolon?
Here is an implementation!!!


#include "stdio.h"
int main()
{
if(printf("Hello World")){}
}

Search Google

Books that I refer to...

  • The Complete Reference C, Fourth Edition
  • The C Programming Language