Friday, October 19, 2018

Command Line Arguments - Add numbers

Command Line Arguments

Following C program will Add two numbers passed as Command Line Arguments .
[Note: This program is saved with filename: "addnumbers.c" in C: drive.]

#include <stdio.h>

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

  n1 = atoi(argv[1]);
  n2 = atoi(argv[2]);

  if( argc != 3 ) 
{
    printf("Pl. enter three arguments..\n");
  }
  else
    printf("%d", n1+n2);

  return 0;
}

Output of program [Run this program from Command Prompt of your system]

C:\addnumbers 5 5
Total = 10


No comments:

Post a Comment