Introduction
Command-line arguments allow you to pass information to a C program when you start it instead of entering the information after the program begins. In C, command-line arguments are received through argc and argv. In this chapter, you will practice displaying arguments, working with numbers, performing calculations, comparing values, and building small practical programs using command-line input. Command-Line Arguments in C Practice questions with solutions to help you understand the concepts.
Q1. Display Command-Line Arguments
Problem Statement
Write a C program that displays all command-line arguments provided when the program is executed.
C Program
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
printf("Number of arguments: %d\n", argc);
for (i = 0; i < argc; i++)
{
printf("Argument %d: %s\n", i, argv[i]);
}
return 0;
}
How to Run
Suppose the program is compiled as:
program.exe
Run:
program.exe Hello C Programming
Sample Output
Number of arguments: 4
Argument 0: program.exe
Argument 1: Hello
Argument 2: C
Argument 3: Programming
Explanation
The main() function can receive two parameters:
int main(int argc, char *argv[])
argc means argument count.
argv means argument vector and stores the arguments as strings.
The program name is normally stored at:
argv[0]
The first argument supplied by the user is:
argv[1]
Concepts Covered
argcargv- Command-line arguments
- Arrays of strings
forloop
Q2. Print Your Name Using a Command-Line Argument
Problem Statement
Write a C program that accepts a person’s name from the command line and displays it.
C Program
#include <stdio.h>
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Please provide your name.");
return 1;
}
printf("Hello, %s!", argv[1]);
return 0;
}
How to Run
program.exe Rahul
Sample Output
Hello, Rahul!
Explanation
The program expects at least two arguments:
argv[0] → program name
argv[1] → user's name
We check:
if (argc < 2)
This prevents the program from trying to access argv[1] when no name was supplied.
Concepts Covered
argcargv[1]- String arguments
- Input validation
Q3. Add Two Numbers from the Command Line
Problem Statement
Write a C program that accepts two integers through command-line arguments and calculates their sum.
C Program
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int a, b, sum;
if (argc != 3)
{
printf("Usage: program.exe number1 number2");
return 1;
}
a = atoi(argv[1]);
b = atoi(argv[2]);
sum = a + b;
printf("Sum = %d", sum);
return 0;
}
How to Run
program.exe 25 15
Sample Output
Sum = 40
Explanation
Command-line arguments are received as strings.
For example:
argv[1] → "25"
argv[2] → "15"
To convert these strings to integers, we use:
atoi(argv[1])
atoi() is provided by:
#include <stdlib.h>
Then normal integer addition can be performed.
Concepts Covered
- Numeric command-line arguments
atoi()argcargv- Integer addition
Q4. Calculate the Area of a Rectangle
Problem Statement
Write a C program that accepts the length and width of a rectangle through command-line arguments and calculates its area.
C Program
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
float length, width, area;
if (argc != 3)
{
printf("Usage: program.exe length width");
return 1;
}
length = atof(argv[1]);
width = atof(argv[2]);
area = length * width;
printf("Area of rectangle = %.2f", area);
return 0;
}
How to Run
program.exe 10.5 5
Sample Output
Area of rectangle = 52.50
Explanation
atof() converts a command-line string into a double value.
For example:
atof("10.5")
converts the text "10.5" into a numeric value.
The rectangle formula is:
Area = Length × Width
Concepts Covered
atof()- Floating-point arguments
- Command-line input
- Arithmetic operations
Q5. Find the Largest of Three Numbers
Problem Statement
Write a C program that accepts three integers through command-line arguments and finds the largest number.
C Program
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int a, b, c;
int largest;
if (argc != 4)
{
printf("Usage: program.exe number1 number2 number3");
return 1;
}
a = atoi(argv[1]);
b = atoi(argv[2]);
c = atoi(argv[3]);
largest = a;
if (b > largest)
{
largest = b;
}
if (c > largest)
{
largest = c;
}
printf("Largest = %d", largest);
return 0;
}
How to Run
program.exe 25 70 45
Sample Output
Largest = 70
Explanation
The first number is initially stored as the largest:
largest = a;
Then it is compared with b and c.
If a larger number is found, largest is updated.
This combines command-line arguments with conditional statements.
Concepts Covered
atoi()if- Comparison
- Command-line arguments
- Finding maximum values
Q6. Perform Arithmetic Operations
Problem Statement
Write a C program that accepts two integers through the command line and displays their addition, subtraction, multiplication, and division.
C Program
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int a, b;
if (argc != 3)
{
printf("Usage: program.exe number1 number2");
return 1;
}
a = atoi(argv[1]);
b = atoi(argv[2]);
printf("Addition = %d\n", a + b);
printf("Subtraction = %d\n", a - b);
printf("Multiplication = %d\n", a * b);
if (b != 0)
{
printf("Division = %.2f\n", (float)a / b);
}
else
{
printf("Division = Not possible");
}
return 0;
}
How to Run
program.exe 20 5
Sample Output
Addition = 25
Subtraction = 15
Multiplication = 100
Division = 4.00
Explanation
The two arguments are converted using atoi().
The program then performs four operations.
The division is checked separately:
if (b != 0)
This prevents division by zero.
The expression:
(float)a / b
produces a floating-point result.
Concepts Covered
- Command-line numbers
atoi()- Arithmetic operators
- Division by zero
- Type casting
Q7. Check Whether a Number is Even or Odd
Problem Statement
Write a C program that accepts an integer through the command line and checks whether it is even or odd.
C Program
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int number;
if (argc != 2)
{
printf("Usage: program.exe number");
return 1;
}
number = atoi(argv[1]);
if (number % 2 == 0)
{
printf("%d is Even", number);
}
else
{
printf("%d is Odd", number);
}
return 0;
}
How to Run
program.exe 24
Sample Output
24 is Even
Explanation
The command-line value is converted into an integer:
number = atoi(argv[1]);
Then the remainder is checked:
number % 2
If the remainder is 0, the number is even.
Otherwise, it is odd.
Concepts Covered
atoi()- Modulus operator
if-else- Command-line input
Q8. Calculate the Sum of Multiple Command-Line Numbers
Problem Statement
Write a C program that accepts multiple integers through command-line arguments and calculates their total sum.
C Program
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i;
int sum = 0;
if (argc < 2)
{
printf("Please provide at least one number.");
return 1;
}
for (i = 1; i < argc; i++)
{
sum += atoi(argv[i]);
}
printf("Sum = %d", sum);
return 0;
}
How to Run
program.exe 10 20 30 40 50
Sample Output
Sum = 150
Explanation
Unlike previous examples, this program does not expect exactly two or three arguments.
It accepts multiple numbers.
The loop starts from:
i = 1
because argv[0] contains the program name.
The loop continues while:
i < argc
Each argument is converted into an integer and added to sum.
Concepts Covered
- Variable number of arguments
argcargvforloopatoi()- Accumulator variable
Q9. Find the Average of Command-Line Numbers
Problem Statement
Write a C program that accepts multiple numbers through command-line arguments and calculates their average.
C Program
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i;
int sum = 0;
int count;
float average;
if (argc < 2)
{
printf("Please provide numbers.");
return 1;
}
count = argc - 1;
for (i = 1; i < argc; i++)
{
sum += atoi(argv[i]);
}
average = (float)sum / count;
printf("Average = %.2f", average);
return 0;
}
How to Run
program.exe 10 20 30 40
Sample Output
Average = 25.00
Explanation
If:
argc = 5
then there are four actual numbers because argv[0] is the program name.
Therefore:
count = argc - 1;
The program calculates:
Average = Sum / Number of Values
The cast:
(float)sum
ensures that decimal division is performed.
Concepts Covered
- Multiple arguments
argc - 1atoi()- Average calculation
- Type casting
Q10. Build a Simple Command-Line Calculator
Problem Statement
Create a calculator that accepts an operator and two numbers from the command line.
The program should support:
+
-
*
/
C Program
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char operator;
float num1, num2;
if (argc != 4)
{
printf("Usage: program.exe number1 operator number2");
return 1;
}
num1 = atof(argv[1]);
operator = argv[2][0];
num2 = atof(argv[3]);
switch (operator)
{
case '+':
printf("Result = %.2f", num1 + num2);
break;
case '-':
printf("Result = %.2f", num1 - num2);
break;
case '*':
printf("Result = %.2f", num1 * num2);
break;
case '/':
if (num2 == 0)
{
printf("Division by zero is not allowed");
}
else
{
printf("Result = %.2f", num1 / num2);
}
break;
default:
printf("Invalid operator");
}
return 0;
}
How to Run
program.exe 20 + 5
Sample Output
Result = 25.00
Another example:
program.exe 20 * 4
Output:
Result = 80.00
Explanation
The command line contains three actual arguments:
argv[1] → first number
argv[2] → operator
argv[3] → second number
The operator is obtained using:
operator = argv[2][0];
For example:
argv[2] = "+"
so:
argv[2][0]
gives the first character:
+
The switch statement then selects the appropriate calculation.
Concepts Covered
argcargvatof()- Character extraction
switch- Arithmetic operations
- Division by zero
- Command-line calculator
Key Takeaways
- Command-line arguments allow users to provide data when starting a C program.
argcstores the number of command-line arguments.argvstores the argument strings.argv[0]normally contains the program name.- Actual user-supplied values normally begin at
argv[1]. - Command-line arguments are received as strings.
atoi()can convert a string to an integer.atof()can convert a string to a floating-point value.- Always check
argcbefore accessing an expected argument. - Command-line arguments can be combined with loops, conditions,
switch, arrays, and functions. - For stronger input validation in larger programs,
strtol()andstrtod()are preferable toatoi()andatof().
FAQs
1. What are command-line arguments in C?
Command-line arguments are values supplied to a program when it is started. C provides argc and argv in main() to access these values.
2. What is argc in C?
argc stands for argument count. It tells the program how many command-line arguments were passed, including the program name.
3. What is argv in C?
argv stands for argument vector. It is an array containing the command-line arguments as strings.
4. Why is argv[0] usually the program name?
By convention, the first element of the argument array contains the name or invocation used to start the program. The actual user-supplied arguments normally begin at argv[1].
5. How do I pass an integer through the command line?
Pass the value when running the program:
program.exe 25
Then convert it:
int number = atoi(argv[1]);
6. Can command-line arguments contain spaces?
Yes. Put the complete argument inside quotation marks.
program.exe "C Programming"
This treats C Programming as one argument.
7. What is the difference between atoi() and atof()?
atoi() converts a string to an int, while atof() converts a string to a floating-point value represented by double.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
