Thread: i read the faq, but i dont understand wut int main (int argc, char *argv[]) is

  1. #1
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116

    i read the faq, but i dont understand wut int main (int argc, char *argv[]) is

    http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    thats the faq page for it, but i didnt understand what it meant. ive always used int main(), but my compiler defaults to int main (int argc, char *argv[]). i dont see what the difference is.
    But what about int main(int argc, char *argv[], char *envp[])
    As an extension to the guaranteed standard, an additional parameter to main() can, on some systems, be used to gain access to the environment variables. This is isn't guaranteed to work on all compilers, so use it with care if you want to keep your code portable.
    is int main(int argc, char *argv[], char *envp[]) the same thing as (int argc, char *argv[])? thanks for the help
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    int main (int argc, char *argv[]) is for accepting command line arguments. If you don't need to accept any, ignore it.
    Away.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    When you run a program (even from a symbolic link), the first argument is always its name (so, argc - the argument count - is always at least one). Here is an example (invoking gcc, for example):

    % gcc -oprogram program.cpp

    argc = 3
    argv[0] = "gcc"
    argv[1] = "-oprogram" (which will later be translated into the flag "-o" and argument "program"
    argv[2] = "program.cpp"

    Hope this helps.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138

    Re: i read the faq, but i dont understand wut int main (int argc, char *argv[]) is

    Originally posted by Geo-Fry

    is int main(int argc, char *argv[], char *envp[]) the same thing as (int argc, char *argv[])? thanks for the help
    No. The char* envp[] gives you a little more info. It tells you some information about the environment.

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

    Re: Re: i read the faq, but i dont understand wut int main (int argc, char *argv[]) i

    Originally posted by golfinguy4
    No. The char* envp[] gives you a little more info. It tells you some information about the environment.
    Also note that int main(int argc, char *argv[], char *envp[]) is non-standard. If you want environmental variables, use the getenv() function included in stdlib.h, or cstdlib.

  6. #6
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    oh ok, i understand what it is now. some of the programs i downloaded from the previous contests had to use parameters and had a bunch of things dealing with the int argv and char *argv[]. thank you for your help
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    stdlib.h is non-standard too.
    Use cstdlib.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    fowl
    Guest
    int main(int argc, char **argv){
    cout << "Calling program: << *argv++ << endl
    << argc << " items in the command line:"<< endl;
    argc = 0;
    while(*argv) cout << ++argc << ") " << *argv++ << endl;
    return cin.get()
    }

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> Wrong, it is standard.

    So is stdio.h, but you've got the wrong language.


    >>> Not necessarily...

    All right, I'll take your word for that, but I've never seen meaningless value passed for argv[0] in UNIX/Linux/anything else.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Interesting... though somewhat surprising. I'd still recommend against dumping everything into the global namespace like that, though... Oh well.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >though somewhat surprising.
    Why? C++ was designed with compatibility to C in mind. It boasts that just about any valid C program is also a C++ program. If standard C++ disallowed the standard C headers then any hope of compatibility with C would be destroyed.
    My best code is written with the delete key.

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    They aren't perfectly compatible. There are differences (especially since the C99 standard - which of course is newer than the C++98 standard). There were some things in the older C that C++ didn't allow, and it seems to me that putting all standard library headers in the same namespace would make a lot of sense, without making things irreparably incompatible... thats all.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  13. #13
    Registered User
    Join Date
    Jun 2003
    Posts
    11
    Originally posted by vVv
    > So is stdio.h

    Indeed!

    > but you've got the wrong language.

    Nope, I'm talking about Standard C++. I've posted the relevant quotation in the past, but here it is again (ISO/IEC 14882-1998, Annex D.5):



    Just in case someone is going to accuse me of recommending C headers in C++ code: I am not. I'm just saying that using C headers in C++ code is permitted by the standard, while old C++ headers like iostream.h are no longer allowed.
    Here is one doubt from a newbie.

    So does it mean that for c headers we simply write them the way they are (stdio.h) but for c++ headers we use namespace (iostream and then using namespace) ???
    Never Give up.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >They aren't perfectly compatible.
    And they shouldn't be. If they were then they really would be the same language, don't you think? Some differences are to be expected, but not for something as extensively used as the standard library.

    >and it seems to me that putting all standard library headers in the same namespace would make a lot of sense
    I don't know about you, but I would be terribly annoyed if C++ (with claims of good compatibility with C) refused to compile this
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        printf("Hello, World!\n");
    
        return 0;
    }
    because it made sense to put all standard library headers in the same namespace. Even for this simple program, porting to C++ would be a pain:
    Code:
    #include <cstdio>
    
    int main(void)
    {
        std::printf("Hello, World!\n");
    
        return 0;
    }
    And as we both know, this is no longer valid C. The standards committee agreed that C functions should be in the std namespace, but doing so exclusively would invalidate far too much existing code. So the compromise was made to have two variants of the C standard library, one for namespaces and one for compatibility with C.

    >So does it mean that for c headers we simply write them the way they are (stdio.h)
    Not quite. While the *.h C headers are supported for compatibility with C code, new C++ code should be written using the C++ headers (cstdio, cstdlib, etc...) and namespaces employed.
    My best code is written with the delete key.

  15. #15
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> And they shouldn't be. If they were then they really would be the same language...

    Thats the point I was making.

    >> And as we both know, this is no longer valid C.

    Many things in C++ aren't. We seem to be going in circles. If someone really has a problem with even 'using namespace std;', then there is nothing to prevent quickly writing the following:
    Code:
    #ifndef STDIO_H
    #define STDIO_H
    
    #include <cstdio>
    using namespace std; // Or just including the stdio specific symbols.
    
    #endif
    Anyways, that is my opinion on the subject. I know they had their reasons for coming to the compromise they did, but I also have my reasons for my opinion.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  2. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  3. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 06:40 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM