Program to print sum of integers through command line arguments
#include<stdio.h> #include<stdlib.h> main(int argc,char **argv) { int *a,i,sum=0; a=(int *)malloc(argc*sizeof(int *)); for(i=1;i<argc;i++) a[i]=malloc(argc*sizeof(int)); for(i=1;i<argc;i++) { a[i]=atoi(argv[i]); sum=sum+a[i]; } printf("sum=%d\n",sum); }OutPut:
madan@madan-Lenovo-G570:~/madan$ cc array18.c madan@madan-Lenovo-G570:~/madan$ ./a.out 10 20 sum=30 madan@madan-Lenovo-G570:~/madan$ ./a.out 10 20 40 50 60 sum=180