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 All Command Line Arguments

 

C Program to Print All Command Line Arguments

Introduction

In this C program, we will learn how to print all Command Line Arguments passed to a program during execution. Command line arguments allow users to provide input while running the program from the terminal without using scanf(). The program uses the argc variable to determine the number of arguments and the argv array to access each argument.


Table of Contents


Algorithm

  1. Start the program.
  2. Receive command line arguments using argc and argv.
  3. Print a heading.
  4. Run a loop from 0 to argc-1.
  5. Print each argument using argv[i].
  6. Stop the program.

C Program


#include<stdio.h>

int main(int argc, char *argv[])
{
    printf("Command line arguments are:\n");

    for(int i = 0; i < argc; i++)
    {
        printf("Argument[%d]=%s\n", i, argv[i]);
    }

    return 0;
}

Input

Run the program from the terminal by passing command line arguments.


./a.out Apple Mango Orange

Sample Output


Command line arguments are:

Argument[0]=./a.out
Argument[1]=Apple
Argument[2]=Mango
Argument[3]=Orange

Output

Displays all the command line arguments passed to the program along with their index.


Explanation

Step 1

The program receives the total number of command line arguments using argc.


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

Step 2

Print a heading before displaying all arguments.


printf("Command line arguments are:\n");

Step 3

Run a loop from 0 to argc-1.


for(int i = 0; i < argc; i++)

The loop ensures every command line argument is visited exactly once.


Step 4

Print each argument stored in the argv array.


printf("Argument[%d]=%s\n", i, argv[i]);
  • argv[0] contains the program name.
  • argv[1] onwards contain the user-supplied arguments.

Dry Run

Input


./a.out 100 Hello Linux

Processing

Iteration i argv[i] Printed Output
1 0 ./a.out Argument[0]=./a.out
2 1 100 Argument[1]=100
3 2 Hello Argument[2]=Hello
4 3 Linux Argument[3]=Linux

Output


Argument[0]=./a.out
Argument[1]=100
Argument[2]=Hello
Argument[3]=Linux

Flow of Execution


Start
   │
Receive argc and argv
   │
Print Heading
   │
Loop from i = 0 to argc-1
   │
Print argv[i]
   │
Increment i
   │
Loop Ends
   │
Stop

Time Complexity

Case Complexity
Best Case O(n)
Average Case O(n)
Worst Case O(n)

Overall Time Complexity: O(n), where n is the number of command line arguments.


Space Complexity

  • No extra data structure is used.
  • Only loop variable is used.

Auxiliary Space Complexity: O(1)


Applications

  • Passing filenames to programs.
  • Passing configuration options.
  • Executing automation scripts.
  • Developing Linux utilities.
  • Building command line tools.

Key Points

  • argc stores the total number of command line arguments.
  • argv stores all arguments as strings.
  • argv[0] always contains the program name.
  • Arguments start from argv[1].
  • The loop prints every argument one by one.
  • No scanf() is required.

Interview Questions

  1. What is the purpose of argc and argv?
  2. What does argv[0] contain?
  3. Why is argv declared as char *argv[]?
  4. Can command line arguments contain spaces?
  5. What is the data type of argv?

Frequently Asked Questions (FAQs)

1. What is argc?

argc stands for Argument Count. It stores the total number of command line arguments passed to the program.

2. What is argv?

argv is an array of character pointers that stores all command line arguments as strings.

3. What is stored in argv[0]?

argv[0] contains the name or path of the executable program.

4. Can we pass numbers through command line arguments?

Yes. However, they are received as strings and must be converted using functions like atoi() if numerical operations are required.

5. Why are command line arguments useful?

They allow users to provide input directly while running a program, making applications more flexible and suitable for automation.


Keywords

C Command Line Arguments, argc argv in C, Print Command Line Arguments, Command Line Arguments Program in C, C Programming Examples, Linux C Programming, argc Example, argv Example, C Interview Programs, Command Line Input in C.


Conclusion

This program demonstrates how to print all command line arguments using argc and argv in C. It is one of the most fundamental concepts in C programming for developing command-line applications. Understanding command line arguments is essential for Linux programming, system programming, scripting, and technical interviews.

Comments

Popular Posts

🌙