What are Command Line Arguments?
When running a C program, you can pass additional data directly from the terminal or command prompt. These inputs are known as command line arguments. They allow you to provide input without modifying the source code or using scanf() or gets().
For example:
./myprogram Sanfoundry 123
Here, Sanfoundry and 123 are command line arguments passed to the program myprogram.
How Do Command Line Arguments Work in C?
In C, command line arguments are handled through the main() function’s parameters:
int main(int argc, char *argv[])
Parameters Explained:
- argc (Argument Count): An integer representing the total number of command-line arguments passed, including the program name.
- argv[] (Argument Vector): An array of character pointers (strings) that hold each argument passed to the program.
- argv[0] is the name of the program itself.
- argv[1], argv[2], …, argv[argc-1] are the actual arguments passed.
Command Line Arguments Examples
Example 1:
#include <stdio.h> int main(int argc, char *argv[]) { printf("Total Arguments: %d\n", argc); for (int i = 0; i < argc; i++) { printf("Argument %d: %s\n", i, argv[i]); } return 0; }
Sample Terminal Execution:
$ ./program Hello World
Output:
Total Arguments: 3 Argument 0: ./program Argument 1: Hello Argument 2: World
This program shows how to use command-line arguments in C. It prints the total number of arguments using argc and then displays each argument using a loop with argv. The first argument is always the program’s name. It’s a simple way to take input from the command line.
Example 2: Converting String Arguments to Numbers
#include <stdio.h> #include <stdlib.h> int main(int arg_count, char *arg_values[]) { if (arg_count != 3) { printf("Usage: %s value1 value2\n", arg_values[0]); return 1; } int num1 = atoi(arg_values[1]); int num2 = atoi(arg_values[2]); printf("Total: %d\n", num1 + num2); return 0; }
Sample Output if Run Like This:
$ ./calculate 25 75
Output:
Total: 100
This C program adds two numbers given through the command line. First, it checks if exactly two values are provided. If not, it prints a message showing the correct way to run the program. Then, it converts the input strings to numbers using atoi() and adds them. Finally, it prints the result. It’s a simple and clear example of how to use command-line arguments in C.
Common Mistakes to Avoid
- Accessing argv[] beyond argc: This causes undefined behavior. Always check argc before accessing arguments.
- Using atoi() without validation: atoi() doesn’t handle invalid input well. Prefer strtol() for safer conversion.
- Forgetting that all arguments are strings: You must convert them to the appropriate type (int, float, etc.).
Advantages of Using Command Line Arguments
- Dynamic Input: Users can give input while running the program. There’s no need to change the code.
- Works with Automation: Useful for scripts and tasks that run by themselves. Helps in coding and deployment.
- Easy Testing: You can test the program with many inputs. No need to change the code each time.
- No Need for Prompts: Good for places where user input isn’t possible. You don’t need scanf() or a keyboard.
- Used in Real Projects: Common in tools like compilers, version control, and network tools. It makes your program look more pro.
Common Use Cases
- Calculator Programs: Take numbers and operations as arguments to perform calculations.
- File Handling: Pass file names or paths to read/write data (e.g., ./fileReader input.txt).
- Test Automation: Supply test parameters for scripts or programs during testing.
- Configuration Settings: Set options like modes, flags, or configuration paths at runtime.
- Network Utilities: Pass IP addresses, ports, or URLs to network-based tools (like ping, wget, etc.).
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Apply for C Internship
- Practice BCA MCQs
- Practice Computer Science MCQs