Hi

I am newbee to C

Can anybody explain about int main(int argc,char* argv[])

Advance Thanks for helping

Recommended Answers

All 6 Replies

main is the main entry point of your program.

its return value is int because it can return an integer to say whether it has excuted sucessfully or failed or whatever. Its also what the standard specifies.

The paramaters (int argc,char* argv[]) are the command line paramaters passed to the program

Hi

I am newbee to C

Can anybody explain about int main(int argc,char* argv[])

Advance Thanks for helping

I would forward you to my C crash course, but I haven't added that section just yet (later tonight maybe).

So let's discuss it. The main() function always returns a value, which is the exit code for the process. Typically 0 means success, and something other than zero means the process terminated because of an error.

argc stores the number of parameters that are given on the command line. If you're writing a GUI, then you probably won't need them. argv is a null-terminated array of pointers to the parameters/options passed to your program.

For instance, if your program is called test, and you run it as follows:

test a b c

argv[0] will store "test" (the name of the program),
argv[1] will be "a"
argv[2] will be "b"
argv[3] will be "c"
argv[4] will be NULL
and argc will be 4

Try this program out (I used char **argv as a matter of preference, *argv[] and **argv mean the same thing):

#include <stdio.h>

int main(int argc, char **argv){
	int i=0;

	printf("argc=%d\n",argc);
	while(argv[i]){
		printf("argv[%d]=\"%s\"\n",i,argv[i]);
		i++;
	}
		printf("argv[%d]=NULL\n",i);

}

When this section is finished in my C crash course, you'll find this topic covered under "More on main()".

This definition for main is equally valid (but you won't have argv,argc to use):

int main(){
}

or even this:

int main(void){
}

This IS NOT valid:

void main(){
}

The reason for this is that main() has to return a value regardless, and that value is always of type int.

When you execute a program you can include arguments on the command line.
The run time environment will create an argument vector.
argv is the argument vector
argc is the number of arguments
Argument vector is an array of pointers to strings.
a string is an array of characters terminated by a binary 0 (NULL or ‘\0’).
argv[0] is always the program name, so argc is at least 1.

>argv is the argument vector
>argc is the number of arguments
Note that argc and argv are simply conventional names. You can change them. In fact, I see av and ac quite a bit as alternate names.

>Argument vector is an array of pointers to strings.
It's an array of pointers to char where each pointer to char represents a string (ie. it's terminated with a null character). Or rather, since it's a function parameter, it's a pointer to the first element in an array of pointers to char. That's why char *argv[] and char **argv are equivalent.

>a string is an array of characters terminated by a binary 0 (NULL or ‘\0’).
NULL isn't equivalent to '\0' because it can be defined as a null pointer constant ((void*)0), which isn't required to have the same representation. I highly recommend you avoid using the NULL macro in anything except pointer contexts.

>argv[0] is always the program name, so argc is at least 1.
Incorrect. argc is allowed to be 0, and even when argc is 1, argv[0] can be an empty string if the program name isn't available.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.