CoolInterview.com - World's Largest Collection of Interview Questions
Start Your Own website Now
Sponsored Links

Interview Questions
Our Services

Get 9,000 Interview Questions & Answers in an eBook.


  • 9500+ Pages
  • 9000 Question & Answers
  • All Tech. Categories
  • 14 MB Content

    Get it now !!



    Send your Resume to 6000 Companies


  • INTERVIEW QUESTIONS C C BASICS DETAILS
    Question :
    Can you write a c program without using main function?

    Posted by: mohammed nawaz on 12/19/2008

    Contact mohammed nawaz Contact mohammed nawaz
    Category C Basics Interview Questions
    Rating (0.4) By 1209 users
    Added on 12/19/2008
    Views 2219
    Rate it!
    Answers:

    we can write the programme without main but it can't run becuase linking is from main().



    Posted by: Ameen    

    Contact Ameen Contact Ameen

    yes,we can write c program without using main.



    Posted by: c.karuppusamy    

    Contact c.karuppusamy Contact c.karuppusamy

    WE CAN WRITE THE PROGRAM WITHOUT MAIN.BUT,IT WILL NOT RUN.BECAUSE,THE COMPILER PERFORMS THE TASK AFTER SEEING THE MAIN FUNCTION ONLY.OTHERWISE,IT SHOWS ERROR.



    Posted by: k.sangeetha    

    Contact k.sangeetha Contact k.sangeetha

    yes, we can write a c program without using main function. but it'll not be executed as the execution starts from the main function only.



    Posted by: rashmi    

    Contact rashmi Contact rashmi

    basically,i thought not to post uo solution here but i have seen the same query in many web sites and no proper solution is found
    so IM posting here the solution for this tragedic question
    1)its true,we can't write a C program without main function
    2)IN THIS WORLD NOTHING IS IMPOSSIBLE HEHEHEHE.........
    solution:
    #include<stdio.h>

    #define decode(s,t,u,m,p,e,d) m##s##u##t
    #define begin decode(a,n,i,m,a,t,e)

    int begin()
    {
    printf(? hello ?);
    }
    i listened people who said program will not get executed,DAMN stop such things.if you don't know plz shut your mouths and don't mislead readres plzzzzzzz
    the above code works perfectly and if any1 need the explanation for that plz refer www.gohacking.com and in the right most side (below) see C SOURCE CODES coloumn and then read it there clearly.even after that if you have any doubts feel free to contact me at mani.konakandla@yahoo.com



    Posted by: venkat    

    Contact venkat Contact venkat

    otherwise try this one...

    _start()
    {
    _exit(function_name())
    }
    void funtion_name()
    {
    printf("Without main
    ");
    }


    and compile this code like this..
    $cc -nostartfile source_file.c



    Posted by: Rao Sahab    

    Contact Rao Sahab Contact Rao Sahab

    yeah ofcourse we can write a c prog without using main().,. but it leads to error in it's execution stage



    Posted by: lol    

    Contact lol Contact lol

    a program cannot be executed witout a main()function.Since te compiler starts compiling te program after te main()



    Posted by: sukanya    

    Contact sukanya Contact sukanya

    we can write the code without main() function. The code will gets compiled and during compilation it won't show any error but while executing it show an error saying LINKER ERROR.



    Posted by: deepak    

    Contact deepak Contact deepak

    Yes we can write a C Program without writing main()

    see this

    #include<stdio.h>
    #include<conio.h>
    #pragma start show()

    void show()
    {
    printf("Hello");
    }



    Posted by: Swesh Mishra    

    Contact Swesh Mishra Contact Swesh Mishra

    yes you can write a program without main but in short just like this

    #include<stdio.h>
    #include<conio.h>
    {
    printf("stupids we can do anything");
    }



    Posted by: sanjana    

    Contact sanjana Contact sanjana

    yes we can write c prog without using main() but it will not run because the prog execution always begins with the main() function.
    so it will give an error.



    Posted by: abhinav kumar singh    

    Contact abhinav kumar singh Contact abhinav kumar singh

    The compilation process always starts from the first line of your source code and the compiler checks only syntax, if you don't have a main() not at all a problem. But if you want to execute that you need main(). Check how we are going to create a header file are we going to place a main() in a header file.



    Posted by: venkatesh    

    Contact venkatesh Contact venkatesh

    No we conn't write c program with out using main().Because it shows error in its run time.



    Posted by: Latthi.k    

    Contact Latthi.k Contact Latthi.k

    #include<stdio.h>

    #define decode(s,t,u,m,p,e,d) m##s##u##t
    #define begin decode(a,n,i,m,a,t,e)

    int begin()
    {
    printf(? without main ?);
    }



    Posted by: naresh    

    Contact naresh Contact naresh

    main is not compulsory, every C program needs an entry point so it can be main, pain, slain or anything else, in embedded systems the low level startup code jumps to main, u can change it to something else, similar in visual studio or turbo C u need to find out a compile directive to change the default jump to main.
    and to correct the above answers it is not an runtime error, it is linking error



    Posted by: shrads    

    Contact shrads Contact shrads

    yes, we can write c program without main function ,but this program can't execute without main function.
    some errors can you found.
    so, boy you write with main....,



    Posted by: ritu raj    

    Contact ritu raj Contact ritu raj

    Ya it is not possible to execute a C program without main though u can compile it successfully.
    Compiler will check for only syntactical errors.
    It never bother about whether the main is present in the program or not.
    It is possible to execute a program without main in java but not in C.



    Posted by: Ravinder Reddy     

    Contact Ravinder Reddy Contact Ravinder Reddy

    we can write a c program without main function using PreProcessors.

    #define k 5
    #define n 12
    #if (n>k) printf("n is big campare to k ");
    #elif printf("n is small campare to k");
    #endif



    Posted by: Bharat kumar .G    

    Contact Bharat kumar .G Contact Bharat kumar .G

    Hi Friends..

    See the above code which is posted by Venkat..That code will works but at there also internally void()methods called by the processor.



    Posted by: Kiran Kuma Kala    

    Contact Kiran Kuma Kala Contact Kiran Kuma Kala

    yes, we can write but in the linking time it will throw error
    undefined reference to main
    ld returned one exit status



    Posted by: iranna    

    Contact iranna Contact iranna

    according to me,program can be written without main() but it will not run because before runing the program compiler will not compile the program without main().



    Posted by: vikash chesi    

    Contact vikash chesi Contact vikash chesi

    yes,use #pragma(preprocessor)



    Posted by: m.rajendra    

    Contact m.rajendra Contact m.rajendra

    yes we can get the answer without using main function in c.
    try this.

    #include<stdio.h>
    #define siva(s,o,l,v,i,n,g) v##s##l##o
    #define start siva(a,n,i,m,a,l,s)
    int start()

    {
    printf(" we got it ");
    return 0;
    }



    Posted by: siva    

    Contact siva Contact siva

    yes u can write without main but it will not run.it will show error.



    Posted by: sureshkumar.v    

    Contact sureshkumar.v Contact sureshkumar.v

    Finally main is must



    Posted by: bharat    

    Contact bharat Contact bharat

    yes, ofcourse u can but it cant b excuted.............



    Posted by: prabhakar    

    Contact prabhakar Contact prabhakar

    yes,ofcourse we can write it without main function but it can't run it.bcz before run the prog the compiler will not compile the prog . it will show the run time error.



    Posted by: sourav    

    Contact sourav Contact sourav

    262, N.S.Road, Sarkarpara, Sheoraphuly, Hooghly.

    pin- 712223



    Posted by: Suhit Sinha    

    Contact Suhit Sinha Contact Suhit Sinha

    #include<stdio.h>
    #include<conio.h>

    void foo(void);

    #pragma startup foo

    void foo()
    {
    int x,y;
    printf("Enter a number ");
    scanf("%d", &x);
    printf("Enter anorther number ");
    scanf("%d",&y);
    printf("The sum is %d ", x+y);
    getch();
    }

    void main()
    {

    }



    Posted by: Suhit Sinha    

    Contact Suhit Sinha Contact Suhit Sinha

    without main function we can't write the c program it will show an error.



    Posted by: supriya    

    Contact supriya Contact supriya

    Yes we can do.



    Posted by: Sajan Tripathy    

    Contact Sajan Tripathy Contact Sajan Tripathy

    We cant run c program without main function.
    Some told that we can run and they showed some programs. In that program main fun is there but is hidden. Dont tell such things if you are not confirm rashmi.



    Posted by: suyambu    

    Contact suyambu Contact suyambu

    yes i can write a program without using main
    #include<stdio.h>
    #include<stdlib.h>
    exmination()
    {
    printf("shravan");
    }



    Posted by: shravan kumar katta    

    Contact shravan kumar katta Contact shravan kumar katta

    I DISAGREE TO EVERYONE'S ANSWER SAYING THAT A "C" PROGRAM CAN SUCCESSFULLY RUN WITHOUT THE "MAIN" FUNCTION...

    1.)There must be the use of main somewhere in the program directly or indirectly for a C program to run.
    2.)The guys giving the example (as restated below)please understand the program intension:--

    #include<stdio.h>
    #define siva(s,o,l,v,i,n,g)v##s##l##o
    #define start siva(a,n,i,m,a,l,s)
    int start()

    {
    printf(" we got it ");
    return 0;
    }

    here the ## operator is used to form the word "main" in place of "start"...THUS THE PROGRAM SEES FORMATION OF "main" BEFORE IT PROCEEDS.

    3)Pragma directive is used for program execution only after the "Main" has completed..SO THE PROGRAM DEPENDS ON "MAIN"



    Posted by: sourav    

    Contact sourav Contact sourav

    #include<stdio.h>
    #include<conio.h>
    {
    printf("stupids we can do anything");
    }
    es program m to error aayegi * declaration terminated incorrectly*



    Posted by: reena gangwar    

    Contact reena gangwar Contact reena gangwar

    #include<stdio.h>
    #define decode(s,t,u,m,p,e,d) m##s##u##t
    #define begin decode(a,n,i,m,a,t,e)

    int begin()
    {
    printf("Guna");
    }





    Does the above program run without the main function? Yes, the above 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.

    NOTE: A Preprocessor is program which processess the source code before compilation.

    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?



    Posted by: Gunaram Mondal    

    Contact Gunaram Mondal Contact Gunaram Mondal

    #define start main
    #define program ()
    #define now {
    #define end }

    start program now
    printf("program without main");
    end



    Posted by: Hari Srinivas    

    Contact Hari Srinivas Contact Hari Srinivas

    If you have the better answer, then send it to us. We will display your answer after the approval.
    Rules to Post Answers in CoolInterview.com:-

  • There should not be any Spelling Mistakes.
  • There should not be any Gramatical Errors.
  • Answers must not contain any bad words.
  • Answers should not be the repeat of same answer, already approved.
  • Answer should be complete in itself.
  • Name :*
    Email Id :*
    Answer :*
    Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
    Process Verification  Enter the above shown code:*
    Inform me about updated answers to this question

       
    Related Questions
    View Answer
    Tell me answer for this statement
    printf("printf("aim")");
    View Answer
    Difference between :- 1) C and C++
    2) main() in c and main() in c++ ?
    View Answer
    What is :- Asembler , compiler , Preprocessor , laxical analysis , parsing ?
    View Answer
    How can we use data connectivity in 'c' language?
    View Answer
    Write a program for result management system of a college ?
    View Answer
    Is it possible to print graphics output in C?
    View Answer
    How can C programs run without header files?
    View Answer
    What is c graphics?
    View Answer
    write a program for printing when i press a key next to the letter in the alphabetical order can be printed
    View Answer
    how to create relation calculator program using c language
    View Answer
    how to create love flames program using c language
    View Answer
    Can anyone please tell me about a website from which I could install Turbo C (compiler) for free??
    View Answer
    What is the use of semicolon at the end of every statement?
    View Answer
    What are the packages in c?
    View Answer
    What is object file? How can you access object file?
    View Answer
    Is there anything you can do in C++ that you cannot do in C ?
    View Answer
    What is an abstract base class?
    View Answer
    What is the difference between compile time error and run time error?
    View Answer
    What is the difference between #include <file> and #include �file�
    View Answer
    When we access the integer and the character variables without declaring them in program?and Why?
    View Answer


    Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.

    View ALL C Basics Interview Questions

    User Options
    Sponsored Links


    Copyright ©2003-2014 CoolInterview.com, All Rights Reserved.
    Privacy Policy | Terms and Conditions

    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Dedicated Servers | C Interview Questions & Answers

    Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes


    Cache = 0 Seconds