locked
Doubt about main(int argc , char *argv[])

    Question

  •  

    Hi All

    I ve a doubt. I could nt find in net.

    Why some times main(int argc , char *argv[]) is workiong fine while main() giving error.

     

    Regards

    Sudhansu

    Tuesday, April 15, 2008 5:32 AM

Answers

  • "main()" is only valid in a 'C' program.  C++ requires "int main()".
    Tuesday, April 15, 2008 6:50 AM
    Moderator
  •  

    One more example:

     

    This program will output 123 456 abc whether built with UNICODE set or not:

     

    Code Snippet

     

    int _tmain (int argc, _TCHAR **argv )
    {
    #ifdef _UNICODE
    wprintf(L"\n%s %s %s\n", argv[1], argv[2], argv[3]);
    #else
    printf("\n%s %s %s\n", argv[1], argv[2], argv[3]);
    #endif
     return 0;
    }

     

     

    - Wayne

     

     

    Tuesday, June 24, 2008 1:20 AM

All replies

  • Wat's The error you are geting Can you elaborate your question Little bit more . i am sure atleast you are not performing any calculation on argv or argc in case of main()

    Thanx
    Tuesday, April 15, 2008 6:33 AM
  • "main()" is only valid in a 'C' program.  C++ requires "int main()".
    Tuesday, April 15, 2008 6:50 AM
    Moderator
  • Hi proggers,

    For some reason this does not work. I forget how this stuff works and I have been looking around and keep going in the same circles. *argv[] or argv[][] or **argv or whatever. but never an all element type example.

    Can someone please correct this code?

    I would be very greatfull.

    Thanx and greetz
    Seppe


    Code Snippet

    #include<stdio.h>
    #include<malloc.h>
    #include<stdlib.h>
    #include<string.h>
    #include<conio.h>


    char ibestand[46];
    char ebestand[46];
    int key;


    int main (int argc,char **argv )
    {
     if ( argc > 1 )
     {
      key = atoi(argv[0]);
      if ( argc > 2 )
      {
       ibestand = &argv[1];
       if (argc > 3 )
       {
        ebestand = &argv[2];
       }
      }
     }

     printf("%d %s %s", key, &ibestand, &ebestand);

     scanf("%c");

     return 0;

    }

    Wednesday, April 23, 2008 12:07 PM
  • ibestand and ebestand should be char*, not char[].  Or use strcpy() to copy the argv[] string into ibestand.  The scanf() call doesn't look happy either.
    Wednesday, April 23, 2008 12:28 PM
    Moderator

  • As previously noted, you have to copy the string arguments using a
    function such as strcpy. You can't assign a C string, like you can with
    a C++ string object.

     

    Note that the first argument (argv[0]) is always the name of the program
    itself. Your command line arguments begin at argv[1].

    Try this:

     

    Code Snippet

     

    #include<stdio.h>
    #include<malloc.h>
    #include<stdlib.h>
    #include<string.h>
    #include<conio.h>

    char ibestand[46];
    char ebestand[46];
    int key;

    int main (int argc,char **argv )
    {
     if ( argc > 1 )
     {
      key = atoi(argv[1]);
      if ( argc > 2 )
      {
     strcpy(ibestand, argv[2]);
       if (argc > 3 )
       {
        strcpy(ebestand, argv[3]);
       }
      }
     printf("%d %s %s", key, ibestand, ebestand); 
     }
      return 0;
    }

     

     

     

     


    Note that this is inherently unsafe, as buffer overruns may occur if
    arguments greater than 45 characters are input.


    - Wayne

     

    Wednesday, April 23, 2008 9:12 PM
  • strcpy() is only copying the first character to ibestand[]. EDIT: same for ebestand[]

    i.e. I'd run the program and pass "12345" as an argument.

    ibestand[0] will be '1'
    ibestand[1] will be 0


    I don't understand why...I was expecting
    ibestand[] to contain "12345"
    Monday, June 23, 2008 10:04 PM
  •  

    What are the *exact* arguments you are passing?

     

    - Wayne

     

     

    Monday, June 23, 2008 10:48 PM
  • I have the VS2005 project properties set to pass the following command arguments:

    123 456 abc


    stepping through the program in debug mode yields:

    key == 1

    ibestand == '4' (next character in array is a 0)

    ebestand == 'a' (next character in array is a 0)


    which has me very puzzled.
    Monday, June 23, 2008 11:08 PM
  •  

    Quote>stepping through the program in debug mode ...

     

    What does the printf display?

     

    - Wayne

     

    Monday, June 23, 2008 11:38 PM
  •  WayneAKing wrote:
     

    What does the printf display?



    1 4 a


    this does the same:
    printf("%s %s %s", argv[1], argv[2], argv[3]);

    1 4 a
    Tuesday, June 24, 2008 12:08 AM
  •  

    Quote>this does the same:

    Then there's clearly something amiss with your environment.

     

    Do you have any options set to use UNICODE or Multi-Byte characters?

    If so, turn them all off, do a rebuild and try again.

     

    - Wayne

     

    Tuesday, June 24, 2008 12:35 AM
  •  

    Note that if Unicode character set is chosen, then this program:

     

    Code Snippet

     

    int _tmain (int argc, _TCHAR **argv )
    {
     printf("\n%s %s %s\n", argv[1], argv[2], argv[3]);
     return 0;
    }
     

     

     

    will output

     

    1 4 a

     

    while this program

     

    Code Snippet

     

    int main (int argc,char **argv )
    {
     printf("\n%s %s %s\n", argv[1], argv[2], argv[3]);
     return 0;
    }

     

     

     

    will output:

     

    123 456 abc

     

    If Unicode is NOT set, both programs will output:

     

    123 456 abc


    - Wayne

     

    Tuesday, June 24, 2008 12:56 AM
  •  

    One more example:

     

    This program will output 123 456 abc whether built with UNICODE set or not:

     

    Code Snippet

     

    int _tmain (int argc, _TCHAR **argv )
    {
    #ifdef _UNICODE
    wprintf(L"\n%s %s %s\n", argv[1], argv[2], argv[3]);
    #else
    printf("\n%s %s %s\n", argv[1], argv[2], argv[3]);
    #endif
     return 0;
    }

     

     

    - Wayne

     

     

    Tuesday, June 24, 2008 1:20 AM
  • thankyou for your time!!

    The Visual Studio 2005 C++ wizard used

    int _tmain()

    instead of

    int main()


    after I replaced "_tmain" with "main" everything worked as expected.
    Tuesday, June 24, 2008 7:24 AM
  •  

    Glad to hear you got it working. For the sake of completeness, here is one
    additional example which will output 123 456 abc whether built with UNICODE
    set or not. It does essentially the same as my last example, but with a little
    less coding:

     

    Code Snippet

     

    int _tmain (int argc, _TCHAR **argv )
    {
    _tprintf(_T("\n%s %s %s\n"), argv[1], argv[2], argv[3]);
     return 0;
    }

     

     

    - Wayne

    Tuesday, June 24, 2008 9:03 AM
  • Thank you all,

    you have been a great help in freshening up my C vs C++ knowhow.

    May life treat you well!

    Seppe
    Tuesday, June 24, 2008 2:44 PM