Showing posts with label cpp tricks. Show all posts
Showing posts with label cpp tricks. Show all posts

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
























  • Thursday, 16 May 2013

    Traversing an Array

    Traversing an array means to access each and very element of array .It can be accessed using for loops.The data stored in array can be travesred by using for loop and the elements can be accessed by incerement the for loop. The loop starts to traverse array from the start of the array and continue to traverse array until the for lopp continues its execution until the for loop ends.UIn the program below the elements are stored in an array and are travesred using for loop . The loop prints all the elements of array using traversing.
     

    Program for traversing an array.

    #include<iostream.h>
    #include<conio.h>
    void main()
    {
    int data[5]={1,2,3,4,5};
    int i;
    clrscr();
    cout<<""Elements of array Data are";
    for(i=0;i<=4;i++)
    {
    cout<<data[i];
    }
    getch();
    }

     

    full details in json
    Proudly Powered by Blogger.
    back to top