Thread: [Help] Main

Results 1 to 14 of 14
  1. #1
    Gab's Avatar
    Join Date
    Jul 2010
    Gender
    male
    Posts
    6,716
    Reputation
    1755
    Thanks
    1,543

    [Help] Main

    I want to know what is the difference between those functions, and why there isn't only one. :

    Code:
    int main()
    int main(int argc, char *argv[])
    int main(int argc, char *argv)
    int main(void)
    void main()
    void main(void)
    Thank u

  2. #2
    Kallisti's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    深い碧の果てに
    Posts
    4,019
    Reputation
    52
    Thanks
    376
    My Mood
    In Love
    They all do the same thing.

    未来が見えなくて怖いから
    未来が見えてしまって悲しいから
    目を閉じて優しい思い出に浸ってしまう




  3. #3
    Void's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Location
    Inline.
    Posts
    3,198
    Reputation
    205
    Thanks
    1,445
    My Mood
    Mellow
    I don't think the main function in C could be of void return type.

    1st and 4th are the same.

    2nd's main function can take multiple parameters where as the 3rd's can only take one ( why would you do that? ).

    Read about arrays and command line arguments to get detailed explanations. Once you understand them this won't be a problem anymore.

  4. #4
    Gab's Avatar
    Join Date
    Jul 2010
    Gender
    male
    Posts
    6,716
    Reputation
    1755
    Thanks
    1,543
    thank u guyz (:

  5. #5
    -Raz0r-'s Avatar
    Join Date
    Aug 2010
    Gender
    male
    Location
    Australia
    Posts
    117
    Reputation
    15
    Thanks
    38
    My Mood
    Lurking
    The 'standard' declaration of main, should be..
    [php]int main( int argc, char *argv[] )[/php]
    I tend to use..
    [php]int main( int argc, char **argv )[/php]
    because of the whole 'array decay to pointer' thing. I don't know, I like pointers.

    The return value of main should be an int, notifying the user/OS whether the program terminated correctly or not (You should return the constants EXIT_FAILURE or EXIT_SUCCESS, as defined in <stdlib.h>)

    The first argument to main is the amount of arguments passed in via the command line, the second argument is a pointer to the array of arguments.
    This provides you enough functionality to retrieve information about how the program should run, and notify the OS how the program terminated.

    The reason you see a few different declarations, as far as I can tell, is because some compilers aren't as strict. Don't expect your code to be portable if you don't conform to the standard.
    Last edited by -Raz0r-; 10-10-2010 at 11:59 PM.
    Languages: C, C++, x86 ASM, PHP, Lua

  6. #6
    .::SCHiM::.'s Avatar
    Join Date
    Sep 2010
    Gender
    male
    Posts
    733
    Reputation
    180
    Thanks
    880
    My Mood
    Twisted
    I've always learnt that

    void main();

    Is wrong, wrong, wrong!
    And should never be used, it's not defined in the ASNI standard, and thus it's not really part of the language. I think it's something that .net permits you to do. But a native compiler should not allow void main();

    I'm SCHiM

    Morals derive from the instinct to survive. Moral behavior is survival behavior above the individual level.

    Polymorphic engine
    Interprocess callback class
    SIN
    Infinite-precision arithmetic
    Hooking dynamic linkage
    (sloppy)Kernel mode Disassembler!!!

    Semi debugger




  7. The Following User Says Thank You to .::SCHiM::. For This Useful Post:

    Hell_Demon (10-11-2010)

  8. #7
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    Quote Originally Posted by xXModz View Post
    I want to know what is the difference between those functions, and why there isn't only one. :

    Code:
    int main()
    int main(int argc, char *argv[])
    int main(int argc, char *argv)
    int main(void)
    void main()
    void main(void)
    Thank u
    int main() <- bad
    int main(int argc, char *argv[]) <- good
    int main(int argc, char *argv) <- bad
    int main(void) <- bad
    void main() <- bad
    void main(void) <- bad
    int main(int argc, char **argv) <- good
    int main(int argc, char argv[][]) <- good
    Ah we-a blaze the fyah, make it bun dem!

  9. #8
    Gab's Avatar
    Join Date
    Jul 2010
    Gender
    male
    Posts
    6,716
    Reputation
    1755
    Thanks
    1,543
    Quote Originally Posted by Hell_Demon View Post
    int main() <- bad
    int main(int argc, char *argv[]) <- good
    int main(int argc, char *argv) <- bad
    int main(void) <- bad
    void main() <- bad
    void main(void) <- bad
    int main(int argc, char **argv) <- good
    int main(int argc, char argv[][]) <- good
    I always use int main() ...

    But thank u, i'll use int main(int argc, char *argv[])

  10. #9
    doofbla's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    Biel*****/Germany
    Posts
    369
    Reputation
    10
    Thanks
    179
    My Mood
    Psychedelic
    i also would prefer
    int main(int argc, char **argv) instead of
    int main(int argc, char *argv[])

    because a array is nothing more than a pointer, and in my eyes **argv is better because if you're calling a function with this parameter **argv it is more clear that it should be a pointer to a pointer like:

    char * c;
    &c is a pointer to a pointer then

    and with ** it is clear that you need a POINTER to a POINTER


    But in the end there isn't any damn different so use what you want
    _____________________________________________

    READING TUTORIAL:

    1. READ MY POST
    2. THINK ABOUT MY POST
    3. PRESS THANKS
    4. MAYBE CORRECT MY POSTS :P




    Dijkstra:
    "Computer Science is no more about computers than astronomy is about
    telescopes."


    THANKS BUTTON RIGHT DOWN --->

  11. #10
    .::SCHiM::.'s Avatar
    Join Date
    Sep 2010
    Gender
    male
    Posts
    733
    Reputation
    180
    Thanks
    880
    My Mood
    Twisted
    Quote Originally Posted by Hell_Demon View Post
    int main() <- bad
    int main(int argc, char *argv[]) <- good
    int main(int argc, char *argv) <- bad
    int main(void) <- bad
    void main() <- bad
    void main(void) <- bad
    int main(int argc, char **argv) <- good
    int main(int argc, char argv[][]) <- good
    But If you know your program won't be receiving any parameters through the command line?
    Wouldn't one be eating space one doesn't need?
    I mean, if we look at an assembly entry point (proc main) we don't see any special variables either...

    Code:
    main:
    main proc
    
    push eax
    push ...
    mov...
    ca...
    c...
    ...
    main endp
    END main

    I'm SCHiM

    Morals derive from the instinct to survive. Moral behavior is survival behavior above the individual level.

    Polymorphic engine
    Interprocess callback class
    SIN
    Infinite-precision arithmetic
    Hooking dynamic linkage
    (sloppy)Kernel mode Disassembler!!!

    Semi debugger




  12. #11
    Void's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Location
    Inline.
    Posts
    3,198
    Reputation
    205
    Thanks
    1,445
    My Mood
    Mellow
    Quote Originally Posted by .::SCHiM::. View Post
    But If you know your program won't be receiving any parameters through the command line?
    Wouldn't one be eating space one doesn't need?
    I mean, if we look at an assembly entry point (proc main) we don't see any special variables either...

    Code:
    main:
    main proc
    
    push eax
    push ...
    mov...
    ca...
    c...
    ...
    main endp
    END main
    That's what we have the stack pointer for. :P

  13. #12
    .::SCHiM::.'s Avatar
    Join Date
    Sep 2010
    Gender
    male
    Posts
    733
    Reputation
    180
    Thanks
    880
    My Mood
    Twisted
    Quote Originally Posted by Void View Post
    That's what we have the stack pointer for. :P
    True, so C++ doesn't allocate extra space is you define it as (int argc, char *argv[]) because the (if any) arguments are just pushed to the stack? like you would with a normal function?

    I'm SCHiM

    Morals derive from the instinct to survive. Moral behavior is survival behavior above the individual level.

    Polymorphic engine
    Interprocess callback class
    SIN
    Infinite-precision arithmetic
    Hooking dynamic linkage
    (sloppy)Kernel mode Disassembler!!!

    Semi debugger




  14. #13
    -Raz0r-'s Avatar
    Join Date
    Aug 2010
    Gender
    male
    Location
    Australia
    Posts
    117
    Reputation
    15
    Thanks
    38
    My Mood
    Lurking
    One can only assume the calling convention is the standard cdecl (Caller maintains the stack)

    AFAIK from my little experiments involving 'TinyPE' and 'TinyELF' files, 'main' isn't the real entry point - there's a proxy layer between the OS and your program's 'main' function and this is where the stack is maintained(?)
    Languages: C, C++, x86 ASM, PHP, Lua

  15. #14
    Gab's Avatar
    Join Date
    Jul 2010
    Gender
    male
    Posts
    6,716
    Reputation
    1755
    Thanks
    1,543
    Thank you all for your answers

Similar Threads

  1. [Help Request] Combat arms Vid help
    By djw111 in forum Combat Arms Help
    Replies: 4
    Last Post: 12-24-2011, 05:06 PM
  2. need help with dll main
    By steffen123456789 in forum CrossFire Hack Coding / Programming / Source Code
    Replies: 5
    Last Post: 08-13-2011, 11:51 AM
  3. [Help Request] AFK Bot [help]
    By fet in forum Combat Arms Help
    Replies: 7
    Last Post: 04-28-2011, 03:17 AM
  4. [Help Request] Ajuda / Help
    By - Battery' in forum Combat Arms BR Coding Help
    Replies: 3
    Last Post: 04-22-2011, 07:15 PM
  5. [Help Request] Help my!
    By Windowns7 in forum Combat Arms BR Coding Help
    Replies: 2
    Last Post: 04-18-2011, 01:41 PM