F.Holzne
F.Holzne

Reputation: 19

How to provide parameters with command Line

i'm trying to deliver parameters for a program with the command line. I want, that the program is working as shown now: - start the program with parameter "program.exe " - then the should be useable in the programm How can i approach this thing?

Here is the essential part of my programm:

int main(){
int length;
unsigned int i=0;
length=strlen(word);
for(i=0;i<length;i++) {
       printf("%d",word[i]);
       }
}

And i wanted to add this word[] parameter via command line. Thanks!

Upvotes: 0

Views: 92

Answers (2)

Jeegar Patel
Jeegar Patel

Reputation: 27210

For command line arguments Use argv and argc

#include <stdio.h>
#include <stdlib.h>

int main( int argc, char* argv[] )
{
int i;
printf("argc is %d\n",argc);
for(i = 1; i < argc ; i++){
        printf("%d \n", atoi(argv[i]));
}

}

Run your program as

./a.out 10 20 30
argc is 4
10 
20 
30 

Upvotes: 1

benbrb
benbrb

Reputation: 11

int main( int argc, char* argv[] ) {
    return 0;
}
  1. argc => argument count / command line parameter count
  2. argv[x] => argument value / parameter text at position

Upvotes: 1

Related Questions