What is the problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the problem

#include <stdio.h> #include<stdlib.h> #include<string.h> int main(int argc, char *argv[]) { char *o; int n1,n2; o=argv[1]; n1= atoi(argv[2]); n2= atoi(argv[3]); printf("%d",n2); if(strcmp(o,"add") == 0) { printf("%d",n1+n2); } else if(strcmp(o,"substract") == 0) { printf("%d",n1-n2); } else if(strcmp(o,"multiply") == 0) { printf("%d",n1*n2); } else if(strcmp(o,"devide") == 0) { printf("%d",n1/n2); } return 0; }

29th Sep 2022, 9:50 AM
ANGROSH
3 Answers
+ 1
ANGROSH , Unfortunately here on sololearn you can't pass command line arguments to the program (if somebody knows how, please correct me). So you can't do: n = atoi(argv[2])... this causes a segmentation fault because argv[2] and above doesn't exist. Try your code on https://www.onlinegdb.com/ it will work... BTW if you expect command line arguments ALWAYS check that they're supplied by checking argc value.
30th Sep 2022, 6:39 AM
Tina
Tina - avatar
0
Hi
29th Sep 2022, 9:50 AM
ANGROSH
0
Please LINK YOUR CODE. Go to Code section, click +, select the programming language, insert your code and save. Come back to this thread, click +, Insert Code, sort for My Code Bits, select your code.
29th Sep 2022, 9:56 AM
Lisa
Lisa - avatar