i'm new to programming and i was trying to compile this code:
usage: as.exe value1 value2 value3
but it always show me it worked!Code:if(argv[2]=="x"){
printf("worked\n");
}else{printf("not worked\n");}
=P
does any one can help me on this?
thanks
Printable View
i'm new to programming and i was trying to compile this code:
usage: as.exe value1 value2 value3
but it always show me it worked!Code:if(argv[2]=="x"){
printf("worked\n");
}else{printf("not worked\n");}
=P
does any one can help me on this?
thanks
Post the smallest and simplest compilable program that demonstrates the problem.
What Sebastiani stated, but on a tangent you may want to review the use of getopt().
Example of Getopt - The GNU C Library
sorry people! something really weird happened to this computer while trying to compile this code, or it just didn't want to follow my commands. =P
i'd tried everything and this:
always returned true! but today i've compiled this code again and it worked! o0Code:if(strcmp(argv[2],"x")==FALSE) {
printf("worked");}else{printf("error");}
thanks ^^
>> always returned true! but today i've compiled this code again and it worked! o0
Post the entire program. And strcmp doesn't return false/true. Always be sure to read the documentation before attempting to use any function/library.
When i said it always returned true, i was talking about 1 and 0.The function returns an int. That's the code. If i change the size of var,for ex. var[10]="abcdefgh", the strcmp() function will give me printf("false");Code:main()
{
char var[8]="abcdefgh";
if(strcmp(var,"abcdefgh")==1){printf("true\n");
system("pause");
}else{printf("false\n");system("pause");}
}
thanks
Um, strcmp() returns negative integer, zero (0), or a positive integer depending on how its two arguments compare with each other... i.e. A < B, A ==B, or A > B respectively.
The way you've coded it, you are interpreting a +1 as "true". When A > B you could get any positive integer - not necessarily limited to +1.
Perhaps you are getting your "false" because the strings are indeed equal.
You should probably allow for the null terminator - so do var[9]. I'm surprised the compiler didn't whine.
It's actually valid to put a string without a string literal into an array in C, so it's no wonder it doesn't complain.
However, you must ALWAYS null terminate strings if you want to use string functions with it. Don't specify the size; instead, let the compiler figure it out:
Also, main must return int.Code:char str[] = "My string";