Results 1 to 7 of 7

Thread: Why is this.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Posts
    143
    Can someone tell me what this means.
    I see it in alot of programs.
    The part I am confused on is the
    if((argc<2) || (argc>3))
    { proceed w. code

    Why do they put that at the beginning of code ?

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <conio.h>
    void main(int argc, char *argv[])
    
    
        {
        unsigned char car, dump;
        FILE *in, *out;
        long int i, perc, cont;
        i=1;
        perc=1;
        cont=0;
        if ((argc<2) || (argc>3))
    
    
            	{
    Thanks

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It means that argc must be either 2 or 3.

    || == Logical OR
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Guest

    Cool

    A high level answer is that they are testing to see if 2 and only 2 parameters were passed in on the command line to start the app.

    app arg1 arg2

    as opposed to:
    app
    app arg1
    app arg1 arg2 arg3
    app arg1 arg2 arg3 argN etc.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It's >3 so 3 is acceptable...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Posts
    143
    See I understand all of that,
    What I dont understand is what arguments are passed at the beginning.,

    I never knew arguments where passed right when the main function is hit.

    The user has no controll over those arguments , so what is the point of them.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Ah...but the user does

    argv[0] is always the location of the program. The rest of the array up to index (argc-1) are the arguments passed on the command line:
    Code:
    myprog arg1 arg2
    argv[0] = "c:\programs\myprog.exe"
    argv[1] = "arg1"
    argv[2] = "arg2"

    argc = 3
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    Guest

    Question

    Yeah, I miscounted. They want either:
    app arg1
    or
    app arg1 arg2

    Is that right parksie?

    Help
    The user has no controll over those arguments , so what is the point of them.

    The user enters either "arg1" or "arg1 arg2" when launching "app".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width