LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Blogs > TheIndependentAquarius
User Name
Password

Notices



Rate this Entry

How to pass command line arguments to main() through GDB? - C

Posted 01-10-2012 at 07:12 AM by TheIndependentAquarius
Updated 02-03-2012 at 06:32 AM by TheIndependentAquarius (Thanks to Mr.Code for pointing out the nonsense)

test.c
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;
}
Passing command line arguments to main() through GDB:
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) 
Posted in GNU GDB
Views 11186 Comments 3
« Prev     Main     Next »
Total Comments 3

Comments

  1. Old Comment
    ...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:

    Code:
    if(argc == 2)
        printf("Haalloo!\n");
    ...would work just as well in this particular instance.

    ...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:

    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;
    }
    ...just a couple helpful tips/suggestions.
    Posted 02-03-2012 at 05:29 AM by MrCode MrCode is offline
  2. Old Comment
    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 06:04 AM by TheIndependentAquarius TheIndependentAquarius is offline
    Updated 02-03-2012 at 06:13 AM by TheIndependentAquarius
  3. Old Comment
    Quote:
    also, I think, the red code below would make more sense, for the yellow to exist.
    Yeah, whoops.

    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 06:19 AM by MrCode MrCode is offline
 

  



All times are GMT -5. The time now is 08:07 AM.

Main Menu
Advertisement

Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration