Friday, December 4, 2009

c++ program tricks


C Program Without a Main Function


How to write a C program without a main function?.Is it possible to do that.Yes there can be a C program without a main function.Here’s the code of the program without a main function…

#include
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)
int begin()
{
printf(” hello “);
}
Does the above program run without the main function? Yes, the above program runs perfectly fine even without a main function.But how,whats the logic behind it? How can we have a C program working without main ?
Here we are using preprocessor directive #define with arguments to give an impression that the program runs without main.But in reality it runs with a hidden main function.
The ‘##‘ operator is called the token pasting or token merging operator.That is we can merge two or more characters with it.
NOTE: A Preprocessor is program which processess the source code before compilation.
Look at the 2nd line of program-
#define decode(s,t,u,m,p,e,d) m##s##u##t
What is the preprocessor doing here.The macro decode(s,t,u,m,p,e,d) is being expanded as “msut” (The ## operator merges m,s,u & t into msut).The logic is when you pass (s,t,u,m,p,e,d) as argument it merges the 4th,1st,3rd & the 2nd characters(tokens).
Now look at the third line of the program-
#define begin decode(a,n,i,m,a,t,e)
Here the preprocessor replaces the macro “begin” with the expansion decode(a,n,i,m,a,t,e).According to the macro definition in the previous line the argument must de expanded so that the 4th,1st,3rd & the 2nd characters must be merged.In the argument (a,n,i,m,a,t,e) 4th,1st,3rd & the 2nd characters are ‘m’,'a’,'i’ & ‘n’.
So the third line “int begin” is replaced by “int main” by the preprocessor before the program is passed on for the compiler.That’s it…
The bottom line is there can never exist a C program without a main function.Here we are just playing a gimmick that makes us beleive the program runs without main function, but actually there exista a hidden main function in the program.Here we are using the proprocessor directive to intelligently replace the word begin” by “main” .In simple words int begin=int main. 

C Program for Pigeon Breeding Problem


The problem is as follows…
Initially i have a pair of adult pigeons(capable of breeding) which give rise to another young pair every month until it reaches the age of 5 years(60 months).But the young pair starts breeding only when it is 2 months old.Once the adult pigeon pair starts breeding it never stops untils the age of 5 years.Assume the age of initial adult pigeon is 2 months old.This program takes the no. of months as input and will calculate the total no. of pigeons over a given time(as given by the input).This problem is framed, based on my own imagination and i call this problem as PIGEON BREEDING PROBLEM.Heres the code
#include
#include

struct node
{
int age;
struct node *link;
};
typedef struct node* NODE;
NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
{
printf(”Out of memory\n”);
exit(1);
}
return x;
}
void main()
{
unsigned long int count=1;
unsigned int months,i;
NODE first=getnode();/*this is the intial adult pair*/
first->age=2; /*assume the age of initial adult pair as 2*/
first->link=NULL;
printf(”Enter the no. of months\n”);
scanf(”%u”,&months);
for(i=0;iage>=2)&&(temp->age<=60)) { NODE temp1=getnode(); temp->age+=1;
temp1->age=1;
temp1->link=first;
first=temp1;
temp=temp->link;
++count;
}
else
{
temp->age+=1;
temp=temp->link;
}
}
}
printf(”Total no. of pairs after %u months=%ld\n”,months,count);
}

C Program to Get the Current System Time



This program reads the current system time and displays it in the form HH:MM:SS
#include 
#include 

int main(void)
{
struct time t;
gettime(&t);
printf(”The current time is: %2d:%02d:%02d\n”, t.ti_hour, t.ti_min, t.ti_sec); return 0;
}

C Program to Print the Entered Number in Words


The following C program print’s the entered number in words.For example if the number entered is 12345 then the program prints the entered number in words as One Two Three Four Five



#include
void main()
{
int i=0;
unsigned long int digit;
char str[12],ch;
puts(”Enter the number (less than 10 digit)”);
scanf(”%lu”,&digit);
ultoa(digit,str,10); /*converts an unsigned long int to string*/
while(str[i]!=’\0′)
{
ch=str[i];
i++;
switch(ch)
{
case ‘1′:
printf(”ONE “);
break;
case ‘2′:
printf(”TWO “);
break;
case ‘3′:
printf(”THREE “);
break;
case ‘4′:
printf(”FOUR “);
break;
case ‘5′:
printf(”FIVE “);
break;
case ‘6′:
printf(”SIX “);
break;
case ‘7′:
printf(”SEVEN “);
break;
case ‘8′:
printf(”EIGHT “);
break;
case ‘9′:
printf(”NINE “);
break;
case ‘0′:
printf(”ZERO “);
break;
}
}
}

This program will destroy itself upon execution.The program will cause the .exe file to be deleted upon execution.That is this program is capable of destroying itself upon execution.Heres the code


#include
#include
#include
void main()
{
printf(”This program will destroy itself if u press any key!!!\n”);
getch();
remove(_argv[0]);/*array of pointers to command line arguments*/
}

HOW TO COMPILE ?
Load the source code to the compiler and compile(press Alt-F9) and then press F9.This will generate the .exe file in the current directory(Bin directory).Execute this .exe file it will destroy itself upon execution.


How to Compile C Programs


In many of my previous posts especially in the VIRUS CREATION section, I have used C as the programming language. If you’re new to C programming and find it difficult to compile the C source codes then this post is for you. Here is a step-by-step procedure to install Borland C++ compiler 5.5 and compile C programs.


How to install Borland C++ compiler

1. Download Borland C++ compiler 5.5 (for Windows platform) from the following link.
http://www.codegear.com/downloads/free/cppbuilder
2. After you download, run freecommandlinetools.exe. The default installation path would be
C:\Borland\BCC55


How to configure Borland C++ compiler

1. After you install Borland C++ compier, create two new Text Documents
2. Open the first New Text Document.txt file and add the following two lines into it
-I”c:\Borland\Bcc55\include”
-L”c:\Borland\Bcc55\lib”
Save changes and close the file. Now rename the file from New Text Document.txt to bcc32.cfg.
3. Open the second New Text Document (2).txt file and add the following line into it
-L”c:\Borland\Bcc55\lib”
Save changes and close the file. Now rename the file from New Text Document (2).txt to ilink32.cfg.
4. Now copy the two files bcc32.cfg and ilink32.cfgnavigate to C:\Borland\BCC55\Bin and paste them.


How to compile the C source code (.C files)

1. You need to place the .C (example.c) file to be compiled in the following location
C:\Borland\BCC55\Bin
2. Now goto command prompt (Start->Run->type cmd->Enter)
3. Make the following path as the present working directory (use CD command)
C:\Borland\BCC55\Bin
4. To compile the file (example.c) use the following command
bcc32 example.c
5. Now if there exists no error in the source code you’ll get an executable file (example.exe) in the same location (C:\Borland\BCC55\Bin).
6. Now you have successfully compiled the source code into an executable file(.exe file).
NOTE: The above tutorial assumes that you’ve installed the compiler onto the C: drive (by default).

0 comments:

Post a Comment

 
 

Blogger