Tuesday 19 September 2017

c programs to add two numbers using cmd line arguments

-Write the code in the editors like vc edit in fedora

-Example add.c

-Save the file.

#include<stdio.h>
#include<stdlib.h>
int main(int argc, char * argv[]) {
       int i, sum = 0;

       if (argc != 3) {
              printf("You have forgot to specify two numbers.");
              exit(1);
       }
     printf("The sum is : ");
    sum= atoi(argv[1])+atoi(argv[2]);
     printf("%d", sum);
    return 0;

}


-execute the file like

C:bin> cc add.c

C:bin> ./a.out 10 20


output : 30
 

No comments:

Post a Comment