Lesson 12
Command-Line Arguments
Read input from argc and argv
1What are Command-Line Arguments?✨
Command-line arguments allow you to pass information to your C program when you run it from the command line. This makes your programs more flexible and interactive!
2Why Use Command-Line Arguments?🎯
- ✓ Customize program behavior without changing code
- ✓ Handle user input directly from the command line
- ✓ Create scripts that can process multiple inputs easily
3Understanding argc and argv💡
In C, command-line arguments are accessed via two parameters in the main function: `int argc` (argument count) and `char *argv[]` (argument vector). `argc` counts how many arguments were passed, and `argv` is an array of strings representing those arguments.
4Example of argc and argv:🚀
```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;
}
```5Running Your Program🔍
- To run a program with command-line arguments, compile your C code and use the terminal. For example:
- ```
- ./your_program arg1 arg2 arg3
- ```
- This will display:
- ```
- Number of arguments: 4
- Argument 0: ./your_program
- Argument 1: arg1
- Argument 2: arg2
- Argument 3: arg3
- ```
6Practical Uses of Command-Line Arguments⭐
- You can use command-line arguments for various purposes:
- Accepting user input for calculations
- Specifying file names for reading/writing
- Configuring settings for your program's execution
7Building Interactive Programs🧩
This lesson sets the stage for creating more interactive programs. Imagine writing a program that calculates the sum of numbers provided via command-line arguments!
Now you have the tools to make your C programs more dynamic and user-friendly!
🎯 Practice Challenges
Master Command-Line Arguments by completing all 3 difficulty levels
Command-Line Arguments - Easy Challenge
Start with the basics. Perfect for beginners!
Command-Line Arguments - Medium Challenge
Get more challenging. Build your confidence!
Command-Line Arguments - Hard Challenge
Master this concept. Prove your skills!
🏆 Study a lesson for 2+ minutes to earn +10 TalentJR Points a day. Badges, certificates & prizes →
Ready to Master Command-Line Arguments? 🚀
Complete the challenges, take the quiz and keep your learning streak going!
Keep learning
- ← Previous lessonLesson 11: Recursion in CSolve problems with self-calling functions
- Next lesson →Lesson 13: Linked ListsBuild your first dynamic data structure
- Grade 11 courseC++ OOP - Advanced ProgrammingC++ coding course for kids.
- Student ToolsFree calculators for studentsPercentage, fraction, ratio and marks calculators with steps.
- CBSEClass 10 CBSE & NCERT study materialMaths, Science and Computer chapters with solutions.
- Future SkillsExplore future skillsAI, robotics and new-age skills explained for students.