C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 12th, 2005, 11:35 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 9 m 3 sec
Reputation Power: 4
Send a message via Yahoo to BloodlustShaman
A simple and random question

Well I have got books and this has past my mind and I wonder whats the difference between

Code:
int main()


and this

Code:
int main( int nNumberofArgs, char* pszArgs[])


cause when I got the book that shows the second one it says no reason of y it gots the things in the middle

also I wanted to know so that in the future when I write my programs I can pick which one

also the book always uses the second one even when they talk about the int main

plz reply i want to know

Reply With Quote
  #2  
Old October 13th, 2005, 01:31 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 992 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 743193 Folding Title: Super Ultimate Folder - Level 2Folding Points: 743193 Folding Title: Super Ultimate Folder - Level 2Folding Points: 743193 Folding Title: Super Ultimate Folder - Level 2Folding Points: 743193 Folding Title: Super Ultimate Folder - Level 2Folding Points: 743193 Folding Title: Super Ultimate Folder - Level 2Folding Points: 743193 Folding Title: Super Ultimate Folder - Level 2Folding Points: 743193 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 13 h 38 m 16 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
The second case is the one you use if you want your program to read parameters from the command line.

If your that program is called, nNumberofArgs contains the number of parameters, and pszArgs is an array of nNumberofArgs strings, containing all of the parameters.
try this snippet for a demonstration (just compile it, and run it with different stuff behind it on the command line)
c Code:
Original - c Code
  1. #include <stdio.h>
  2.  
  3. int main(int argc, char *argv[])        // note the two parameters, an integer and an array of constant strings
  4. {
  5.     // declarations
  6.     int i;
  7.  
  8.     // read all parameters and print them to stdout
  9.     for(i = 0; i < argc; i++)
  10.         printf("Argument nr. %d is: %s\n", i, argv[i]);
  11.  
  12.     // exit
  13.     return 0;
  14. }
__________________
This is my code. Is it not nifty?

"The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools."
---Douglas Adams


Join the Itsacon fanclub!    
Zero Tolerance: Spammers banned so far: 275

Last edited by Itsacon : October 14th, 2005 at 04:24 PM.

Reply With Quote
  #3  
Old October 13th, 2005, 08:41 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 9 m 3 sec
Reputation Power: 4
Send a message via Yahoo to BloodlustShaman
First-Thanks for your time Itsacon

K now to go to business k so I compile it and my first question is what compiler do you have? Cause I had to had a system PAUSE so it will let me see the program but thats not quiet important

What is important is what came out of the program which it was



Argument nr. 0 is : (the place where the program is saved on my computer)
Press any key to continue...

K well first let me say what I understand and what I don't

The Argument nr. part I understand cause it was written on the section of printf

but I don't know is where the %d and %s is it like a rule for the stdio cause I am new to that (I usually use iostream)

so thats pretty much it plz explain to me I really appreciate your time (and I really mean I dont say stuff cause to be nice when I say something nice I mean it)

Reply With Quote
  #4  
Old October 14th, 2005, 03:01 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 992 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 743193 Folding Title: Super Ultimate Folder - Level 2Folding Points: 743193 Folding Title: Super Ultimate Folder - Level 2Folding Points: 743193 Folding Title: Super Ultimate Folder - Level 2Folding Points: 743193 Folding Title: Super Ultimate Folder - Level 2Folding Points: 743193 Folding Title: Super Ultimate Folder - Level 2Folding Points: 743193 Folding Title: Super Ultimate Folder - Level 2Folding Points: 743193 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 13 h 38 m 16 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
You need to add the pause because you probably start it from windows. If you open a dos box (start->run->cmd in 2000/XP, start->run->command in Win98), go to the proper directory and start the program, the output will still be visible, even though the program is finished.

Try running the program like <program name> arg1 arg2 arg3

it should return something like:

Argument nr. 0 is : (the place where the program is saved on your computer)
Argument nr. 1 is : arg1
Argument nr. 2 is : arg2
Argument nr. 3 is : arg3
Press any key to continue...

as for the %d and %s, these are indeed options for the printf() function.
%d means: here I want to insert an integer
%s means: here I want to insert a string
you then give those parameters to the function, after the string with the options in it (in the correct order)
so in my example:
Code:
printf("Argument nr. %d is: %s\n", i, argv[i]);

I tell the computer output a string with an integer and a string in it, and the value of the integer is i, and the value of the string is argv[i].

printf() and other versions like fprintf() to print to files, etc have of course many more of these options (for floats, for specifying the width you want the output to have, etc)
Look here for more information.

Oh, and indeed, the first entry of the array argv[] is indeed the name of your program.
This can be useful for instance if you are outputting help files.

Reply With Quote
  #5  
Old October 14th, 2005, 11:17 AM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 4
Send a message via AIM to Geo.Garnett
Thanks for that definition of main(int argc, int * argv[]) I too have been having problems understanding this(and that helped a lot). Mostly because it only worked when I used visual studio as a compiler, because it would make a debug file or a final version depending on the compiler settings, and then I could just use the debug or final version and parameters from the command line, or from run. Now that I have been using Dev-C++ it doesn't work for some reason. I don't use that way of coding anyways, I mean I understand the concept but I lack the knowledge of how to use it correctly. You should be a school teacher Itsacon, your pretty good at this describing things so they are understandable to just about anyone.
__________________
---Official Member Of The Itsacon Fan Club---
Give a man a fish and he will eat for a day. Teach a man to fish and he will sit in a boat all day drinking beer.

Reply With Quote
  #6  
Old October 14th, 2005, 04:28 PM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 992 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 743193 Folding Title: Super Ultimate Folder - Level 2Folding Points: 743193 Folding Title: Super Ultimate Folder - Level 2Folding Points: 743193 Folding Title: Super Ultimate Folder - Level 2Folding Points: 743193 Folding Title: Super Ultimate Folder - Level 2Folding Points: 743193 Folding Title: Super Ultimate Folder - Level 2Folding Points: 743193 Folding Title: Super Ultimate Folder - Level 2Folding Points: 743193 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 13 h 38 m 16 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
Oh, and to answer your other question, my compiler is the GCC, and the reason I don't need the pause() is because I do all my compiling and running from the command line... Command Line Warriors Forever!!!
Comments on this post
B-Con agrees!

Reply With Quote
  #7  
Old October 14th, 2005, 07:52 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 9 m 3 sec
Reputation Power: 4
Send a message via Yahoo to BloodlustShaman
K thanks for the help Itsacon- and I agree you should be a teacher or some net guy that gets paid well thanks now I understand but for the output of the program Ill fix it later but atleast I learned of the %s %d.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > A simple and random question


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway