Command Line Arguments - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

Command Line Arguments

Description:

This is how arguments can be passed in at the command line. int main(int argc, char *argv ... argc and argv ... argv is an array of character strings. ... – PowerPoint PPT presentation

Number of Views:386
Avg rating:3.0/5.0
Slides: 7
Provided by: soonte
Category:
Tags: arguments | argv | command | line

less

Transcript and Presenter's Notes

Title: Command Line Arguments


1
Command Line Arguments
  • Soon Tee Teoh
  • CS 49C

2
The main( ) function
  • So far, we have been defining the main() function
    to receive no arguments.
  • Actually, the main( ) function can receive two
    arguments.
  • To do that, the main( ) function should be
    defined as below.
  • This is how arguments can be passed in at the
    command line.

int main(int argc, char argv )
3
Command Line Arguments
  • From the command prompt, we can start running a
    program by typing its name and pressing ENTER.
  • To pass arguments, we type in the programs name
    followed by some arguments, then press ENTER.
  • Below is an example from the Unix command prompt.

myprog
myprog 5 23
4
argc and argv
  • When the program is called from the command line
    with arguments, argc will contain the number of
    arguments.
  • When the user types in arguments, the user
    separates each argument with a space.
  • argv is an array of character strings.
  • argv1 is the character string containing the
    first argument, argv2 the second, etc.
  • Note argv0 contains the character string that
    is the name of the executable.

5
Example
int main(int argc, char argv) int
i for (i 1 i lt argc i)
printf("s ", argvi) printf("\n")
return 0
6
Using sscanf to get floats and ints
  • Suppose you expect an integer and a floating
    point number as command line arguments.
  • They will be put in argv1 and argv2
    respectively.
  • Use sscanf to get their values.

int main(int argc, char argv) int
n float f sscanf(argv1,
d, n) sscanf(argv2, f, f)
Write a Comment
User Comments (0)
About PowerShow.com