Thread: Help understanding ? int main(int argc, char *argv[])

  1. #1
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57

    Question Help understanding ? int main(int argc, char *argv[])

    Code:
    int main(int argc, char *argv[])
    through out read programming books and I keep see this under advanced programming Techniques but it dosen't go in detail on what it's about. I was hoping someone could tell me what it dose if it dose anything and is it a good idea to use it. because i see it use in one section of the book and never see it again and i don't see anyone on forums or other website use it ether, so if someone can help me thanks........
    "Blood you have thirsted for -- now, drink your own!"
    (Dante)

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    596

  3. #3
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Using "int argc, char *argv[]" as function arguments let you make a function that can take unlimited arguments, specified by the "argc" if I remember correctly
    so if you had a function like:
    string cookies(int argc, char *argv[]) you can call it by using
    cookies(4, choco, normal, other, vanilla) -- or something similar
    Or that's how I was told it worked
    Currently research OpenGL

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That is incorrect.
    It's a special version of main, to put it simple.
    argc will contain the number of arguments passed in and argv will contain the contents of those command lines.
    It is not magical - it is simply defined by the standard that it should be so, and so usually there is some code in or before main that fills those arguments properly before use.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Dammit! I fail again >.<
    Currently research OpenGL

  6. #6
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    If you're at all familiar with unix command line tools (CLTs), then you are familiar with examples that utilize int main(int argc, char *argv[]). In this case, it allows you to pass parameters to main, to change the behavior of the application.

    For example:
    Code:
    $ cal
    in unix will print the current month to the console.

    But if you add a paramter:
    Code:
    $ cal -y
    the same program (but with different behavior) will now print the entire current year.

    EDIT: That "-y" is passed to main, and within main, a different flow of events occurs then would have without the extra parameter.
    Last edited by dudeomanodude; 12-06-2008 at 04:44 PM.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Btw, usually the command line is passed as single long string (at least it is in Windows; I cannot speak for linux). Then the C runtime before main guts and cuts that long command line to fit into argv.
    That's the true story.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    In Linux, it works differently. The fundamental system call in Windows for creating a process (these days, CreateProcess, but in 16-bit Windows, it was WinExec) expects a single string for simplicity (so many things in Windows are relics of efficiency concerns from the early days). Why should the OS go to the trouble and expense of parsing the command line if the program doesn't really need it parsed? So Windows doesn't.

    Linux, on the other hand, follows the tradition of Unix, where it's simply expected that the command line is an array of strings. The extreme efficiency concerns of early Windows versions never bothered Linux, which was created for the 386 initially and could rely on a reasonable amount of processing power being available. The fundamental system call for executing a new process image (exec) thus expects an array of strings, not a single string. This actually can be more efficient when one program calls another, because it might already have the arguments available separately. Then, you avoid the overhead of concatenating all arguments, only to have them parsed apart again. It is thus only shells that have to parse arguments.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think the main reason for Windows doing it the way it is, is not that Windows was memory restricted, but rather that DOS, which Windows ran on top of, was doing things that way, and DOS applications and Windows applications may well be coexisting on the same system. DOS, however, was designed to run on a machine with a 8086 and 64KB of memory (and 320KB of floppy-disk, or if you had LOTS of money, a 5MB hard-disk).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I don't think so, for several reasons:
    1) DOS used a classic main() function for its programs, so it needed the split command line. Only Windows introduced WinMain().
    2) Windows had its own process management. Obviously, since DOS didn't really have such a thing.
    3) Raymond Chen said it was Windows that did it this way.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    I don't think so, for several reasons:
    1) DOS used a classic main() function for its programs, so it needed the split command line. Only Windows introduced WinMain().
    2) Windows had its own process management. Obviously, since DOS didn't really have such a thing.
    3) Raymond Chen said it was Windows that did it this way.
    Well, DOS itself wasn't all written in C [if any, in the original version] - much of it was written in assembler. The system call itself to load and execute a new executable takes one string. Part of this is of course that they copied CP/M which also had one long string as command line argument.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  4. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  5. Passing a double array to a function as an argument
    By Glirk Dient in forum C++ Programming
    Replies: 20
    Last Post: 09-10-2003, 02:54 PM