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


I’ve made it mostly headache-free through 40 pages so far without any programming background but still haven’t read any description of what this all means:

Can someone explain it piece by piece like I’m eight?

Thanks a lot.


I think I can explain like you’re an eight year old. First, keep you hands to yourself and quit bothering your sister.

The line is a function definition, just like ay other plain old C function. It has been passed down through time, and the first people to use it were creating command line programs. For instance, it might have been for the program “cp”, the command line program used to copy files. Here’s how cp might be used in a terminal window:

Next, pinchee, please take your finger out of your nose.

Here’s the code you’re asking about:

Let look at this as if it were for the cp program:

“int” is the data type integer, which the CP program will return upon completion. If cp returns a zero, everything worked. If it returns > zero, something failed.

“main” is the name of the main body of the program “cp”. It’s tradition.

() - the parans enclose the two arguments the operating system will pass to cp when it starts.

“int argc” is the first argument, it tells how many strings are on the command line.

“const char * argv[]” is an array of pointers to strings. The strings are the commands from the command line.

So for the cp example above,

argc contains the integer 4.

argv[0] contains ‘cp’

argv[1] contains ‘-R’

argv[2] contains ‘Documents’

argv[3] contains ‘“Documents backup”’

So its all about sending data to the program through the command line. If your programs don’t launch from the command line, you’ll never need or want to use it.

And put that candy down, it will spoil your dinner.


Also the int main line comes around and gets explained at the very end of the book!

Just keep on pushing ahead and you’ll get there. :smiley: