Tuesday 19 September 2017

C program to add numbers and execute using cmd line arguments

-Write the code in the editors like vc edit in fedora
-Example add.c

#include<stdio.h>
void main(int argc , char * argv[])  
{
  int i,sum=0; if(argc!=3)  
{
printf("enter two numbers numbers.");  
exit(1);
  }
  printf("The sum is : ");
  for(i=1;i<argc;i++)
sum = sum + atoi(argv[i]);
printf("%d",sum);  
}

-Save the file.
-execute the file like
C:bin> cc add.c
C:bin> ./a.out 10 20

output : 30

 


No comments:

Post a Comment