Command Line Arguments in C
Most C programs you've written so far receive input from the keyboard using functions like scanf(). While this is useful for interactive programs, many real-world applications receive input directly when the program starts.
For example, imagine running a calculator from the terminal:
bashcalculator 10 20
or copying a file:
bashcopy source.txt destination.txt
The values entered after the program name are called Command Line Arguments.
Command line arguments make programs more flexible and are widely used in automation, scripting, utilities, and system programming.
🎯 Learning Objectives
After completing this lesson, you will be able to:
- Understand command line arguments.
- Learn about
argcandargv. - Pass values from the terminal.
- Access individual arguments.
- Convert string arguments into numbers.
- Build programs using command line arguments.
- Understand real-world applications.
📖 What are Command Line Arguments?
Command line arguments are values passed to a program when it is executed from the terminal.
Example
bashprogram Hello World
Here,
program
Hello
World
are received by the program.
main() Function with Arguments
Normally we write:
cint main() { }
For command line arguments:
cint main(int argc, char *argv[]) { }
or
cint main(int argc, char **argv) { }
Both forms are equivalent.
Understanding argc
argc stands for Argument Count.
It stores the total number of command line arguments.
Example
Command
bashprogram apple mango banana
Value of
textargc = 4
Because
argv[0] -> program
argv[1] -> apple
argv[2] -> mango
argv[3] -> banana
The program name is always counted.
Understanding argv
argv stands for Argument Vector.
It is an array of character pointers containing all arguments as strings.
Example
textargv[0] = program argv[1] = apple argv[2] = mango
Printing Command Line Arguments
c#include <stdio.h> int main(int argc, char *argv[]) { for(int i = 0; i < argc; i++) { printf("%s\n", argv[i]); } return 0; }
Execution
bashprogram KauraX C Programming
Output
textprogram KauraX C Programming
Accessing Individual Arguments
c#include <stdio.h> int main(int argc, char *argv[]) { if(argc < 3) { printf("Not enough arguments"); return 1; } printf("First : %s\n", argv[1]); printf("Second : %s\n", argv[2]); return 0; }
Converting Strings to Numbers
All command line arguments are received as strings.
To perform mathematical operations, convert them using atoi().
Example
c#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { if(argc != 3) { printf("Usage: program number1 number2"); return 1; } int a = atoi(argv[1]); int b = atoi(argv[2]); printf("Sum = %d", a + b); return 0; }
Execution
bashprogram 15 25
Output
textSum = 40
Using atof()
For decimal numbers, use atof().
cdouble price = atof(argv[1]);
Using strtol()
For better error handling, use strtol() instead of atoi().
Example
c#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { if(argc != 2) { printf("Usage: program number"); return 1; } char *end; long value = strtol(argv[1], &end, 10); if(*end != '\0') { printf("Invalid number"); return 1; } printf("%ld", value); return 0; }
Real-World Example
Suppose KauraX has a utility program to create a new course.
Execution
bashcreate-course "C Programming"
Program
c#include <stdio.h> int main(int argc, char *argv[]) { if(argc != 2) { printf("Usage: create-course <course_name>"); return 1; } printf("Creating Course: %s", argv[1]); return 0; }
Output
textCreating Course: C Programming
Common Uses
Command line arguments are widely used in:
- File copy utilities
- Backup software
- Compilers
- Package managers
- Database tools
- Linux commands
- Automation scripts
Common Beginner Mistakes
Forgetting argv[0]
Remember:
textargv[0]
contains the program name.
Accessing Missing Arguments
Wrong
cprintf("%s", argv[2]);
without checking
cargc
Always verify the argument count first.
Assuming Arguments are Numbers
Arguments are always strings.
Wrong
cint sum = argv[1] + argv[2];
Correct
cint sum = atoi(argv[1]) + atoi(argv[2]);
Always validate both the number of arguments (argc) and their contents before using them. This makes your programs more reliable and user-friendly.
Best Practices
- Always check
argc. - Display helpful usage messages.
- Prefer
strtol()overatoi()for robust number parsing. - Validate user input.
- Handle errors gracefully.
🧠 Interview Questions
- What are command line arguments?
- What is
argc? - What is
argv? - Why is
argv[0]special? - Why are arguments stored as strings?
- How do you convert arguments to integers?
- Difference between
atoi()andstrtol()? - What happens if insufficient arguments are passed?
- Where are command line arguments commonly used?
- Write a program that adds two numbers using command line arguments.
✍ Practice Questions
- Print all command line arguments.
- Count the number of arguments.
- Add two numbers from the command line.
- Multiply three numbers from the command line.
- Find the largest number passed as arguments.
- Reverse the order of command line arguments.
- Validate whether an argument is numeric.
📚 Lesson Summary
In this lesson, you learned:
- What command line arguments are.
argcandargv.- Accessing arguments.
- Converting strings to numbers.
- Using
atoi(),atof(), andstrtol(). - Best practices and error handling.
Command line arguments allow programs to receive input directly when they start, making them ideal for utilities, automation scripts, and professional command-line tools.
🎉 Core C Programming Completed!
Congratulations! You have now completed all the core concepts of C programming in the KauraX course, including:
- Basics and Syntax
- Variables & Data Types
- Operators
- Decision Making
- Loops
- Functions
- Arrays & Strings
- Pointers
- Structures & Unions
- Dynamic Memory Allocation
- File Handling
- Preprocessor Directives
- Storage Classes
- Enumerations &
typedef - Command Line Arguments
🚀 What's Next?
The remaining sections will focus on mastering C through practice:
- 100+ C Programming Practice Problems
- 10 Beginner Mini Projects
- 10 Intermediate Projects
- Top 100 C Interview Questions
- 250+ MCQs with Answers
- Common Errors & Debugging Guide
- Complete C Programming Cheat Sheet
- Final Revision Notes
These sections will help you transition from learning C to confidently building real-world applications.
Happy Coding with KauraX 💙