I noticed that comparing a normal string variable with a string using strcmp and == operator both works, but comparing argv string using == doesn't works, only it works with strcmp. why? What is so special?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *argv[]) {
char *tempStr = "string";
int x = atoi(argv[2]), y = atoi(argv[3]);
if (strcmp(tempStr, "string") == 0) {
printf("'strcmp' methods works in case comparing with normal string variable\n");
}
if (tempStr == "string") {
printf("'strcmp' as well as '==', both the methods works in case comparing with normal string variable\n");
}
/* this works for argv[] strings*/
if (strcmp(argv[1], "add") == 0) printf("%d", x + y);
else if (strcmp(argv[1], "subtract") == 0) printf("%d", x - y);
else if (strcmp(argv[1], "multiply") == 0) printf("%d", x * y);
else if (strcmp(argv[1], "divide") == 0) printf("%d", x / y);
// /* this doesn't works for argv[] strings */
// if (argv[1] == "add") printf("%d", x + y);
// else if (argv[1] == "subtract") printf("%d", x - y);
// else if (argv[1] == "multiply") printf("%d", x * y);
// else if (argv[1] == "divide") printf("%d", x / y);
return 0;
}