Command-line arguments - PowerPoint PPT Presentation

About This Presentation
Title:

Command-line arguments

Description:

Command-line arguments CS 201 Fundamental Structures of Computer Science – PowerPoint PPT presentation

Number of Views:149
Avg rating:3.0/5.0
Slides: 10
Provided by: CGD9
Category:

less

Transcript and Presenter's Notes

Title: Command-line arguments


1
Command-line arguments
  • CS 201 Fundamental Structures of Computer Science

2
Introduction
  • A command-line argument is the information that
    follows the name of the program on the command
    line of the operating system
  • Command-line arguments are used to pass
    information into a program when you run it
  • They facilitate the use of your program in batch
    files
  • They give a professional appearance to your
    program

3
Introduction
  • C defines two built-in parameters to main()
  • They receive the command line arguments
  • Their names are argc and argv
  • The names of the parameters are arbitrary.
    However, argc and argv have been used by
    convention for several years.
  • They are optional

4
int main( int argc, char argv )
  • argc is an integer
  • Holds the number of arguments on the command line
  • Since the name of the program always corresponds
    to the first argument, it is always at least 1

5
int main( int argc, char argv )
  • argv is a pointer to an array of character
    pointers.
  • Each character pointer in the argv array
    corresponds a string containing a command-line
    argument
  • argv0 points the name of the program, argv1
    points to the first argument, argv2 points to
    the second argument,
  • Each command-line argument is a string
  • If you want to pass numerical information to your
    program, your program should convert the
    corresponding argument into its numerical
    equivalent
  • Each command-line argument must be separated by
    spaces or tabs
  • Commas, semicolons, and the like are not valid
    argument separators

6
  • include ltiostreamgt
  • using namespace std
  • int main( int argc, char argv )
  • cout ltlt "Hello "
  • for (int i 1 i lt argc i)
  • cout ltlt argvi ltlt " "
  • cout ltlt "! " ltlt endl
  • return 0

cgunduz_at_knuth cgunduz g prog1.cpp o
exe_1 cgunduz_at_knuth cgunduz ./exe_1 Cigdem
Gunduz Hello Cigdem Gunduz ! cgunduz_at_knuth
cgunduz ./exe_1 Cigdem Gunduz Demir Hello
Cigdem Gunduz Demir !
7
Passing numeric command-line arguments
  • All command-line arguments are passed to the
    program as strings
  • Your program should convert them into their
    proper internal format
  • For that, C supports standard library functions
    most commonly used ones are
  • atof() converts a string to a double and
    returns the result
  • atoi() converts a string to a int and returns
    the result
  • atol() converts a string to a long int and
    returns the result
  • Each of these functions
  • Expects a string containing a numeric value as an
    argument
  • Uses the header ltcstdlibgt

8
  • include ltiostreamgt
  • include ltcstdlibgt
  • using namespace std
  • int main( int argc, char argv )
  • if (argc ! 4)
  • cout ltlt "Usage \n\t"
  • cout ltlt "1. Integer (0) or double (1)
    division\n\t"
  • cout ltlt "2. Operand 1\n\t3. Operand 2\n\n"
  • exit(1)
  • if (atoi(argv1) 0)
  • int a atoi(argv2)
  • int b atoi(argv3)
  • cout ltlt a ltlt "\\" ltlt b ltlt " " ltlt a / b ltlt
    endl
  • else
  • double a atof(argv2)
  • double b atof(argv3)

9
cgunduz_at_knuth cgunduz g prog2.cpp -o
exe_2 cgunduz_at_knuth cgunduz ./exe_2 Usage
1. Integer (0) or double (1) division
2. Operand 1 3. Operand 2 cgunduz_at_knuth
cgunduz ./exe_2 0 5 3 5\3
1 cgunduz_at_knuth cgunduz ./exe_2 1 5
3 5\3 1.66667 cgunduz_at_knuth cgunduz ./exe_2
0 8.2 2.9 8\2 4 cgunduz_at_knuth cgunduz
./exe_2 1 8.2 2.9 8.2\2.9 2.82759
Write a Comment
User Comments (0)
About PowerShow.com