Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 4 — Command-Line Arguments

Lesson 23 of 32 in the free C Language notes on Siksha Sarovar, written by Rohit Jangra.

Command-Line Arguments in C

C programs can accept arguments from the command line using argc and argv parameters of the main function.

---

Syntax

int main(int argc, char *argv[]) {
    /* argc = argument count */
    /* argv = argument vector (array of strings) */
}
ParameterDescription
argcNumber of arguments (including program name)
argv[0]Name of the program
argv[1]First argument
argv[2]Second argument
argv[argc-1]Last argument

---

Example 1: Print All Arguments

#include <stdio.h>

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

Running:

./program hello world 123

Output:

Number of arguments: 4
argv[0] = ./program
argv[1] = hello
argv[2] = world
argv[3] = 123

---

Example 2: Add Two Numbers from Command Line

#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 a = atoi(argv[1]);
    int b = atoi(argv[2]);
    printf("Sum = %d\n", a + b);
    
    return 0;
}

Running:

./add 5 7
Sum = 12

---

Example 3: File Processing

#include <stdio.h>

int main(int argc, char *argv[]) {
    if (argc < 2) {
        printf("Usage: %s filename\n", argv[0]);
        return 1;
    }
    
    FILE *fp = fopen(argv[1], "r");
    if (!fp) {
        printf("Cannot open: %s\n", argv[1]);
        return 1;
    }
    
    char line[200];
    while (fgets(line, sizeof(line), fp)) {
        printf("%s", line);
    }
    fclose(fp);
    return 0;
}

---

Notes

  • All command-line arguments are received as strings (char *)
  • Use atoi(), atof(), atol() from stdlib.h to convert to numbers
  • Always validate argc before accessing argv to avoid out-of-bounds access