C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-30-2009, 12:14 AM   #1
Registered User
 
Join Date: Jul 2009
Posts: 3
Unhappy a little help!

i'm new to programming and i was trying to compile this code:

usage: as.exe value1 value2 value3
Code:
if(argv[2]=="x"){
printf("worked\n");
}else{printf("not worked\n");}
but it always show me it worked!
=P

does any one can help me on this?

thanks
jonathanrs is offline   Reply With Quote
Old 07-30-2009, 12:23 AM   #2
C++ Witch
 
laserlight's Avatar
 
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   Reply With Quote
Old 07-30-2009, 01:20 AM   #3
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
Quote:
Originally Posted by jonathanrs View Post
i'm new to programming and i was trying to compile this code:

usage: as.exe value1 value2 value3
Code:
if(argv[2]=="x"){
printf("worked\n");
}else{printf("not worked\n");}
but it always show me it worked!
=P

does any one can help me on this?

thanks
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   Reply With Quote
Old 07-30-2009, 08:00 AM   #4
Registered User
 
slingerland3g's Avatar
 
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   Reply With Quote
Old 07-30-2009, 08:41 PM   #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");}
always returned true! but today i've compiled this code again and it worked! o0

thanks ^^
jonathanrs is offline   Reply With Quote
Old 07-30-2009, 08:45 PM   #6
Guest
 
Sebastiani's Avatar
 
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   Reply With Quote
Old 08-01-2009, 02:58 PM   #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");}

}
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");

thanks
jonathanrs is offline   Reply With Quote
Old 08-01-2009, 03:08 PM   #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   Reply With Quote
Old 08-01-2009, 03:18 PM   #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";
Also, main must return int.
__________________
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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Tags
command line, line arguments, problem, solution, typecasting

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 12:20 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22