Command Line Arguments

📘 C 👁 31 views 📅 Dec 22, 2025
⏱ Estimated reading time: 1 min

Command line arguments allow values to be passed to a C program at the time of execution from the command prompt.


main() with Command Line Arguments

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

Explanation

  • argc – Argument count (number of arguments)

  • argv – Argument vector (array of strings)

  • argv[0] – Program name

  • argv[1] onwards – User-supplied arguments


Example Program

#include int main(int argc, char *argv[]) { int i; printf("Number of arguments: %d\n", argc); for (i = 0; i < argc>printf("Argument %d: %s\n", i, argv[i]); } return 0; }

Sample Execution

program.exe Hello C

Output

Number of arguments: 3 Argument 0: program.exe Argument 1: Hello Argument 2: C

Converting Command Line Arguments

  • Arguments are strings

  • Use functions like atoi(), atof()

int num = atoi(argv[1]);

Example (Add Two Numbers)

#include #include 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; }

Advantages

  • No need for user input during execution

  • Useful in batch processing

  • Flexible program execution


Summary

  • Command line arguments are passed during execution

  • argc stores count, argv stores values

  • Arguments are strings by default

  • Conversion functions are required


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes