Skip to main content
Engineering LibreTexts

16.2: High-Level Language Example

  • Page ID
    19961
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    In general, it is assumed that the reader is already familiar with the basic C/C++ command line handling. This section presents a brief summary of how the C/C++ language handles the delivery of command line information to the program.

    The count of arguments is passed to the main program as the first integer parameter, typically called argc. The second parameter, typically called argv, is an array of addresses for each string associated with the corresponding argument.

    For example, the following C++ example program will read the command line arguments and display them to the screen.

         #include <iomanip>
         #include <iostream>
         using namespace std;     
         
         int main(int argc, char* argv[])
         {
             string bars; 
             bars.append(50,'-');        
             
             cout << bars << endl;
             cout << "Command Line Arguments Example"                 
                          << endl << endl;       
             
             cout << "Total arguments provided: " <<
                      argc << endl;      
                   
             cout << "The name used to start the program: "
                      << argv[0] << endl;
                      
             if (argc > 1) { 
                 cout << endl << "The arguments are:" << endl;
                 for (int n = 1; n < argc; n++)
                     cout << setw(2) << n << ": " <<
                            argv[n] << endl;
             }
             
             cout << endl;
             cout << bars << endl;
    
             return 0; 
          } 
    

    Assuming the program is named argsExp, executing this program will produce the following output:

        ./argsExp   one 34        three
        --------------------------------------------------
        Command Line Arguments Example    
        
        Total arguments provided: 4
        The name used to start the program: ./argsExp    
        
        The arguments are:
         1: one
         2: 34
         3: three    
         
        --------------------------------------------------
    

    It should be noted that the parameter '34' is a string. This example simply printed the string to the console. If a parameter is to be used as a numeric value, the program must convert as required. This includes all error checking as necessary.


    This page titled 16.2: High-Level Language Example is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Ed Jorgensen.

    • Was this article helpful?