[prev] 67 [next]

Arrays of Strings (cont)

main() needs different prototype if you want to access command-line arguments:

int main(int argc, char *argv[]) { ...

  • argc … stores the number of command-line arguments + 1
    • argc == 1 if no command-line arguments
  • argv[] … stores program name + command-line arguments
    • argv[0] always contains the program name
    • argv[1],argv[2],… are the command-line arguments if supplied
<stdlib.h> defines useful functions to convert strings:
  • atoi(char *s) converts string to int
  • atof(char *s) converts string to double   (can also be assigned to float variable)