The main () function with parameters in C language, main

Source: Internet
Author: User

The main () function with parameters in C language, main

Sample Code:

1 # include <stdio. h> 2 3 int main (int argc, char * argv []) 4 {5 int I; 6 printf ("Total % d arguments \ n", argc ); 7 for (I = 0; I <argc; I ++) 8 {9 printf ("Argument % d = % s \ n", I + 1, argv [I]); 10} 11 12 system ("pause"); 13 return 0; 14}

Parameter description:

Argc: number of parameters. If a parameter is not passed to the main () function, the default value is 1. That is, at least one parameter is the name of the executable file (including the directory ).

Argv: A pointer array pointing to the first address of each string parameter, where argv [0] stores the first address of the executable file name.

Save the preceding code as main.c and compile it. Generate the executable file main.exe (under the root directory of drive d). Execute the following command on the cmd command line:

D: \> main.exe hello world
Total 3 arguments
Argument 1 = main.exe
Argument 2 = hello
Argument 3 = world
Press any key to continue...

Tip: In win7, click Start. In the search box, Enter cmd and press enter to open the cmd window. Enter D at the prompt to switch to the root directory of drive D.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.