-1

I was doing my programs Python, so not much familiar with C. I am doing a program on creating a binary tree in C. I am using an "insert" function created by me . The goal is that for typing "insert " in command line, the function should work. But I have not much idea regarding how to get and parse command line arguments in C. Can anyone help?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • http://www.cprogramming.com/tutorial/c/lesson14.html – akonsu Mar 01 '14 at 03:26
  • there are quite a few examples doing a quick [google search](https://www.google.com/search?q=command+line+c+simple+example&oq=command+line+c+simple+example) – mateuscb Mar 01 '14 at 03:48

3 Answers3

0

http://www.cprogramming.com/tutorial/c/lesson14.html

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

This should be the declaration of your main function. argc is the amount of arguments. argv[] is an array that contains each command-line argument as a string, the program name is argv[0] so the first argument will be argv[1]. I'm not a C programmer, so this might not be good info, I highly recommend checking out the link.

Awalrod
  • 268
  • 1
  • 4
  • 14
0

Use a library to handle the low-level details, such as getopt. The code is much more involved than with Python's argparse or getopt, but is similar in concept. (the wikipedia article to which I linked contains example C code using getopt)

dsh
  • 12,037
  • 3
  • 33
  • 51
0

Command line arguments are passed during run time.

You have to specify the number of arguments and also a char pointer to point to these arguments. This is done in main() syntax itself. Void main(int argc, char* argv)

In order to compile and generate an executable in CC compiler, cc -o exec_name program_name.c

In order to run, exec_name arg1 arg2.........

It is to be noted that the exec_name is also considered as an argument

user3256147
  • 378
  • 2
  • 9