Connect with Facebook Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Tutorials > C Tutorials

C Tutorials All C Tutorials and Code

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-16-2008, 02:26 AM
Chinmoy's Avatar   
Programming Expert
 
Join Date: Feb 2008
Location: where heaven meets earth
Posts: 391
Rep Power: 9
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
  #2 (permalink)  
Old 06-16-2008, 05:55 PM
Jordan's Avatar   
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 18,335
Blog Entries: 90
Rep Power: 20
Jordan is a glorious beacon of lightJordan is a glorious beacon of lightJordan is a glorious beacon of lightJordan is a glorious beacon of lightJordan is a glorious beacon of light
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Default Re: Program without main

Excellent tutorial, good information and original. +Rep.
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   
Programming Expert
 
Join Date: Feb 2008
Location: where heaven meets earth
Posts: 391
Rep Power: 9
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
Learning Programmer
 
Join Date: Aug 2008
Posts: 31
Rep Power: 0
envoy14 is an unknown quantity at this point
Default Re: Program without main

thanks!!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 10-01-2008, 01:45 AM
Newbie
 
Join Date: Oct 2008
Posts: 2
Rep Power: 0
collee_c is an unknown quantity at this point
Default Re: Program without main

nice tutorial 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
Getting Started (C Programming) LogicKills C Tutorials 25 11-10-2008 07:06 AM
Tutorial: Starting C# with C# 2008 Express Edition Jordan CSharp Tutorials 19 08-08-2008 12:48 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 08:21 AM.

Freelance Jobs

XML/XSL: Need code for Book with Chapers using XML
Create an XML file for a book of your creation, and a basic CSS file that will format it to display ...
Earn: $40.00


C++/C: Simple firework cue sequencer
What I require is a rework of a simple cue sequencer. I have a piece of hardware (an Arduino boar...
Earn: $50.00


HTML/XHTML: Menu Rework - ASCIIBin
I'm placing this in the HTML/XHTML section of the Freelance site but you are not limited to HTML. Wh...
Earn: $20.00



CodeCall Goal

Goal #1: 1,000 Blogs
Goal #2: 1,000 Wiki Pages
Goal #3: 300,000 Posts
Goal #4: 20,000 Threads
Done: 30%, 23%, 55%, 75%

Ads