a little help!
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!
does any one can help me on this?
Post the smallest and simplest compilable program that demonstrates the problem.
You need to use strcmp to compare strings. Even so, the test should have failed since argv most certainly does not have the same address as the string literal.
Originally Posted by jonathanrs
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! o0
>> 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="abcdefgh", the strcmp() function will give me printf("false");
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. 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.
char str = "My string";