Sunday, 18 August 2013

C++ program without main

 Yes, the following 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.

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 be 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 exists 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.

Following is a program to run a c++ program without main() :





Output:




Saturday, 17 August 2013

Primitive Data types

data type informs the compiler what type of data a variable is permitted to store. Data type should also be declared when a variable s declared.

Declaring a data type:     datatypename  variablename

 Java as Java is a strongly typed language The data type indicates what type of values that variable can store and possible operations that can be performed. The language system allocates memory to the variable on the type of data it can hold like an integer or double etc. 
String in Java is not a data type. String is a class from java.lang package. String is not a keyword; Strings are used very often in coding like data types.
Java supports 8 data types that can be divided into 4 broad categories.
All data types are keywords. As a rule, all keywords should be written in lowercase. Java does not support unsigned data types; every data type is implicitly signed (except char). "unsigned" is not a keyword of Java. Note that nulltrue and false are also not keywords.



  • Whole numbers – byteshortintlong
  • Floating-point numbers – floatdouble
  • Character values – char
  • Boolean values – boolean
























  • Friday, 16 August 2013

    Drawing animated circle



    The program is to draw an animated circle in computer graphics . The header file used for using graphics in C code is     #include<graphics.h>  , This  graphics functions are included in this header file. The functions like circle, rectangle, arc, eclipse etc all are included in the graphics header file. It is required to declare this header file in the beginning of the program to use these inbuilt functions. The graphics need to be initialized after the main function is defined .The graphics are initialized using initgraph function.In initgraph function the path of the BGI directory is given .

    Program to draw an animated circle

    #include<iostream.h>
    #include<conio.h>
    #include<graphics.h>
    void main()
    {
    int gd=DETECT,gm;
    int x,y;
    initgraph(&gd,&gm,"c:/tc/bgi");
    setbkcolor(RED);
    x=getmaxx()/2;
    y=getmaxy()/2;

    while(!kbhit())
    {
    for(int i=1;i<=15;i++)
    {
    for(int p=400;p>=1;p--)

    {
    circle(x,y,p);
    }
    setcolor(i+2);
    for(int r=1;r<400;r++)
    {
    circle(x,y,r);
    }
    }
    }
    getch();
    }
    full details in json
    Proudly Powered by Blogger.
    back to top