Wednesday, 9 September 2015

Program to demonstrate command line arguments in C - Addition of two numbers using command line arguments

Program:

#include<stdio.h>

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

   if (argc != 3) {
      printf("You have forgot to type numbers.");
      exit(1);
   }

   printf("The sum is : ");

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

   printf("%d", sum);

}


No comments:

Post a Comment