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 Last Command Line Argument

 

C Program to Print the Last Command Line Argument

Introduction

In this C program, we will learn how to print the last command line argument passed to a program using argc and argv. The program first checks whether any command line arguments are supplied. If at least one argument exists, it accesses the last argument using argv[argc - 1]. 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[argc - 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("Last argument is: %s\n", argv[argc - 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 Apple Banana Mango Orange

Sample Output

Case 1: Argument Passed


Last argument is: Orange

Case 2: No Argument Passed


No arguments are passed.

Output

Displays the last 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 through the following function parameters.


int main(int argc, char *argv[])
  • argc stores the total number of command line arguments.
  • argv stores all command line arguments as strings.

Step 2

Check whether at least one user-supplied argument is available.


if(argc > 1)

If the condition is true, the program safely accesses the last argument.


Step 3

Print the last command line argument.


printf("Last argument is: %s\n", argv[argc - 1]);
  • argc - 1 gives the index of the last argument.
  • argv[argc - 1] always points to the last user-supplied argument.

Step 4

If no arguments are supplied, print an informative message.


printf("No arguments are passed.\n");

Dry Run

Input


./a.out Linux C Programming Tutorial

Processing

Index Argument
0 ./a.out
1 Linux
2 C
3 Programming
4 Tutorial

Since argc = 5, the last argument is stored at argv[4], which is "Tutorial".

Output


Last argument is: Tutorial

Flow of Execution


Start
   │
Receive argc and argv
   │
argc > 1 ?
   │
 ┌────Yes────────────┐
 │                   │
Print argv[argc-1]   │
 │                   │
 └─────No────────────┘
         │
Print "No arguments are passed."
         │
        Stop

Time Complexity

Operation Complexity
Checking argc O(1)
Printing last 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 function parameters are used.

Auxiliary Space Complexity: O(1)


Applications

  • Reading the last filename supplied to a program.
  • Obtaining the last command-line option.
  • Developing Linux command-line utilities.
  • Writing shell automation tools.
  • Processing the final user input parameter.

Key Points

  • argv[0] stores the program name.
  • argv[argc - 1] always stores the last command line argument.
  • Always check argc > 1 before accessing the last argument.
  • The last argument index is calculated dynamically.
  • This program demonstrates safe access to command line arguments.

Interview Questions

  1. Why do we use argv[argc - 1]?
  2. What happens if argc == 1?
  3. Why is it necessary to check argc > 1?
  4. What is stored in argv[0]?
  5. Can the last command line argument be accessed without a loop?

Frequently Asked Questions (FAQs)

1. Why is the last argument accessed using argv[argc - 1]?

Array indexing starts from 0, so the last element is always located at index argc - 1.

2. What happens if no arguments are passed?

The program prints "No arguments are passed." because there are no user-supplied arguments.

3. What is stored in argv[0]?

It contains the program name or executable path.

4. Can argc be used to find any argument?

Yes. By using the appropriate index, any command line argument can be accessed through the argv array.

5. Is this program safe?

Yes. It checks the value of argc before accessing the last argument, preventing invalid memory access.


Keywords

C Program to Print Last Command Line Argument, argc argv in C, Last Command Line Argument in C, Print argv[argc-1], Command Line Arguments Example, Linux C Programming, C Interview Programs, Command Line Input in C.


Conclusion

This program demonstrates how to print the last command line argument using argc and argv in C. By calculating the last index as argc - 1 and validating the number of arguments beforehand, the program safely retrieves the final user-supplied argument. This technique is commonly used in Linux utilities, command-line tools, and system programming applications.

Comments

Popular Posts

🌙