LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-10-2006, 11:56 AM   #1
Abomb
Member
 
Registered: Dec 2005
Location: ~/
Distribution: Ubuntu
Posts: 156

Rep: Reputation: 30
GTK tutorial question


Hey guys,

I'm trying to teach myself some GTK. I'm currently on this: http://www.gtk.org/tutorial/x406.html

Compiles fine but I don't get the desired response when I run the program.

Code:
$ ./packbox
usage: packbox num, where num is 1, 2, or 3.
I understand that it has something to do with this
Code:
if (argc != 2) {
  fprintf (stderr, "usage: packbox num, where num is 1, 2, or 3.\n");
  /* This just does cleanup in GTK and exits with an exit status of 1. */
  exit (1);
}
But I don't see where argc would be initialized to 2.

Thanks for any reply! Just a n00b trying to learn some programming.
 
Old 05-10-2006, 12:39 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Every C or C++ program will have a function "main ()" that looks like this:
Code:
int
main (int argc, char *argv[])
There's one entry in the "char *argv[]" array for every command-line argument passed to your program (along with the pathname of your program itself, passed in as the first argument).

"int argc" is the number of cmd-line arguments. Argc will always be at least "1" (because the OS will always pass at least the name of your program).

In answer to your question "where does argc come from?" - the OS gives it to you when it begins executing your program.

Does that help?

Last edited by paulsm4; 05-10-2006 at 12:40 PM.
 
Old 05-10-2006, 03:17 PM   #3
Abomb
Member
 
Registered: Dec 2005
Location: ~/
Distribution: Ubuntu
Posts: 156

Original Poster
Rep: Reputation: 30
Hey, thanks for the reply.

Kinda make sense. How would do I get argc to 2? Not too sure I understand by what you mean when you say argc is the number of command line arguments.

Thanks for the help!
 
Old 05-10-2006, 04:33 PM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Here's an example that might help:

1. Here's the program:
Code:
#include <stdio.h>

int
main (int argc, char *argv[])
{
  printf ("argc= %d...\n", argc);
  for (int i=0; i < argc; i++)
  {
    printf ("argv[%d]= %s...\n", i, argv[i]);
  }
  return 0;
}
2. Here's the build (sorry: my linux dev box isn't handy at the moment ;-):
Quote:
C:\temp>vcvars32
C:\temp>cl hello.cpp
3. And finally, here's a sample run (with corresponding sample output):
Quote:
C:\temp>hello a b c
argc= 4...
argv[0]= hello...
argv[1]= a...
argv[2]= b...
argv[3]= c...
4. Here's another example:
Quote:
C:\temp>hello major kong
argc= 3...
argv[0]= hello...
argv[1]= major...
argv[2]= kong...
5. And yet another example:
Quote:
C:\temp>cd \
C:\>c:\temp\hello dr. strangelove or how i learned to stop worrying and love the bomb
argc= 14...
argv[0]= c:\temp\hello...
argv[1]= dr....
argv[2]= strangelove...
argv[3]= or...
<< snipped >>
argv[12]= the...
argv[13]= bomb...
I hope that helps .. PSM

Last edited by paulsm4; 05-10-2006 at 04:35 PM.
 
Old 05-10-2006, 04:53 PM   #5
Abomb
Member
 
Registered: Dec 2005
Location: ~/
Distribution: Ubuntu
Posts: 156

Original Poster
Rep: Reputation: 30
Ahh! Got it! I'm still a n00b at programming, I'm not use to arguments in in main(). I'm just use to doing void main() {}.

Thanks for all your help!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question on NeHe OpenGL/SDL/linux tutorial Moebius Programming 0 12-08-2004 10:05 PM
Question about C - GTK alitrix Programming 4 11-12-2004 04:47 AM
GTK+ and c question mofofish Programming 1 04-13-2004 12:51 PM
GTK+ using C++ Tutorial qcoder Programming 3 02-15-2004 05:06 PM
GTK question starbreaker Linux - Software 1 02-16-2002 12:55 PM


All times are GMT -5. The time now is 08:23 PM.

Main Menu
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