C Command Line Arguments Practice Questions with Solutions

Command Line Arguments are values passed to a C program at the time of execution through the command line. They allow users to provide input without modifying the source code or using functions like scanf().

In C, command line arguments are handled through the main() function using the following syntax:

int main(int argc, char *argv[])

Where:

  • argc (Argument Count) stores the total number of command line arguments.
  • argv (Argument Vector) is an array of character pointers containing each command line argument as a string.

The first argument (argv[0]) always contains the program name.

Command line arguments are widely used in:

  • File Processing Utilities
  • System Programming
  • Compiler Design
  • Automation Scripts
  • Linux Applications
  • Batch Processing
  • Network Utilities
  • Competitive Programming

In this chapter, you’ll learn how to work with command line arguments through practical programs and real-world examples.C Command Line Arguments practice questions with solutions help to understnad the concepts.


1. C Program to Display Command Line Arguments

Problem Statement

Write a C program to display all command line arguments entered by the user.

C Solution

#include <stdio.h>

int main(int argc, char *argv[])
{
    int i;

    printf("Command Line Arguments:\n");

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

    return 0;
}

Sample Execution

program.exe Hello C Programming

Sample Output

Command Line Arguments:
program.exe
Hello
C
Programming

Explanation

The program loops through the argv array and prints every command line argument entered by the user.

Concepts Covered

  • argc
  • argv
  • Command Line Arguments
  • Loops

2. C Program to Count Command Line Arguments

Problem Statement

Write a C program to count the total number of command line arguments.

C Solution

#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("Total Arguments = %d", argc);

    return 0;
}

Sample Execution

program.exe Apple Mango Orange

Sample Output

Total Arguments = 4

Explanation

The variable argc automatically stores the total number of arguments, including the program name.

Concepts Covered

  • argc
  • Command Line Arguments
  • Program Execution

3. C Program to Display the Program Name

Problem Statement

Write a C program to display the program name using command line arguments.

C Solution

#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("Program Name = %s", argv[0]);

    return 0;
}

Sample Output

Program Name = program.exe

Explanation

The first element of argv always stores the name of the executable program.

Concepts Covered

  • argv
  • Program Name
  • Command Line Arguments

4. C Program to Display the First User Argument

Problem Statement

Write a C program to display the first command line argument entered by the user.

C Solution

#include <stdio.h>

int main(int argc, char *argv[])
{
    if(argc > 1)
    {
        printf("First User Argument = %s", argv[1]);
    }
    else
    {
        printf("No Argument Passed.");
    }

    return 0;
}

Sample Execution

program.exe Programming

Sample Output

First User Argument = Programming

Explanation

  • argv[0] contains the program name.
  • argv[1] contains the first argument entered by the user.
  • The program first checks whether at least one user argument exists.

Concepts Covered

  • argc
  • argv
  • Command Line Arguments
  • Conditional Statements

5. C Program to Check Whether Command Line Arguments Are Passed

Problem Statement

Write a C program to determine whether the user has passed any command line arguments.

C Solution

#include <stdio.h>

int main(int argc, char *argv[])
{
    if(argc > 1)
    {
        printf("Arguments Received.");
    }
    else
    {
        printf("No Arguments Received.");
    }

    return 0;
}

Sample Execution

program.exe Hello

Sample Output

Arguments Received.

Another Execution

program.exe

Sample Output

No Arguments Received.

Explanation

The program checks the value of argc.

  • If argc is greater than 1, at least one command line argument has been supplied.
  • Otherwise, only the program name exists.

Concepts Covered

  • argc
  • argv
  • Command Line Arguments
  • Conditional Statements

6. C Program to Add Two Numbers Using Command Line Arguments

Problem Statement

Write a C program to add two numbers entered through command line arguments.

C Solution

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int firstNumber, secondNumber;

    if(argc != 3)
    {
        printf("Please provide exactly two numbers.");
        return 1;
    }

    firstNumber = atoi(argv[1]);
    secondNumber = atoi(argv[2]);

    printf("Sum = %d", firstNumber + secondNumber);

    return 0;
}

