Assuming the program name is "test.exe", why the if statement is not true when I enter "test hello"?
Code:include <stdio.h>
int main(int argc, char *argv[])
{
if(argv[1] == "hello")
printf("%s", argv[1]);
return 0;
}
Printable View
Assuming the program name is "test.exe", why the if statement is not true when I enter "test hello"?
Code:include <stdio.h>
int main(int argc, char *argv[])
{
if(argv[1] == "hello")
printf("%s", argv[1]);
return 0;
}
You're comparing pointers. You need to #include <string.h> and use the strcmp function.
Code:#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
if( strcmp( argv[1], "hello") == 0 )
printf("%s", argv[1]);
return 0;
}
You can't test strings using ==, use strcmp in <string.h> instead:
Code:if (strcmp(argv[1], "hello") == 0)
printf("%s\n", argv[1]);
Code:include <stdio.h>
#define C(a,b) !strcmp(a,b)
int main(int argc, char *argv[])
{
if(C(argv[1],"hello"))
printf("%s", argv[1]);
return 0;
}
ROFL!
So many replies at the same time.
>#define C(a,b) !strcmp(a,b)
That's truly fugly.
stfu noob.Quote:
Originally posted by Prelude
>#define C(a,b) !strcmp(a,b)
That's truly fugly.
ive been writing in c since 1953.
OK. Two (edit: three) things wrong with that flame.
1) You're talking to Prelude. She is not a 'noob'. She is probably one of the best programmers on this board.
2) C was created in the 70s.
3) It truly is 'fugly'. :)
Ive always found it makes more sense, especially for new programmers. strcmp(x,y) == 0 seems like it should mean the strings don't compare.
And I wrote a book on C. My surname is Kerningham, and I rox.
1) Using terms like: stfu, noob, rox, etc is equal to saying, "Please ignore me for I am lower the pond scum."
2) So you wrote a book, you want a cookie or something? You know how many badly written books are in the market?
3) I agree with Prelude and XSquared, truely fugly.
>And I wrote a book on C. My surname is Kerningham, and I rox.
No matches for "Kerningham". Below are matches for "kernighan". :rolleyes:
>>And I wrote a book on C. My surname is Kerningham, and I rox.
Then why is you're e-mail address briancollins@blueyonder.co.uk?
Hi guys, thx for fast replies, very much appreciated!!!
Give Brian a break, he's afterall have gd intention. :D
I've been getting compilation error: "parse error before xxx". What does it mean?
PS: BTW is argv a matrix array of pointers?
If I've a ADT, and I've created a function in the main method to call the ADT, is that wrong? Coz I got this undefined reference compilation errors.
Code:include <stdio.h>
include "adt.h"
void adt(void);
int main()
{
adt();
return 0;
}
void adt()
{
adt x;
create(x);
destroy(x);
...
}
Hard to say without seeing more of the implementation.
Post more code.
But I *will* tell you that assuming that argv[1] is valid is not very wise. If you call the application with no command line, it will crash.
There are two mechanisms we have to tell us how many args there are. The most obvious is argc, which will be greater than 1 if any args were passed. The second is the fact that argv is an array of pointers to strings, and the end of the array is marked with a NULL pointer. So the proper way to handle it is something like:
Code:
int main()
{
if(argc>1)
{
for(argc = 1; argv[argc] != NULL; ++argc)
{
printf("Arg #%d: %s \n", argc, argv[argc]);
// process each arg
}
}
}