Question

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?

No correct solution

OTHER TIPS

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.

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)

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top