Sample Execution

program.exe 25 30

Sample Output

Sum = 55

Explanation

The atoi() function converts command line arguments from strings into integers before performing the addition.

Concepts Covered

  • Command Line Arguments
  • atoi()
  • Arithmetic Operations
  • stdlib.h

7. C Program to Multiply Two Numbers Using Command Line Arguments

Problem Statement

Write a C program to multiply two numbers entered through command line arguments.

C Solution

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int firstNumber, secondNumber;

    if(argc != 3)
    {
        printf("Please provide exactly two numbers.");
        return 1;
    }

    firstNumber = atoi(argv[1]);
    secondNumber = atoi(argv[2]);

    printf("Product = %d", firstNumber * secondNumber);

    return 0;
}

Sample Execution

program.exe 8 9

Sample Output

Product = 72

Explanation

Both command line arguments are converted into integers and multiplied together.

Concepts Covered

  • atoi()
  • Command Line Arguments
  • Multiplication
  • stdlib.h

8. C Program to Convert a String to an Integer Using atoi()

Problem Statement

Write a C program to convert a command line string into an integer.

C Solution

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int number;

    if(argc != 2)
    {
        printf("Please provide one number.");
        return 1;
    }

    number = atoi(argv[1]);

    printf("Integer = %d", number);

    return 0;
}

Sample Execution

program.exe 150

Sample Output

Integer = 150

Explanation

The atoi() function converts a numeric string into an integer value.

Concepts Covered

  • atoi()
  • String Conversion
  • Command Line Arguments
  • stdlib.h

9. C Program to Print Arguments in Reverse Order

Problem Statement

Write a C program to print all command line arguments in reverse order.

C Solution

#include <stdio.h>

int main(int argc, char *argv[])
{
    int i;

    printf("Arguments in Reverse Order:\n");

    for(i = argc - 1; i >= 0; i--)
    {
        printf("%s\n", argv[i]);
    }

    return 0;
}

Sample Execution

program.exe Apple Mango Orange

Sample Output

Orange
Mango
Apple
program.exe

Explanation

The program traverses the argv array from the last element to the first element.

Concepts Covered

  • argv
  • Reverse Traversal
  • Loops
  • Command Line Arguments

10. C Program to Find the Length of a Command Line Argument

Problem Statement

Write a C program to determine the length of the first command line argument.

C Solution

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        printf("Please provide one argument.");
        return 1;
    }

    printf("Length = %lu", strlen(argv[1]));

    return 0;
}

Sample Execution

program.exe Programming

Sample Output

Length = 11

Explanation

The strlen() function from <string.h> calculates the number of characters in the command line argument.

Concepts Covered

  • strlen()
  • Command Line Arguments
  • String Functions
  • string.h

11. C Program to Compare Two Command Line Strings

Problem Statement

Write a C program to compare two command line strings.

C Solution

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    if(argc != 3)
    {
        printf("Please provide two strings.");
        return 1;
    }

    if(strcmp(argv[1], argv[2]) == 0)
    {
        printf("Strings are Equal.");
    }
    else
    {
        printf("Strings are Different.");
    }

    return 0;
}

Sample Execution

program.exe Hello Hello

Sample Output

Strings are Equal.

Explanation

The strcmp() function compares two strings.

  • Returns 0 if both strings are identical.
  • Returns a non-zero value if they differ.

Concepts Covered

  • strcmp()
  • String Comparison
  • Command Line Arguments
  • string.h

12. C Program to Copy a Command Line String

Problem Statement

Write a C program to copy a command line string into another string.

C Solution

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    char destination[100];

    if(argc != 2)
    {
        printf("Please provide one string.");
        return 1;
    }

    strcpy(destination, argv[1]);

    printf("Copied String = %s", destination);

    return 0;
}

Sample Execution

program.exe Programming

Sample Output

Copied String = Programming

Explanation

The strcpy() function copies one string into another.

Concepts Covered

  • strcpy()
  • String Copy
  • Command Line Arguments
  • string.h

13. C Program to Convert a Command Line String to Uppercase

Problem Statement

