C Program to Implement Command Line Arguments

C Program to Implement Command Line Arguments

Command line arguments in C are a way to provide input to a program when you run it, instead of asking the user to enter input during execution. This approach makes programs more flexible and convenient, especially when automating tasks or working with scripts. By using command line arguments, you can pass values directly to your program, which can then process these values without any additional input prompts.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Understanding command line arguments is essential for beginners because it introduces how programs interact with the operating system and how input can be handled in a more dynamic way. They are widely used in real-world applications, such as file processing, automation scripts, and programs that need to accept multiple input parameters efficiently. In this article, we will explore beginner-friendly examples of how to implement command line arguments in C.

How Command Line Arguments Are Passed

When you run a program from a terminal like this:

./program Edward 25 3.14

The operating system automatically passes:

  • argc = 4 → because there are four items: the program name ./program, "Edward", "25", and "3.14".
  • argv[0] = "./program" → the program’s name.
  • argv[1] = "Edward" → first user input.
  • argv[2] = "25" → second input.
  • argv[3] = "3.14" → third input.

This mechanism lets the program know exactly what values were entered at launch and in what order, without prompting the user. Every argument is stored as a string, so numeric values must be converted to the appropriate type using functions like atoi() (integer) or atof() (float/double).

Program 1: Display Command Line Arguments

This program demonstrates how to read and display all command line arguments provided to a C program. It’s the simplest way to get started with this concept.

#include <stdio.h>

int main(int argc, char *argv[]) {

    printf("Number of arguments: %d\n", argc);

    for(int i = 0; i < argc; i++) {
        printf("Argument %d: %s\n", i, argv[i]);
    }

    return 0;

}

In this program, argc (argument count) tells you how many arguments were passed, including the program name itself, and argv (argument vector) is an array of strings representing each argument. Beginners can see how the program captures all inputs provided at the command line, which is useful for programs that need multiple input values or configuration options.

Program 2: Sum of Numbers Using Command Line Arguments

This program demonstrates how to perform a calculation using numbers provided as command line arguments.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {

    if(argc < 3) {

        printf("Usage: %s num1 num2 ...\n", argv[0]);
        return 1;

    }

    int sum = 0;

    for(int i = 1; i < argc; i++) {
        sum += atoi(argv[i]);
    }

    printf("Sum of numbers: %d\n", sum);

    return 0;

}

Here, atoi() (ASCII to integer) converts each command line argument from a string to an integer. By starting the loop from i = 1, we skip the program name in argv[0]. This program helps beginners understand how to process numeric inputs from the command line and perform calculations, demonstrating the practical use of command line arguments in real-world programs.

Program 3: Greeting User Using Command Line Arguments

This program shows how to customize program behavior based on user input provided at runtime.

#include <stdio.h>

int main(int argc, char *argv[]) {

    if(argc != 2) {

        printf("Usage: %s <name>\n", argv[0]);
        return 1;

    }

    printf("Hello, %s! Welcome to C programming.\n", argv[1]);

    return 0;

}

In this example, the program greets the user using the name provided as a command line argument. Beginners can see how command line arguments can be used to personalize program behavior and make programs more interactive without requiring manual input prompts during execution.

Frequently Asked Questions (FAQ)

Here are some common questions beginners have about command line arguments in C.

Q1. What are argc and argv?
argc is the number of arguments passed, including the program name, and argv is an array of strings containing all arguments.

Q2. Can command line arguments handle numbers and strings?
Yes, but all arguments are received as strings. Functions like atoi() or atof() can convert strings to numbers.

Q3. Why start loops from i = 1 sometimes?
argv[0] always contains the program name, so starting from 1 skips it when processing user input.

Q4. What happens if no arguments are provided?
If no additional arguments are provided, argc will be 1, indicating only the program name is available.

Q5. Are command line arguments optional?
Yes, programs can check argc and handle cases when no arguments are provided to avoid errors.

Conclusion

Command line arguments are a powerful way to make C programs flexible and user-friendly. By using argc and argv, beginners can create programs that accept dynamic input directly from the terminal, perform calculations, or customize behavior based on user input. Practicing these programs—from displaying arguments to summing numbers or greeting users—helps build a solid understanding of how programs interact with the operating system and handle inputs efficiently.

Additional & References

For beginners who want to explore command line arguments and input handling in C further, the following resources are very useful:

  1. C Standard Library (stdlib.h) – Documentation for functions like atoi() and atof(), essential for processing numeric command line arguments.
  2. Programiz C Tutorials – Beginner-friendly tutorials with hands-on examples for practicing arithmetic and geometry programs.
  3. GeeksforGeeks C Programming Section – Detailed explanations, sample programs, and exercises for reinforcing core concepts.
  4. Learn-C.org – Interactive platform to write and test C programs online, ideal for experimenting with volume calculations.
Scroll to Top