June 16th, 2008 Codes 3 Comments

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.

  1.  
  2.  
  3. #include<iostream.h>
  4. # include <stdio.h>
  5. #include<conio.h>
  6.  
  7. void disp_start();
  8. void main();
  9. void disp_end();
  10.  
  11. #pragma startup main 70
  12. #pragma startup disp_start 60
  13. #pragma exit disp_end
  14.  
  15. static int a=1;
  16. void disp_start()
  17. {
  18.         FILE *fp=fopen("nomain.cpp","r");
  19.         char c=‘a’;
  20.         while(c!=EOF)
  21.         {
  22.                 putch(c=fgetc(fp));
  23.         }
  24.         fclose(fp);
  25.  
  26.         cout<<"\n\nStarting first priority function\n";
  27.         cout<<"Value of X is initially "<<a;    a++;
  28.         cout<<"\nEnd of start()….\n";
  29.         cout<<"Value of X now is "<<a;
  30. }
  31. void main()
  32. {
  33.         cout<<"\n\nEntering main()\n";
  34.         cout<<"Value of X is initially "<<a;    a++;
  35.         cout<<"\nEnd of main()….\n";
  36.         cout<<"Value of X now is "<<a;
  37. }
  38. void disp_end()
  39. {
  40.         cout<<"\n\nEntering exit priority function\n";
  41.         cout<<"Value of X is initially "<<a;    a++;
  42.         cout<<"\nEnd of exit()….";
  43.         cout<<"\nValue of X now is "<<a;
  44.    cout<<"\n\n\t\tALL CALLS TRACED…";
  45. }
  46.  
  47.  

To keep in mind ::
1.The #pragma wont’ always change priorities to anything specified. Most of the time it is on availability.
2.The highest priorities of 0, 1 and 2 cannot always be assigned to all functions, and are machine dependent. An attempt to assign these priorities may result in a processor fault in some machines on runtime.
3. Any call to a library function, whose priority runs into the explicitly defined area, will overwrite the explicit priority definition and make space for itself.

3 Responses to “Program without main()”

  • Program without main - CodeCall Programming Forum 16June2008

    [...] proceed and the static variable is updated, you can see the procedure for live. Also read :: Program without main | TECHARRAZ __________________ God is real… unless declared an integermy blog :: [...]

  • binod mainali 6August2008

    the code written over here is giving three errors when i tested it in the bloodshed Dev Cpp that multiple declaration of void main() is it compiler specific one should keep in mind while using #pragma pre processor directive

  • admin 3September2008

    #pragma is not compiler specific, though this code needs some modification for gcc compatibility. Change return of main and use new style headers with namespaces.

Leave a Reply

 

RSS