Click to See Complete Forum and Search --> : Why is this.
Help
Mar 9th, 2001, 10:03 AM
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 ?
#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
parksie
Mar 9th, 2001, 11:59 AM
It means that argc must be either 2 or 3.
|| == Logical OR
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.
parksie
Mar 9th, 2001, 01:30 PM
It's >3 so 3 is acceptable... ;)
Help
Mar 9th, 2001, 01:58 PM
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.
parksie
Mar 9th, 2001, 02:14 PM
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:
myprog arg1 arg2
argv[0] = "c:\programs\myprog.exe"
argv[1] = "arg1"
argv[2] = "arg2"
argc = 3
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".
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.