devxlogo

Command Line Arguments

Command Line Arguments

Question:
I have a small console program that requires I pass arguments for it to run specific functions. I want to change the code so it automatically runs a set of command switches but cannot figure out how to do so. If I do

argv[0] = "-a -b -c" 

I get all kinds of errors.

Answer:
I don’t know what your code is doing but argv[0] should produce a valid string. The code snippet below iterates through the list of command line arguments. Perhaps you can use this as a starting point to get the information you need. Note that argv[0], the first argument, is always the name of the executable file.

#include int main(int argc, char* argv[]){   int i;   for (i = 0; i < argc; i++) {      printf("Arg %d = %s
", i, argv[i]);   }   return 0;}

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist