Easy Tutorial: Computer Programming for DUMMIES -- command line arguments

in #programming8 years ago (edited)


Image source

In this tutorial, I will show you what command line arguments are and how to implement them in a C or C++ program. If you want to check out my previous tutorials in this series, here they are:

Part 1: Hello World!

Part 2: Variables

Part 3: Functions

Part 4: if(), else, else if()

Part 5: Loops

Part 6: Arrays

Part 7: Basic Input/Output

Part 9: Sorting Arrays

Part 10: Random Numbers

Part 11: Colored Text in your Terminal

Part 12: Recursion

Part 13: Binary Search

Part 14: 2D Arrays

Part 15: String Processing

Part 16: Binary, Bitwise Operators, and 2^n

Part 17: Pointers

Part 18: Pointer Arithmetic

Part 19: Object Oriented Programming 1 -- Data Structures

Part 20: Object Oriented Programming 2 -- Classes

Part 21: Object Oriented Programming 3 -- Polymorphism

What are command line arguments?

Command line arguments are arguments that you can give to certain programs before they run. They are placed after the command to run an executable. For Linux, the command to run an executable is "./"

Here is an example, running an executable called a.out with two command line arguments:

./a.out 4 6
  • the arguments are 4 and 6

Implementing this in a C program

So how do we tell our program that it should be expecting command line arguments? We just have to include a few arguments when main() is declared.

Here is what it looks like:

int main(int argc, char* argv[])
{
    return 0;
}
  • argc and argv are standard names for these arguments
  • argc --> an integer that holds the number of command line arguments
    • in our example, argc would equal 3
      1: ./a.out
      2: 4
      3: 6
  • argv --> a string array that holds the arguments in the form of a string
    • argv[0] = "./a.out"
    • argv[1] = "4"
    • argv[2] = "6"

Why is it important to have argc? We need to know how many arguments there are, because if we call argv[3] (or a larger value) in our example, we would get an error.

Here is a program I wrote to to implement command line arguments:

#include<stdio.h>

int main(int argc, char* argv[])
{
    int x;
    for(x = 0; x < argc; x++)
        printf("Argument %d is: %s\n", x, argv[x]);
    return 0;
}

Here is the output:

[cmw4026@omega test]$ gcc arguments.c
[cmw4026@omega test]$ ./a.out 5 2 banana 69 more arguments
Argument 0 is: ./a.out
Argument 1 is: 5
Argument 2 is: 2
Argument 3 is: banana
Argument 4 is: 69
Argument 5 is: more
Argument 6 is: arguments
[cmw4026@omega test]$

Here is another program I wrote. This shows you how to safely make sure a program does not execute unless it has a certain amount of arguments. First, it makes sure that only two arguments were included (besides the command to run the executable), then it adds them together and prints the value.

#include<stdio.h>

int main(int argc, char* argv[])
{
    if(argc < 3 || argc > 3)
    {
        printf("usage: ./a.out number1 number2\n");
        return 0;
    }

    int num1 = atoi(argv[1]);
    int num2 = atoi(argv[2]);

    printf("The two arguments added together equals: %d", num1 + num2);
    printf("\n");

    return 0;
}

Here is some sample output:

[cmw4026@omega test]$ gcc arguments2.c
[cmw4026@omega test]$ ./a.out
usage: ./a.out number1 number2
[cmw4026@omega test]$ ./a.out 7
usage: ./a.out number1 number2
[cmw4026@omega test]$ ./a.out 7 5
The two arguments added together equals: 12
[cmw4026@omega test]$ ./a.out 50 25
The two arguments added together equals: 75
[cmw4026@omega test]$ ./a.out 50 25 100
usage: ./a.out number1 number2
[cmw4026@omega test]$
  • If the wrong amount of command line arguments was given, a usage statement was displayed.

I hope that was helpful! Leave any suggestions in the comments.


Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.028
BTC 57709.05
ETH 3100.20
USDT 1.00
SBD 2.33