Lesson 01.5beginnerFoundation

Command-Line Arguments

Using argc and argv to accept command-line arguments. Includes a practical example that opens a file specified on the command line with proper error handling using perror and EXIT_FAILURE.

argcargvperrorerror-handling
Prerequisites:01. Introduction to C
1

Basic argc/argv

Printing command-line arguments by iterating over argv.

argc_argv.c34 lines
/*
 * argc_argv.c — Basic argc/argv — command line arguments
 *
 * Key concepts: argc, argv, main parameters
 * Compile: gcc -o prog argc_argv.c
 * Run:     ./prog arg1 arg2
 */
#include <stdio.h>


/*
we need to run this program using the terminal in order to pass it some arguments
but first, lets run it as usual.
*/

int main(int argc, char *argv[]) {
    printf("\n");
    printf("========================================\n");
    printf("  Command-Line Arguments (argc/argv)\n");
    printf("========================================\n\n");
    printf("  argc (argument count): %d\n\n", argc);
    printf("  %-6s | %s\n", "Index", "Value");
    printf("  ------ | -----\n");
    for (int i = 0; i < argc; i++) {
        printf("  argv[%d] | \"%s\"%s\n", i, argv[i], i == 0 ? "  (program name)" : "");
    }
    printf("\n");
    if (argc == 1) {
        printf("  TIP: Try running with arguments:\n");
        printf("    ./prog hello world 42\n\n");
    }
    printf("========================================\n");
    return 0;
}
Expected Outputclick Run to execute live
========================================
  Command-Line Arguments (argc/argv)
========================================

  argc (argument count): 1

  Index  | Value
  ------ | -----
  argv[0] | "./argc_argv"  (program name)

  TIP: Try running with arguments:
    ./prog hello world 42

========================================
2

File Open with Validation

Opening a file passed via command line, with perror for error messages and EXIT_FAILURE for clean exits.

argc_argv_another_example.c58 lines
/*
 * argc_argv_another_example.c — Opening a file from command line arguments with validation
 *
 * Key concepts: argc/argv, fopen, perror, EXIT_FAILURE
 * Compile: gcc -o prog argc_argv_another_example.c
 * Run:     ./prog arg1 arg2
 */
#include <stdio.h>
#include <stdlib.h>


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

// lets say we are expecting in our program
// to always get a name of a file
// so we can open it and read from it
// we MUST check for argc/argv size
// if we dont, we might get a segfault




int main(int argc, char *argv[]) {
    const int ARGUMENTS_COUNT = 2; 
    if (argc != ARGUMENTS_COUNT) { // if argc is not 2
        perror("Usage: ./a.out <filename>"); // we show the user how to use the program
        return EXIT_FAILURE; // and we return an error
    }
    //here we can continue with the program

    const char* filename = argv[1];

    printf("\n");
    printf("========================================\n");
    printf("  Reading a File from Command Line\n");
    printf("========================================\n\n");
    printf("  Opening file: \"%s\"\n\n", filename);

    FILE *file = fopen(filename, "r");
    if (file == NULL) {
        perror("  ERROR: Cannot open file");
        return EXIT_FAILURE;
    }

    // Read from the file
    printf("  --- File contents ---\n\n");
    char buffer[100];
    while (fgets(buffer, sizeof(buffer), file) != NULL) {
        printf("    %s", buffer);
    }
    printf("\n\n  --- End of file ---\n\n");
    printf("========================================\n");



    return 0;
}
Expected Outputclick Run to execute live
Usage: ./a.out <filename>: Success

Open Full Terminal

Get a Linux terminal with all 2 files from this lesson pre-loaded. Compile, run, and experiment freely.