simple test of main signature fails

Discussion in 'C++' started by Comp1597@yahoo.co.uk, Apr 1, 2009.

  1. Guest

    On Visual Studio 2008 Express, I compiled and ran the following
    program:

    #include "stdafx.h"
    #include <iostream>


    int main(int argc, char * argv[])
    {
    std::cout << argc;

    }

    At the Visual Studio Command Prompt, I entered ProjectName.exe(3, "a",
    "b", "c")

    My thinking was that argc denotes the number of string arguments to
    the main function. This is indicated by the 3 strings "a", "b" and
    "c" and by the first integer parameter which I set to 3.
    So I expected that argc==3 and that therefore the output would be 3.
    However, the output was 2

    I'd be grateful if someone could explain this.

    Thanks in advance.
    , Apr 1, 2009
    #1
    1. Advertising

  2. wrote:
    > On Visual Studio 2008 Express, I compiled and ran the following
    > program:
    >
    > #include "stdafx.h"
    > #include <iostream>
    >
    >
    > int main(int argc, char * argv[])
    > {
    > std::cout << argc;


    Why not go the extra step and also do

    for (int i = 0; i < argc; ++i)
    std::cout << argv << std::endl;

    It would help you see what you get...

    >
    > }
    >
    > At the Visual Studio Command Prompt, I entered ProjectName.exe(3, "a",
    > "b", "c")
    >
    > My thinking was that argc denotes the number of string arguments to
    > the main function. This is indicated by the 3 strings "a", "b" and
    > "c" and by the first integer parameter which I set to 3.
    > So I expected that argc==3 and that therefore the output would be 3.
    > However, the output was 2
    >
    > I'd be grateful if someone could explain this.


    The arguments to 'main' are supplied by the execution environment in the
    system- and implementation-specific manner. How you are supposed to
    supply the arguments to the execution environment when you make your
    program run is also system-specific. Try

    ProjectName.exe a b c

    (the command processor in DOS/Windows collects the strings from the
    line, along with the program name, and puts them in the array which it
    passes then to the program). You should see 4 as the output since there
    are four independent strings (spaces are delimiters) in the command
    line. Also try

    ProjectName.exe "a b c"

    which should give you 2, but the quotes will be removed from the second
    argument and the spaces will become part of the argument string.

    Experiment!

    Good luck!

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 1, 2009
    #2
    1. Advertising

  3. red floyd Guest

    wrote:
    > On Visual Studio 2008 Express, I compiled and ran the following
    > program:
    >
    > #include "stdafx.h"
    > #include <iostream>
    >
    >
    > int main(int argc, char * argv[])
    > {
    > std::cout << argc;
    >
    > }
    >
    > At the Visual Studio Command Prompt, I entered ProjectName.exe(3, "a",
    > "b", "c")
    >
    >


    Why do you think you need the parens? Does any other progra need parens
    like that?
    red floyd, Apr 2, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JJBW
    Replies:
    1
    Views:
    10,064
    Joerg Jooss
    Apr 24, 2004
  2. aruna

    Signature of main

    aruna, Apr 28, 2004, in forum: Java
    Replies:
    11
    Views:
    655
    Steven J Sobol
    May 6, 2004
  3. Gustavo Narea
    Replies:
    14
    Views:
    851
    Gustavo Narea
    Feb 16, 2009
  4. G B-)
    Replies:
    1
    Views:
    114
    Michal Suchanek
    Feb 15, 2006
  5. D. Krmpotic
    Replies:
    4
    Views:
    223
    NAKAMURA, Hiroshi
    Aug 3, 2007
Loading...

Share This Page