Skip to main content

Featured

Merge Sort in C++

  Merge Sort in C++ Introduction Merge Sort is a popular sorting algorithm that follows the Divide and Conquer approach. It divides an array into smaller subarrays, recursively sorts those subarrays, and finally merges the sorted subarrays to produce a completely sorted array. In this tutorial, we will learn how to implement Merge Sort in C++ . The program divides the array into two halves using the mid index, recursively sorts both halves, and then combines them using the merge() function. Merge Sort has a time complexity of O(n log n) in the best, average, and worst cases. Table of Contents Algorithm C++ Program Input Sample Output 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. Read the size of the array. Read the array elements from the user. Call the mer...

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

  1. Start the program.
  2. Receive the command line arguments using argc and argv.
  3. Check whether argc > 1.
  4. If true, print argv[1].
  5. Otherwise, display "No arguments are passed."
  6. 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

  1. Why should we check argc > 1 before accessing argv[1]?
  2. What is stored in argv[0]?
  3. What happens if argv[1] is accessed without checking argc?
  4. Can command line arguments contain spaces?
  5. 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.

Comments

Popular Posts

🌙