0

So I'm confused on how exactly command line arguments work in C... so I have these command line arguments that I'm giving:

 ./myclient1 MyPCName 12 7894 

So I want to read argv[2] (12) as a String... but I'm confused exactly how values are stored in the command line. I looked at both this SO post,this this link but I'm still confused... what is the data type of argv[2]? Is it an integer? Or are all command line arguments originally strings? So argv[2] is actually:

  argv: 
  [0]
  [1]
  [2] --> 1 | 2 | \0 

I'm just really confused.... currently, I just converted to an integer using atoi() and then converted back to a string using snprintf, but it's not working correctly and I'm wondering if I need to do this at all.

I'm new to C so any help would be greatly appreciated, thanks!!

[edit]

this is what I had done before:

int main(int argc, char *argv[])
{
 clientID = atoi(argv[2]);
snprintf(clibuff,300,"%d",clientID);  //now clibuff has the value of 
                                      //clientID in a string.
}
Community
  • 1
  • 1
ocean800
  • 3,489
  • 13
  • 41
  • 73

5 Answers5

3

Command line arguments are stored as an array of zero-terminated strings.

So to answer your question, the type of argv[2] is char *

The type of argv is char **

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • Oh... thanks I see. So then my understanding was largely correct. So then `12` is stored as a string with an array of `char`.... – ocean800 Sep 28 '15 at 20:44
1

A hosted C implementation (such as you surely are using) permits exactly two signatures for main(), and if you want access to the command line arguments then only one will do:

int main(int argc, char *argv[]);

You can spell it in couple of other, equivalent ways, but I prefer that one because it emphasizes the array nature of argv. Each element, of course, points to one of the first character of a standard C string containing the corresponding command-line argument. Thus, the type of argv[2] is always char *, and you don't need to do anything special to handle it as a C string.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
1

a copy of your test code would be useful..

I usually declare argv this way:

int main(int argc, char **argv);

When the program starts, the arguments to main will have been initialized to this:

  • argc is greater than zero.
  • argv[argc] is a null pointer.
  • argv[0] to argv[argc-1] are pointers to strings
  • argv[0] will be a string containing the program's name or a null string if that is not available. Remaining elements of argv represent the arguments supplied to the program.

This Testprogram should work:

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

int main(int argc, char **argv)
{
        while(argc--)
                printf("%s\n", *argv++);
        exit(EXIT_SUCCESS);
}
Tom
  • 357
  • 1
  • 5
  • 18
0

The argv[0] is always the name of your program, and yes all your arguments are string you should count your arguments since the argv[1].

Sebas
  • 324
  • 2
  • 10