Tutorials Home   >   Practical Programming Skills   >   Command-Line Arguments

Command-Line Arguments

1. Introduction

Command-line arguments are values passed to a C program at the time of execution through the command prompt or terminal. Instead of taking input from the keyboard during program execution, command-line arguments allow the user to provide input before the program starts running.

They are very useful in:

  • System programs

  • Utility software

  • File processing programs

  • Automation and scripting


2. Need for Command-Line Arguments

Command-line arguments are used when:

  • Input is required before program execution

  • Multiple inputs need to be passed quickly

  • Programs are executed repeatedly with different values

Example (Real-Life)

copy source.txt destination.txt

Here, source.txt and destination.txt are command-line arguments.


3. main() Function with Command-Line Arguments

In C, command-line arguments are received through the main() function.

Syntax

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

Explanation

  • argc → Argument count

  • argv → Argument vector (array of strings)


4. Understanding argc

  • argc stores the total number of command-line arguments

  • It always has a minimum value of 1

  • argv[0] stores the program name

Example

myprog.exe 10 20

argc = 3


5. Understanding argv

  • argv is an array of character pointers

  • Each element stores one argument as a string

Argument Stored In
Program name argv[0]
First argument argv[1]
Second argument argv[2]

6. Simple Program Using Command-Line Arguments

Example Program

#include <stdio.h>

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

printf(“Total arguments: %d\n”, argc);

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

return 0;
}


7. Passing Numbers as Command-Line Arguments

Command-line arguments are passed as strings, so they must be converted to numbers.

Conversion Functions

  • atoi() – string to integer

  • atof() – string to float


Example: Adding Two Numbers

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int a, b, sum;

a = atoi(argv[1]);
b = atoi(argv[2]);

sum = a + b;
printf(“Sum = %d”, sum);

return 0;
}


8. Checking Number of Arguments

Always validate the number of arguments using argc.

Example

if(argc != 3) {
printf("Usage: program num1 num2");
return 1;
}

9. Command-Line Arguments with Files

Command-line arguments are often used for file handling.

Example

copy input.txt output.txt

Program Example

#include <stdio.h>

int main(int argc, char *argv[]) {
FILE *fp;
fp = fopen(argv[1], “r”);

if(fp == NULL) {
printf(“File not found”);
return 1;
}

printf(“File opened successfully”);
fclose(fp);
return 0;
}


10. Advantages of Command-Line Arguments

  • Faster input

  • No need for user interaction

  • Useful for automation

  • Suitable for large programs


11. Limitations of Command-Line Arguments

  • Limited input size

  • Not user-friendly for beginners

  • Requires correct syntax


12. Common Errors

  • Accessing argv without checking argc

  • Forgetting arguments

  • Passing wrong data type

  • Not converting strings to numbers


13. Real-Life Applications

  • Compiler commands

  • File copy and delete utilities

  • Batch processing programs

  • System tools


14. Best Practices

  • Always check argc

  • Display proper usage message

  • Validate argument values

  • Use meaningful argument names


15. Summary Table

Term Meaning
argc Argument count
argv Argument vector
argv[0] Program name
atoi() String to integer

16. Conclusion

Command-line arguments in C allow programs to receive input at execution time. By using argc and argv, programmers can create flexible and powerful programs that accept inputs quickly and efficiently. This concept is essential for developing system-level and real-world C applications.