How to pass command line arguments to main() through GDB? - C
Posted 01-10-2012 at 06:12 AM by Anisha Kaul
Updated 02-03-2012 at 05:32 AM by Anisha Kaul (Thanks to Mr.Code for pointing out the nonsense)
Updated 02-03-2012 at 05:32 AM by Anisha Kaul (Thanks to Mr.Code for pointing out the nonsense)
test.c
Passing command line arguments to main() through GDB:
Code:
#include <stdio.h>
#include <string.h>
int main (int argc, char *argv[])
{
if (argc >= 2)
{
int i;
for (i = 1; i < argc; i++)
printf ("\nHaalloo! %s", argv[i]);
}
else
printf ("\nForgot to key in something?");
return 0;
}
Code:
anisha@linux-dopx:~/> gcc -g test.c anisha@linux-dopx:~/> gdb a.out -silent Reading symbols from /home/anisha/a.out...done. (gdb) r Starting program: /home/anisha/a.out Forgot to key in something? Program exited normally. (gdb) r anisha kaul Starting program: /home/anisha/a.out anisha kaul Haalloo! anisha Haalloo! kaul Program exited normally. (gdb)
Total Comments 3
Comments
-
...just my
(which aren't worth a whole lot at this point; I don't do a whole lot of programming anymore
):
IMHO the format string in your printf is unnecessary. Don't get me wrong, I'm sure it's good to get in the habit of including it for instances where it is necessary, but I feel that something like:
...would work just as well in this particular instance.Code:if(argc == 2) printf("Haalloo!\n");
...and speaking of instances where the format string is needed, it might be a good idea to introduce the concept of actually using the CLI parameter strings in the program by e.g. echoing them back to the user:
...just a couple helpful tips/suggestions.Code:#include <stdio.h> #include <string.h> int main (int argc, char *argv[]) { if (argc == 2) { printf ("Haalloo! You have entered: "); int i; for (i = 1; i < argc; i++) printf ("%s, ", argv[i]); printf ("and that's all, folks! :)\n"); } else printf ("Forgot to key in something?\n"); return 0; }
Posted 02-03-2012 at 04:29 AM by MrCode
-
Thanks for pointing out the silly parts of the code, I'll update it soon.
also, I think, the red code below would make more sense, for the yellow to exist.
Code:#include <stdio.h> #include <string.h> int main (int argc, char *argv[]) { if (argc >= 2) { printf ("Haalloo! You have entered: "); int i; for (i = 1; i < argc; i++) printf ("%s, ", argv[i]); printf ("and that's all, folks! :)\n"); } else printf ("Forgot to key in something?\n"); return 0; }Posted 02-03-2012 at 05:04 AM by Anisha Kaul
Updated 02-03-2012 at 05:13 AM by Anisha Kaul -
Yeah, whoops.Quote:also, I think, the red code below would make more sense, for the yellow to exist.
Like I said, it's been a while since I've really done anything "serious" with programming lately, so I'm bound to make little errors like that. Thanks for the correction, though.
Posted 02-03-2012 at 05:19 AM by MrCode



