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; }
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; }
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
You can't test strings using ==, use strcmp in <string.h> instead:
Code:if (strcmp(argv[1], "hello") == 0) printf("%s\n", argv[1]);
My best code is written with the delete key.
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.
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
>#define C(a,b) !strcmp(a,b)
That's truly fugly.
My best code is written with the delete key.
stfu noob.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'.
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
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".
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
>>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?
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
Hi guys, thx for fast replies, very much appreciated!!!
Give Brian a break, he's afterall have gd intention.
I've been getting compilation error: "parse error before xxx". What does it mean?
PS: BTW is argv a matrix array of pointers?
Last edited by EeeK; 09-06-2003 at 08:30 PM.
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 } } }
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }