+ Reply to Thread
Results 1 to 5 of 5

Thread: Program without main

  1. #1
    Chinmoy's Avatar
    Chinmoy is offline Programming Expert
    Join Date
    Feb 2008
    Location
    where heaven meets earth
    Posts
    410
    Rep Power
    18

    Program without main

    Often in classrooms we are taught that the main function is the ‘main’ function in a program! But it actually is not so. That is actually the default behavior of the compiler. The compiler has a predefined set of rules according to which ::

    1. The main() function has a default

    priority of 0, which is the highest.
    2. Priorities from 0 to 63 are reserved for use by C libraries.
    3. Priorities from 64 o 99 are kept as a second reserve if the number of library function in a code exceeds 63.
    4. Any user defined function starts with a priority of 100.
    5. The maximum explicitly defined priority can be 254.

    Thus it is obvious that the main() works with the highest priority at a program startup.
    But the c language provides for a scope of changing the priority such that main() has a priority shift from startup to exit, i.e.; it has a higher priority on exit than on startup. This can exactly be done with the #pragma directive. The #pragma can help us to set priorities of out user defined functions, at startup as well at exit.

    In this code I have implemented the #pragma directive to create a function ‘disp_start()’ which has a higher priority of 60 than main(), whose priority has been lowered from 0 to 70.

    Code:
    #include<iostream .h>
    # include <stdio .h>
    #include<conio .h>
     
    void disp_start();
    void main();
    void disp_end();
     
    #pragma startup main 70
    #pragma startup disp_start 60
    #pragma exit disp_end
     
    static int a=1;
    void disp_start()
    {
            FILE *fp=fopen("nomain.cpp","r");
            char c=‘a’;
            while(c!=EOF)
            {
                    putch(c=fgetc(fp));
            }
            fclose(fp);
     
            cout< <"\n\nStarting first priority function\n";
            cout<<"Value of X is initially "<<a;    a++;
            cout<<"\nEnd of start()….\n";
            cout<<"Value of X now is "<<a;
    }
    void main()
    {
            cout<<"\n\nEntering main()\n";
            cout<<"Value of X is initially "<<a;    a++;
            cout<<"\nEnd of main()….\n";
            cout<<"Value of X now is "<<a;
    }
    void disp_end()
    {
            cout<<"\n\nEntering exit priority function\n";
            cout<<"Value of X is initially "<<a;    a++;
            cout<<"\nEnd of exit()….";
            cout<<"\nValue of X now is "<<a;
       cout<<"\n\n\t\tALL CALLS TRACED…";
    }
    As the calls proceed and the static variable is updated, you can see the procedure for live.

    Last edited by Jordan; 06-16-2008 at 04:49 AM.
    God is real... unless declared an integer

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Program without main

    Excellent tutorial, good information and original. +Rep.

  4. #3
    Chinmoy's Avatar
    Chinmoy is offline Programming Expert
    Join Date
    Feb 2008
    Location
    where heaven meets earth
    Posts
    410
    Rep Power
    18

    Re: Program without main

    so many to take away rep! Only 1 to give back..

    Neways, thanks Jordan. And, it has to be original, I made it a few days back. If exists elsewhere, that is a copy [except my blog ].
    And as i see you edited out the link to my blog..Is it not allowed anymore?cuz i did that a lot of times before! Never got edited!
    Last edited by Chinmoy; 06-17-2008 at 01:19 AM.
    God is real... unless declared an integer

  5. #4
    envoy14 is offline Learning Programmer
    Join Date
    Aug 2008
    Posts
    31
    Rep Power
    0

    Re: Program without main

    thanks!!!

  6. #5
    collee_c is offline Newbie
    Join Date
    Sep 2008
    Posts
    2
    Rep Power
    0

    Re: Program without main

    nice tutorial thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 06-02-2011, 12:09 AM
  2. void main(), int main()
    By jackson6612 in forum C and C++
    Replies: 11
    Last Post: 04-10-2011, 09:17 PM
  3. int main() or void main().!
    By UbuntuX in forum C and C++
    Replies: 16
    Last Post: 08-08-2010, 12:21 PM
  4. use of Clrscr and getch outside the main program
    By akpindian in forum C and C++
    Replies: 1
    Last Post: 05-09-2010, 03:03 PM
  5. clrscr and getch problem outside the main program
    By akpindian in forum C and C++
    Replies: 1
    Last Post: 05-04-2010, 03:08 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts