C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • Using Command Line Arguments
  • Example: Printing Command Line Arguments
  • Running the Program
  • Conclusion

Was this helpful?

  1. Basic Concepts

Command Line Arguments

Command line arguments allow a C program to accept inputs directly from the command line when it is executed. These arguments provide a flexible way to pass information to a program without requiring user interaction during runtime.

Using Command Line Arguments

In C, the main function can receive command line arguments through its parameters. The standard signature for the main function that accepts command line arguments is:

int main(int argc, char *argv[]) {
    // Code that uses command line arguments
    return 0;
}

Here, argc (argument count) represents the number of command line arguments, and argv (argument vector) is an array of strings containing the actual arguments.

Example: Printing Command Line Arguments

Let's create a simple program that prints the command line arguments.

#include <stdio.h>

int main(int argc, char *argv[]) {
    // Check if there are any command line arguments
    if (argc > 1) {
        // Print each argument
        for (int i = 1; i < argc; i++) {
            printf("Argument %d: %s\n", i, argv[i]);
        }
    } else {
        printf("No command line arguments provided.\n");
    }

    return 0;
}

In this example, the program checks if there are any command line arguments. If arguments are present, it iterates through them and prints each one along with its position.

Running the Program

To run the program with command line arguments, compile it and provide the arguments after the executable name.

./program_name arg1 arg2 arg3

In the above command, program_name is the name of the compiled executable, and arg1, arg2, arg3 are the command line arguments.

Conclusion

Command line arguments provide a means for C programs to receive input directly from the command line, enabling more dynamic and versatile interactions. This capability is especially useful for scripts, utilities, and applications that need to adapt to different scenarios based on user inputs.

In the upcoming sections, we'll explore more advanced topics in C programming. If you have specific questions or areas you'd like to delve into further, feel free to ask. Happy coding!

PreviousFormat SpecifiersNextProgram: Calculating the Area of a Triangle

Was this helpful?