| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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)
__________________
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. |
|
#3
|
|||
|
|||
|
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) |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
||||
|
||||
|
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. |
|
#7
|
|||
|
|||
|
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.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > A simple and random question |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|