Lost Password?

  #1 (permalink)  
Old 06-16-2008, 02:26 AM
Chinmoy's Avatar   
Chinmoy Chinmoy is offline
Programming Professional
 
Join Date: Feb 2008
Location: where heaven meets earth
Posts: 301
Rep Power: 6
Chinmoy has a spectacular aura aboutChinmoy has a spectacular aura about
Default 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.

__________________
God is real... unless declared an integer

Last edited by Jordan; 06-16-2008 at 06:49 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 06-16-2008, 05:55 PM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Age: 26
Posts: 5,942
Last Blog:
Performance or Maintai...
Rep Power: 20
Jordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud of
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default Re: Program without main

Excellent tutorial, good information and original. +Rep.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
Don't hesitate to ask any questions that you have! Check out our ASCII Calculator!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-17-2008, 03:12 AM
Chinmoy's Avatar   
Chinmoy Chinmoy is offline
Programming Professional
 
Join Date: Feb 2008
Location: where heaven meets earth
Posts: 301
Rep Power: 6
Chinmoy has a spectacular aura aboutChinmoy has a spectacular aura about
Default 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!
__________________
God is real... unless declared an integer

Last edited by Chinmoy; 06-17-2008 at 03:19 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-07-2008, 08:11 PM
envoy14 envoy14 is offline
Learning Programmer
 
Join Date: Aug 2008
Posts: 31
Rep Power: 1
envoy14 is on a distinguished road
Default Re: Program without main

thanks!!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial: Starting C# with C# 2008 Express Edition Jordan CSharp Tutorials 19 08-08-2008 12:48 PM
Getting Started (C Programming) LogicKills C Tutorials 13 07-11-2008 08:47 PM
Help with Square root and calculator program!!! 123456789asdf C and C++ 10 12-02-2007 04:35 PM


All times are GMT -5. The time now is 12:01 AM.

Contest Stats

John ........ 223.00000
dargueta ........ 168.00000
Xav ........ 164.00000
LogicKills ........ 20.00000
gaylo565 ........ 18.00000
WingedPanther ........ 15.00000
|pH| ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 67%

Ads