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.
}
2
  • Please post some code of what you've tried. Commented Sep 28, 2015 at 20:38
  • 2
    All command line arguments come in as strings. Commented Sep 28, 2015 at 20:39

5 Answers 5

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 **

Sign up to request clarification or add additional context in comments.

1 Comment

Oh... thanks I see. So then my understanding was largely correct. So then 12 is stored as a string with an array of char....
2

Here are some great explanations on CLI arguments:

http://www.tutorialspoint.com/cprogramming/c_command_line_arguments.htm

https://en.wikibooks.org/wiki/A_Little_C_Primer/C_Command_Line_Arguments

Comments

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.

1 Comment

thanks... I think this answer made things much more clearer.
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);
}

Comments

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].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.