CommandLine Arguments in UnixC - PowerPoint PPT Presentation

About This Presentation
Title:

CommandLine Arguments in UnixC

Description:

The Use of argc and argv. A program that does not allow the use of command line arguments: ... The Use of argc and argv (cont'd) argc: a count of the number of ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 8
Provided by: dUmn
Learn more at: https://www.d.umn.edu
Category:

less

Transcript and Presenter's Notes

Title: CommandLine Arguments in UnixC


1
Command-Line Arguments in Unix/C
To simplify the initial implementation, we will
assume that getBoardSize will use command line
arguments. Recall 13 knight 6
. . ltknight tour output heregt
. 14
2
The Use of argc and argv
  • A program that does not allow the use of command
    line arguments

main () // statements
A program that does
main (int argc, char argv) //
statements
3
The Use of argc and argv (cont'd)
argc a count of the number of elements
provided on the command line argv a vector
(array) of pointers to char (strings)
representing the command line elements Suppose
main is the main program in command.cc and it
is compiled and run with the following 381
g command.cc -o command 382 command foo bar
baz
4
The Use of argc and argv (cont'd)
Then when main begins, argc and argv are as
follows
4
argv
argc
c
o
m
mmmo
a
n
d
\0
f
o
o
\0
b
a
r
\0
b
a
z
\0
5
Example Using argc and argv
// Demonstrate the use of command-line
arguments include ltiostream.hgt main(int argc,
char argv) int i cout ltlt "The number
of elements on the command line " ltlt
argc ltlt endl cout ltlt "The name of the program
" ltlt argv0 ltlt endl for (i 1 i lt argc
i) cout ltlt "Command line argument number "
ltlt i ltlt " " ltlt argvi ltlt endl
6
Example Using argc and argv (cont'd)
381 g cmdline.cc -o cmdline 382 cmdline foo
bar baz The number of elements on the command
line 4 The name of the program cmdline Command
line argument number 1 foo Command line argument
number 2 bar Command line argument number 3
baz 383 cmdline knight 6 The number of elements
on the command line 3 The name of the program
cmdline Command line argument number 1
knight Command line argument number 2 6 384
7
An Equivalent Usage
  • Since
  • argv is of type char
  • and since arrays in C/C are pointers, then
  • argv is of type char
  • Therefore
  • void Mainprocess(int argc, char argv)
  • is equivalent to
  • void Mainprocess(int argc, char argv)
  • Note that the following is an optimization
  • void Mainprocess(int argc, const char
    argv)
Write a Comment
User Comments (0)
About PowerShow.com