- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Command Line Arguments
![]() Share with a Friend |
C Programming - C Command Line Arguments
Command Line Arguments in C
Command line arguments in C allow you to pass data to your program at the time of execution through the command line. This enables your program to take input directly from the command line, without the need for user interaction during the program's execution.
How Command Line Arguments Work
In C, command line arguments are passed to the main() function through its parameters, which are:
- argc (Argument Count): An integer that represents the number of arguments passed to the program.
- argv (Argument Vector): An array of character pointers (strings) representing the arguments.
The syntax of the main() function when using command line arguments is as follows:
C
int main(int argc, char *argv[])
Where:
- argc: Number of command line arguments, including the name of the program itself.
- argv[]: An array of strings where each string is one of the command line arguments.
How the Arguments Work:
- argv[0] contains the name of the program itself.
- argv[1] to argv[argc-1] contain the actual command line arguments provided by the user.
Example of Using Command Line Arguments:
Here is a simple C program that demonstrates the use of command line arguments:
C
#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;
}
Explanation:
- argc gives the number of arguments (including the program name).
- argv[i] is used to access each command line argument.
- The program prints the number of arguments and then prints each argument.
Example Usage:
If you run the program from the command line like this:
./myprogram arg1 arg2 arg3
The output will be:
Number of arguments: 4
Argument 0: ./myprogram
Argument 1: arg1
Argument 2: arg2
Argument 3: arg3
Command Line Arguments for Practical Usage:
Command line arguments are often used for:
- Passing file names to programs.
- Setting configuration options or flags (e.g., -v for verbose).
- Specifying input data or parameters for computations.
Example: Adding Two Numbers from Command Line:
This example demonstrates how to add two numbers passed as command line arguments:
C
#include <stdio.h>
#include <stdlib.h> // for atoi()
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s <num1> <num2>\n", argv[0]);
return 1;
}
int num1 = atoi(argv[1]); // Convert the first argument to integer
int num2 = atoi(argv[2]); // Convert the second argument to integer
int sum = num1 + num2;
printf("Sum: %d\n", sum);
return 0;
}
Explanation:
- atoi() (ASCII to Integer) is used to convert the string arguments into integers.
- The program expects two arguments representing numbers and calculates their sum.
Usage Example:
./add_numbers 12 30
Output:
makefile
Copy code
Sum: 42
Handling Non-integer Arguments:
If the arguments cannot be converted into integers (e.g., the user enters a non-numeric value), atoi() returns 0, which could lead to incorrect results. To handle such cases, you could use strtol() or manually validate input.
Using strtol() for Better Validation:
C
#include <stdio.h>
#include <stdlib.h> // for strtol()
#include <errno.h> // for errno
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s <num1> <num2>\n", argv[0]);
return 1;
}
char *endptr;
int num1 = strtol(argv[1], &endptr, 10);
if (*endptr != '\0') {
printf("Error: '%s' is not a valid number.\n", argv[1]);
return 1;
}
int num2 = strtol(argv[2], &endptr, 10);
if (*endptr != '\0') {
printf("Error: '%s' is not a valid number.\n", argv[2]);
return 1;
}
int sum = num1 + num2;
printf("Sum: %d\n", sum);
return 0;
}
Explanation:
- strtol() is used to convert the string to a long integer. It also allows error handling: if the input is not a valid number, the endptr will point to the invalid character, which can be checked.
Summary:
- Command line arguments provide a way to pass input to programs at runtime.
- Use argc for the number of arguments and argv[] to access the arguments.
- Argument handling is important for ensuring proper program behavior when user inputs are passed.
Command line arguments make programs more flexible and interactive by allowing them to accept data when run, rather than requiring hard-coded values or manual input.
