C PROGRAMMING PROGRAM • FUNCTIONS, PREPROCESSOR & COMMAND LINE
Read Command-Line Arguments
Learn how to read command-line arguments using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
No input required
Sample output
No command-line arguments supplied.
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
int main(int argc, char *argv[])
{
if (argc == 1) {
puts("No command-line arguments supplied.");
return 0;
}
printf("Argument count = %d\n", argc - 1);
for (int index = 1; index < argc; index++)
printf("Argument %d = %s\n", index, argv[index]);
return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
No command-line arguments supplied.
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Use argc for the number of tokens and argv for the string stored at each command-line position.
- Display the computed result.
Use argc for the number of tokens and argv for the string stored at each command-line position.
EFFICIENCY
Time and space complexity
Time complexity
O(n)
Auxiliary space
O(1)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
