0

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!

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
F.Holzne
  • 19
  • 3
  • 1
    Take a look on it : [what-does-int-argc-char-argv-mean](http://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean) – Missu Sep 28 '15 at 09:52
  • Or for plain C look here: http://stackoverflow.com/q/3734111/694576 – alk Sep 28 '15 at 10:24

2 Answers2

1

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 
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
  • Thanks, is this function also useable for only one argument? – F.Holzne Sep 28 '15 at 10:01
  • Thanks in stackoverflow is Upvote or Accepting Answer !! yes it will be usable for one argument also – Jeegar Patel Sep 28 '15 at 10:04
  • What should be the expected output, please? – alk Sep 28 '15 at 10:10
  • Also `i` is undefined. – alk Sep 28 '15 at 10:10
  • The output should make the word to an number .. ascii codes .. the "a" should be converted into a "97" .. – F.Holzne Sep 28 '15 at 10:12
  • "*The output should make the word to an number*" the code definitely does *not do this*. The code you show takes the pointer to each of the arguments passed on the command-line and subtracts `'0'` and prints it. Better test your code before posting it to the world. – alk Sep 28 '15 at 10:14
1
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
benbrb
  • 11
  • 2