![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 3
| usage: as.exe value1 value2 value3 Code: if(argv[2]=="x"){
printf("worked\n");
}else{printf("not worked\n");}
=P does any one can help me on this? thanks |
| jonathanrs is offline | |
| | #2 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,368
| Post the smallest and simplest compilable program that demonstrates the problem.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is online now | |
| | #3 |
| Guest Join Date: Aug 2001
Posts: 4,923
| You need to use strcmp to compare strings. Even so, the test should have failed since argv[2] most certainly does not have the same address as the string literal. |
| Sebastiani is offline | |
| | #4 |
| Registered User Join Date: Jan 2008 Location: Seattle
Posts: 476
| What Sebastiani stated, but on a tangent you may want to review the use of getopt(). Example of Getopt - The GNU C Library |
| slingerland3g is offline | |
| | #5 |
| Registered User Join Date: Jul 2009
Posts: 3
| thanks 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: Code: if(strcmp(argv[2],"x")==FALSE) {
printf("worked");}else{printf("error");}
thanks ^^ |
| jonathanrs is offline | |
| | #6 |
| Guest Join Date: Aug 2001
Posts: 4,923
| >> 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. |
| Sebastiani is offline | |
| | #7 |
| Registered User Join Date: Jul 2009
Posts: 3
| sorry Code: main()
{
char var[8]="abcdefgh";
if(strcmp(var,"abcdefgh")==1){printf("true\n");
system("pause");
}else{printf("false\n");system("pause");}
}
thanks |
| jonathanrs is offline | |
| | #8 |
| Registered User Join Date: Sep 2008 Location: Toronto, Canada
Posts: 507
| 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. Last edited by nonoob; 08-01-2009 at 03:14 PM. |
| nonoob is offline | |
| | #9 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| 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: Code: char str[] = "My string";
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
![]() |
| Tags |
| command line, line arguments, problem, solution, typecasting |
| Thread Tools | |
| Display Modes | |
|