hey all :)

I'm making a console program, and I want to do that when you run it from the terminal, you can give arguments like -debug (./program -debug). How do i get whether an argument is set or not in the code? i've tried #ifdef debug, but it didn't work.
Btw, I'm using g++ compiler (just in case it was different from compiler to compiler)

8 Years
Discussion Span
Last Post by ulrik04

Command line arguments are passed as parameters to main. The first parameter gives you the number of arguments (including the program name) and the second gives you an array of strings containing the actual arguments:

#include <iostream>

int main ( int argc, char *argv[] )
  // The first argument is the program name, if available
  if ( argc > 1 ) {
    for ( int i = 1; i < argc; i++ )
      std::cout<< argv[i] <<'\n';
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.