The command line arguments with switches/options program example

 

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: none

To do: To list the local computer environment variables using C command line arguments example

To show: The command line arguments with switches/options program example

 

#include <stdio.h>

 

int main(int argc, /* Number of strings in array argv */

char *argv[], /* Array of command-line argument strings */

char **envp) /* Array of environment variable strings */

{

int count;

 

/* Display each command-line argument. */

printf("\nThe command-line arguments:\n");

 

for(count = 0; count < argc; count++)

printf(" argv[%d] %s\n", count, argv[count]);

 

/* Display each environment variable.*/

printf("\nEnvironment variables:\n");

while(*envp != NULL)

printf(" %s\n", *(envp++));

return 0;

}

 

Output example:

(This program run at the command prompt)

 

The command-line arguments:

argv[0] f:\vc2005project\searchpattern\debug\searchpattern.exe

Environment variables:

ALLUSERSPROFILE=C:\Documents and Settings\All Users.WINDOWS

APPDATA=C:\Documents and Settings\Johnny\Application Data

CLIENTNAME=Console

CommonProgramFiles=C:\Program Files\Common Files

COMPUTERNAME=MYPERSONAL

ComSpec=C:\WINDOWS\system32\cmd.exe

FP_NO_HOST_CHECK=NO

HOMEDRIVE=C:

HOMEPATH=\Documents and Settings\Johnny

INCLUDE=c:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\include\

LIB=c:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Lib\

LOGONSERVER=\\MYPERSONAL

NUMBER_OF_PROCESSORS=2

OS=Windows_NT

Path=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Common Files\Adobe\AGL;C:\Prog

ram Files\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\Microsoft Visual Studio 8\;C:\Program Files\Mic

rosoft Visual Studio 8\VC\bin

PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH

PROCESSOR_ARCHITECTURE=x86

PROCESSOR_IDENTIFIER=x86 Family 15 Model 3 Stepping 4, GenuineIntel

PROCESSOR_LEVEL=15

PROCESSOR_REVISION=0304

ProgramFiles=C:\Program Files

PROMPT=$P$G

SESSIONNAME=Console

SystemDrive=C:

SystemRoot=C:\WINDOWS

TEMP=C:\DOCUME~1\Johnny\LOCALS~1\Temp

TMP=C:\DOCUME~1\Johnny\LOCALS~1\Temp

USERDOMAIN=MYPERSONAL

USERNAME=Johnny

USERPROFILE=C:\Documents and Settings\Johnny

VS71COMNTOOLS=c:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\

VS80COMNTOOLS=C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\

WecVersionForRosebud.A30=2

windir=C:\WINDOWS

_ACP_ATLPROV=C:\Program Files\Microsoft Visual Studio 8\VC\Bin\ATLProv.dll

_ACP_INCLUDE=C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include;C:\Program Files\Mic

rosoft Visual Studio 8\VC\include;C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\include;C:\Program

Files\Microsoft Visual Studio 8\SDK\v2.0\include

_ACP_LIB=C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Lib;C:\Program Files\Microsoft Visual

Studio 8\VC\lib;C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\lib;C:\Program Files\Microsoft

Visual Studio 8\SDK\v2.0\lib;C:\Program Files\Microsoft Visual Studio 8\;C:\Program Files\Microsoft Visual Studio 8\lib

_ACP_PATH=C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Bin;C:\Program Files\Microsoft

Visual Studio 8\VC\bin;C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\bin;C:\Program Files\Microsoft

Visual Studio 8\Common7\Tools\bin;C:\Program Files\Microsoft Visual Studio 8\Common7\tools;C:\Program Files\

Microsoft Visual Studio 8\Common7\ide;C:\Program Files\HTML Help Workshop;C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\bin;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;C:\Program Files\Microsoft Visual Studio 8\;

C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Common Files\Adobe\AGL;C:\Program Files\

Microsoft SQL Server\90\Tools\binn\

Press any key to continue . . .

 

 

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