Write a C-program without main() method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bagkalyan
    New Member
    • Sep 2007
    • 1

    Write a C-program without main() method

    please help me..

    Yes it is possible to write a program in c without using main.
    Here is the code:[code=c]
    /* prog_without_ma in.c */
    _start()
    {
    _exit(my_main() );
    }
    int my_main(void)
    {
    printf(”Hello\n ”);
    return 42;
    }[/code]
    And use this command (% is command prompt) to compile:
    %gcc -O3 -nostartfiles prog_without_ma in.c
    Try compiling this example:[code=c]
    #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)
    void begin()
    {
    printf(”hello”) ;
    }[/code]
    here is how it works once we say define we need to understand that
    #define x y
    then ‘x’ ix replaced by ‘y’
    similarly in this case
    #define begin decode(a,n,i,m, a,t,e)
    decode(a,n,i,m, a,t,e) is replaced by m##a##i##n
    bcoz s is replaced by a,t by n,u by i and so on
    s->a
    t->n
    u->i
    m->m
    p->a
    e->t
    d->e
    now the statement becomes
    void m##a##i##n
    And u must be knowing that ## is used for string concatenation so it becomes
    “main”
    finally the code crops down to[code=c]
    void main()
    {
    printf(”hello”) ;
    }[/code]
    Last edited by sicarie; Sep 7 '07, 01:04 PM. Reason: Code tags
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Originally posted by bagkalyan
    please help me..

    Yes it is possible to write a program in c without using main.
    Here is the code:
    ...
    This most certainly belongs in the C/C++ Forum! ^^

    Greetings,
    Nepomuk

    Comment

    • Purple
      Recognized Expert Contributor
      • May 2007
      • 404

      #3
      Hi bagkalyan and welcome to TSDN,

      As this post is a technical question related to c/c++, I have moved it from the cafe to the appropriate forum.

      Take a moment to read the posting guidelines for this site here.

      MODERATOR

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by bagkalyan
        please help me..

        Yes it is possible to write a program in c without using main.
        Here is the code:
        /* prog_without_ma in.c */
        _start()
        {
        _exit(my_main() );
        }
        int my_main(void)
        {
        printf(”Hello\n ”);
        return 42;
        }
        And use this command (% is command prompt) to compile:
        %gcc -O3 -nostartfiles prog_without_ma in.c
        Try compiling this example:
        #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)
        void begin()
        {
        printf(”hello”) ;
        }
        here is how it works once we say define we need to understand that
        #define x y
        then ‘x’ ix replaced by ‘y’
        similarly in this case
        #define begin decode(a,n,i,m, a,t,e)
        decode(a,n,i,m, a,t,e) is replaced by m##a##i##n
        bcoz s is replaced by a,t by n,u by i and so on
        s->a
        t->n
        u->i
        m->m
        p->a
        e->t
        d->e
        now the statement becomes
        void m##a##i##n
        And u must be knowing that ## is used for string concatenation so it becomes
        “main”
        finally the code crops down to
        void main()
        {
        printf(”hello”) ;
        }
        The second really isn't a program without main. Like you said, it comes to the compiler like:
        [code=c]
        void main()
        {
        [/code]
        Anyway, what is your question?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by ilikepython
          The second really isn't a program without main. Like you said, it comes to the compiler like:
          [code=c]
          void main()
          {
          [/code]
          Erm, it's "int", not "void". "int"; remember that: int, int, int, intintintintint intint!

          kind regards,

          Jos ;-)

          Comment

          Working...