The any argument supplied from the command line and take action accordingly −
#include <stdio.h> #include <conio.h> void main( int argc, char *argv[] ) { clrscr(); if( argc == 2 ) { printf("The argument supplied is %s\n", argv[1]); } else if( argc > 2 ) { printf("Too many arguments supplied.\n"); } else { printf("One argument expected.\n"); } getch(); }
3. Compile and Run your program
When the above code is compiled and executed by Turbo C++, it produces the following result
One argument expected.
4. Open DOS Shell
Now open Turbo C++ File Menu (You can use shortcut Alt+F) and select DOS Shell.
5. Change directory
Now you need to change the current directory of DOS Shell to "SOURCES" from "BIN", for this you need to user
cd command.
First use
cd..for coming back to Turbo C++ main directory
cd..
Now use
cd SOURCEto access the SOURCE directory
cd SOURCE
6. Execute Program with Command Line Arguments
ARGS.EXE testing
Here
ARGS.EXE is the executable of your program
ARGS.C and
testing is a command line argument. When you'll hit the enter, it produces the following result
The argument supplied is testing