Write a C program to convert a command line string into uppercase.

C Solution

#include <stdio.h>
#include <ctype.h>

int main(int argc, char *argv[])
{
    int i = 0;

    if(argc != 2)
    {
        printf("Please provide one string.");
        return 1;
    }

    while(argv[1][i] != '\0')
    {
        printf("%c", toupper(argv[1][i]));
        i++;
    }

    return 0;
}

Sample Execution

program.exe coding

Sample Output

CODING

Explanation

The toupper() function converts lowercase characters into uppercase characters one by one.

Concepts Covered

  • toupper()
  • Character Functions
  • ctype.h
  • Command Line Arguments

14. C Program to Calculate the Average of Numbers Passed as Command Line Arguments

Problem Statement

Write a C program to calculate the average of numbers passed through command line arguments.

C Solution

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int i;
    int sum = 0;

    if(argc < 2)
    {
        printf("Please provide numbers.");
        return 1;
    }

    for(i = 1; i < argc; i++)
    {
        sum += atoi(argv[i]);
    }

    printf("Average = %.2f",
           (float)sum / (argc - 1));

    return 0;
}

Sample Execution

program.exe 10 20 30 40

Sample Output

Average = 25.00

Explanation

Each command line argument is converted into an integer using atoi(), added together, and divided by the total number of user-provided values.

Concepts Covered

  • argc
  • argv
  • atoi()
  • Loops
  • Arithmetic Operations

15. C Program to Display Environment Information Using Command Line Concepts

Problem Statement

Write a C program to display the total number of command line arguments and each argument separately.

C Solution

#include <stdio.h>

int main(int argc, char *argv[])
{
    int i;

    printf("Total Arguments = %d\n\n", argc);

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

    return 0;
}

Sample Execution

program.exe India Delhi Coding

Sample Output

Total Arguments = 4

Argument 0 : program.exe
Argument 1 : India
Argument 2 : Delhi
Argument 3 : Coding

Explanation

The program demonstrates how command line arguments are stored and accessed using the argc and argv parameters.

Concepts Covered

  • argc
  • argv
  • Command Line Arguments
  • Loops

Chapter Summary

In this chapter, you learned how to work with Command Line Arguments in C Programming. You practiced reading arguments, counting them, accessing individual arguments, converting strings to integers, performing arithmetic operations, comparing and copying strings, changing letter case, and processing multiple values. Command line arguments make programs more flexible by allowing input to be provided during execution instead of hardcoding values.


Key Takeaways

  • Command line arguments are passed when a program starts.
  • argc stores the total number of arguments.
  • argv stores each argument as a string.
  • argv[0] always contains the program name.
  • The atoi() function converts strings into integers.
  • String functions like strcmp(), strcpy(), and strlen() work with command line input.
  • Loops are commonly used to process multiple arguments.
  • Command line arguments are widely used in system utilities and automation tools.
  • Proper argument validation improves program reliability.
  • Command line arguments eliminate the need for interactive input in many applications.

Frequently Asked Questions (FAQs)

1. What are command line arguments in C?

Command line arguments are values supplied to a C program when it is executed from the command prompt or terminal.


2. What is argc in C?

argc stands for Argument Count. It stores the total number of command line arguments, including the program name.


3. What is argv in C?

argv stands for Argument Vector. It is an array of character pointers where each element points to a command line argument.


4. Why does argv[0] contain the program name?

The operating system automatically places the executable file name in argv[0] so the running program knows how it was invoked.


5. How do you convert a command line argument into an integer?

Use the atoi() function from <stdlib.h>:

int number = atoi(argv[1]);

6. Can command line arguments contain spaces?

Yes, but arguments containing spaces must be enclosed in quotation marks when executing the program.

Example:

program.exe "C Programming Language"

7. Where are command line arguments commonly used?

They are widely used in system utilities, compilers, automation scripts, Linux commands, file processing programs, batch processing, and command-line applications.


8. Why are command line arguments important in interviews?

They demonstrate your understanding of program execution, input handling, pointers, strings, arrays, and real-world software development, making them a frequently tested topic in C programming interviews.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top