Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
I started off in one book, learning to write C, and my programs always started like this....

Code:
#include <stdio.h>
mani{

int 

return 0; 
}

But when posting problems I was having in forums, I saw a lot of people do this...

Code:
#include <stdio.h>

int main (int argc, const char * argv[]) {
}

Now I'm using "Learn C on the Mac" as a supplementary text, and in that book, they always default with the
int main (int argc, const char * argv[])

However, they haven't explained any of that.

Would anyone mind telling me why some of you, and this book does that? Rather than what I've been doing? Is this sort of a "default" method of starting each C program, unless I know of exceptions I want otherwise??

What are these (int argc, const char * argv[]) giving me the ability to do?

I assume those are two int's, because I see only one comma. the two being
argc and
const char * argv[]


Thanks
Scott
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
What are these (int argc, const char * argv[]) giving me the ability to do?
They are giving you the ability to pass arguments to your programs via the command line. argc is an int that counts the number of arguments in argv, argv is an array of array of char (C strings).

e.g. http://publications.gbdirect.co.uk/c_book/chapter10/arguments_to_main.html

The int before main is the thing that lets main use "return 0" to indicate success (or something else to indicate failure, or some other condition). The exit status of your program can be accessed from the shell. If you don't enter it, it is implied, but it is good form to enter it so you remember what main returns.

B
 

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
They are giving you the ability to pass arguments to your programs via the command line. argc is an int that counts the number of arguments in argv, argv is an array of array of char (C strings).

e.g. http://publications.gbdirect.co.uk/c_book/chapter10/arguments_to_main.html

The int before main is the thing that lets main use "return 0" to indicate success (or something else to indicate failure, or some other condition). The exit status of your program can be accessed from the shell. If you don't enter it, it is implied, but it is good form to enter it so you remember what main returns.

B

You've got a new profile picture....nice, makes me hungry!!

One of my questions I'm still curious about, should I use this by default when writing my C programs from now on? Even though I haven't learned these yet, if they are to be used, I can still use them now and get in the habit of using them with every C program I write. Can it ever "hurt" to use them? Like certain situations where they will mess things up??
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
You've got a new profile picture....nice, makes me hungry!!

...

Can it ever "hurt" to use them?

I'm only an Angus Deluxe for the day. You're a Premium Salad.

I would suggest that it's more important to do "int main" than include argc/argv as a good habit. Xcode's C program template will give you the whole shebang, so if you use Xcode you get it for free.

No harm done if you include the arguments but don't use them.

B
 

KnightWRX

macrumors Pentium
Jan 28, 2009
15,046
4
Quebec, Canada
In C89, there are 2 valid declarations of main :

int main(void)
int main(int argc, char * argv[])

There's 2 principles that are at work here, and you should read about them in a chapter on Functions in any of your books.

First, there's the "function type", which essentially declares what kind of value your function must return. In the case of the main function, this value is returned to the calling process of the OS, and should always be of int type. Notice that we also use EXIT_SUCCESS or EXIT_FAILURE, which are both defined in stdlib.h. This is the standard way of reporting either success or failure to the OS for the execution. Using 0 or 1 might make your code fail on certain platforms where EXIT_SUCCESS is not 0.

The other principle is function arguments. Again, these would be explained in the chapter on functions and is a way to pass local variables from one to another function without changing their scope to global.

The reason there are standard declarations is because a compiled/linked C program rarely starts executing in your main function. Each linker usually links a "runtime" to your binary executable which is the actual entry point of the program. In GCC, that would be the crt0.o object file, in Visual Studio and Microsoft's environnement msvcrt.dll. This code executes before your main and is responsible for the function call to main(). On some platforms, this runtime cannot find the main() symbol if it is not declared in the ANSI standard ways.

The valid uses and reasons to not use any other declaration of main are explained in questions 11.12a to 11.15 of the comp.lang.c FAQ, a site you should have bookmarked long ago. It is a very good reference to learning C :

http://c-faq.com/ansi/maindecl.html
 

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
I'm only an Angus Deluxe for the day. You're a Premium Salad.

I would suggest that it's more important to do "int main" than include argc/argv as a good habit. Xcode's C program template will give you the whole shebang, so if you use Xcode you get it for free.

No harm done if you include the arguments but don't use them.

B

Ahhh, hence is why I see some posters with that in their code. Their probably copy and pasting from Xcode to this forum. And my newest book is "Learning C on the Mac", and of course everything is in X code. Okay, well now I know why they're all using it, especially if Xcode's including it automatically.

I'm a premium salad huh? I'm not much of a salad guy, but I'll take a Carl's Jr. Mushroom and Swiss burger!

Thanks Balamw! I've got a few profile pics selected for the day when I'm given the privilege!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.