Echoing back the user input from the standard input to the standard output using command line argument

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: Run at command line

To do: Echoing back the user input from the standard input to the standard output using command line argument

To show: How to use the main() function command line arguments

 

// Program name, myprog.exe

// Echoing the command line argument to standard output

#include <stdio.h>

 

//echo command-line argument using array of character pointers

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

{

int i;

 

if(argc == 1)

printf("you need to supply some arguments!\n");

 

for(i=1; i<argc; i++)

printf("%s%s", argv[i],(i<argc-1)? " ":"");

 

printf("\n");

return 0;

}

 

Output example:

(This program run at the command prompt)

 

C:\amad\myprog\Debug>myprog

you need to supply some arguments!

 

C:\amad\myprog\Debug>myprog testing

testing

C:\amad\myprog\Debug>myprog "echoing some strings"

echoing some strings

C:\amad\myprog\Debug>myprog "echoing another text..."

echoing another text...

C:\amad\myprog\Debug>

 

 

C and C++ Programming Resources | C & C++ Code Example Index