Search This Blog
Welcome to 1printf(), your ultimate destination for C, C++, Linux, Data Structures, and Microcontroller programming! 🚀 🔹Learn advanced coding techniques in C& C++ 🔹Master Linux internals & shell scripting 🔹Deep dive into Data Structures & Algorithms 🔹Explore Embedded Systems & Microcontrollers (8051,UART, RTOS) 🔹Get hands-on coding tutorials, project ideas,and interview preparation tips Whether you're a beginner or an experienced programmer, this channel will help you
Featured
- Get link
- X
- Other Apps
C Program to Print the First Command Line Argument
C Program to Print the First Command Line Argument
Introduction
In this C program, we will learn how to print the first command line argument passed to a program using argc and argv. Before accessing the first argument, the program checks whether any argument is supplied by verifying the value of argc. If at least one argument is present, it prints the first argument; otherwise, it displays an appropriate message indicating that no arguments were passed.
Table of Contents
- Algorithm
- C Program
- Input
- Sample Output
- Explanation
- Dry Run
- Flow of Execution
- Time Complexity
- Space Complexity
- Applications
- Key Points
- Interview Questions
- Frequently Asked Questions
- Keywords
- Conclusion
Algorithm
- Start the program.
- Receive the command line arguments using argc and argv.
- Check whether argc > 1.
- If true, print argv[1].
- Otherwise, display "No arguments are passed."
- Terminate the program.
C Program
#include<stdio.h>
int main(int argc, char *argv[])
{
if(argc > 1)
{
printf("First argument is: %s\n", argv[1]);
}
else
{
printf("No arguments are passed.\n");
}
return 0;
}
Input
Run the program from the terminal by passing one or more command line arguments.
./a.out Linux Programming
Sample Output
Case 1: Argument Passed
First argument is: Linux
Case 2: No Argument Passed
No arguments are passed.
Output
Displays the first command line argument if it exists; otherwise, prints a message indicating that no arguments were supplied.
Explanation
Step 1
The program receives command line arguments using the following function parameters.
int main(int argc, char *argv[])
- argc stores the total number of arguments.
- argv stores all arguments as strings.
Step 2
Check whether at least one user argument is supplied.
if(argc > 1)
If argc is greater than 1, the program knows that argv[1] exists.
Step 3
Print the first command line argument.
printf("First argument is: %s\n", argv[1]);
argv[1] always stores the first user-supplied argument because argv[0] contains the program name.
Step 4
If no command line argument is supplied, display an error message.
printf("No arguments are passed.\n");
Dry Run
Input
./a.out Apple Banana Mango
Processing
| Index | Argument |
|---|---|
| 0 | ./a.out |
| 1 | Apple |
| 2 | Banana |
| 3 | Mango |
Since argc = 4, the condition argc > 1 is true. Therefore, argv[1] is printed.
Output
First argument is: Apple
Flow of Execution
Start
│
Receive argc and argv
│
argc > 1 ?
│
┌─Yes──────────────┐
│ │
Print argv[1] │
│ │
└──────No──────────┘
│
Print "No arguments are passed."
│
Stop
Time Complexity
| Operation | Complexity |
|---|---|
| Checking argc | O(1) |
| Printing first argument | O(1) |
Best Case: O(1)
Average Case: O(1)
Worst Case: O(1)
Overall Time Complexity: O(1)
Space Complexity
- No extra memory is allocated.
- Only predefined variables are used.
Auxiliary Space Complexity: O(1)
Applications
- Reading filenames from the command line.
- Passing usernames or IDs to programs.
- Creating Linux command-line utilities.
- Writing automation scripts.
- Performing basic input validation.
Key Points
- argv[0] contains the program name.
- argv[1] contains the first user-supplied argument.
- Always check argc > 1 before accessing argv[1].
- The program avoids invalid memory access by validating input.
- This is a common interview program on command line arguments.
Interview Questions
- Why should we check argc > 1 before accessing argv[1]?
- What is stored in argv[0]?
- What happens if argv[1] is accessed without checking argc?
- Can command line arguments contain spaces?
- What is the difference between argc and argv?
Frequently Asked Questions (FAQs)
1. Why is argc checked before printing argv[1]?
Checking argc > 1 ensures that the first command line argument exists, preventing invalid memory access.
2. What is stored in argv[1]?
It stores the first argument entered by the user after the program name.
3. What happens if no arguments are passed?
The program prints "No arguments are passed."
4. Does argv[0] store the first user argument?
No. It stores the executable program name or path.
5. Is this program safe?
Yes. It first validates the number of arguments using argc before accessing argv[1].
Keywords
C Program to Print First Command Line Argument, argc argv in C, Print argv[1] in C, First Command Line Argument in C, Command Line Arguments Example, Linux C Programming, C Interview Programs, Command Line Input in C.
Conclusion
This program demonstrates how to safely print the first command line argument using argc and argv in C. By checking whether an argument exists before accessing argv[1], the program prevents invalid memory access and becomes more reliable. This technique is widely used in Linux programming, system programming, and command-line application development.
Popular Posts
C++ Program for Hybrid Inheritance (All Types Together)
- Get link
- X
- Other Apps
C++ Program for Function Overloading Example
- Get link
- X
- Other Apps
Comments
Post a Comment