A very simple main() function and its command line arguments

 

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: Must be run at command line

To do: Read from standard input and display to the standard output the command lines arguments

To show: The main() function and command line arguments code sample

 

 

// An echo program - a command-line arguments example

#include <stdio.h>

 

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

{

int x;

 

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

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

 

printf("\n");

return 0;

}

 

Output example:

(This program run at the command prompt)

 

F:\vc2005project\myaddr\debug>myaddr

F:\vc2005project\myaddr\debug>myaddr first second

first second

F:\vc2005project\myaddr\debug>myaddr first second command line arguments

first second command line arguments

 

 

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