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 Count Total Number of Command Line Arguments

 

C Program to Count Total Number of Command Line Arguments

Introduction

In this C program, we will learn how to count the total number of Command Line Arguments passed to a program using the argc variable. The argc parameter automatically stores the total number of arguments supplied while executing the program, including the program name. This program simply prints the value of argc, making it one of the easiest examples for understanding command line arguments in C.


Table of Contents


Algorithm

  1. Start the program.
  2. Receive command line arguments using argc and argv.
  3. Print the value stored in argc.
  4. Terminate the program.

C Program


#include<stdio.h>

int main(int argc, char *argv[])
{
    printf("Total number of arguments is: %d\n", argc);

    return 0;
}

Input

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


./a.out Apple Mango Orange

Sample Output


Total number of arguments is: 4

Here,

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

Hence, the total number of command line arguments is 4.


Output

Displays the total number of command line arguments passed to the program.


Explanation

Step 1

The program receives command line arguments using the parameters argc and argv.


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

Step 2

Print the value of argc.


printf("Total number of arguments is: %d\n", argc);

The value printed includes the program name (argv[0]) along with all user-supplied arguments.


Dry Run

Input


./a.out Linux C Programming

Processing

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

Since there are four arguments (including the program name), argc = 4.

Output


Total number of arguments is: 4

Flow of Execution


Start
   │
Receive argc and argv
   │
argc stores total arguments
   │
Print argc
   │
Stop

Time Complexity

Operation Complexity
Printing argc O(1)

Best Case: O(1)

Average Case: O(1)

Worst Case: O(1)

Overall Time Complexity: O(1)


Space Complexity

  • No additional memory is allocated.
  • Only the predefined parameter argc is used.

Auxiliary Space Complexity: O(1)


Applications

  • Counting user inputs passed through the terminal.
  • Validating the required number of command line arguments.
  • Developing Linux command-line utilities.
  • Input validation before processing files.
  • Writing automation and shell scripts.

Key Points

  • argc stands for Argument Count.
  • It stores the total number of command line arguments.
  • The program name is also counted as one argument.
  • argv[0] always stores the executable name.
  • Counting arguments using argc is an O(1) operation.

Interview Questions

  1. What is argc in C?
  2. Does argc include the program name?
  3. What is stored in argv[0]?
  4. What is the difference between argc and argv?
  5. Why are command line arguments useful?

Frequently Asked Questions (FAQs)

1. What does argc represent?

argc stands for Argument Count and stores the total number of command line arguments.

2. Is the program name included in argc?

Yes. The executable name is always counted as the first argument.

3. What happens if no arguments are passed?

The value of argc will be 1 because only the program name exists.

4. What is argv?

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

5. Can argc be zero?

In normal C program execution, argc is never zero because the program name is always present.


Keywords

C Program to Count Command Line Arguments, argc in C, Command Line Arguments in C, Count Total Arguments Using argc, C Programming Examples, Linux C Programming, argc argv Program, C Interview Programs, Command Line Input in C.


Conclusion

This program demonstrates how to count the total number of command line arguments using the argc parameter in C. Since argc automatically stores the number of arguments passed during program execution, it provides a simple and efficient way to validate user input before processing it. Understanding argc is essential for developing command-line applications, Linux utilities, and system-level programs in C.

Comments

Popular Posts

🌙