Learn point C language (41): function

Source: Internet
Author: User
Tags printf

The main function is the entry point of C program, which is indispensable and cannot be duplicated.

The main function should return an integer value to the operating system, and a return of 0 indicates a normal end.

The main function used earlier is all without arguments: int main (void);

Main can have two parameters: int main (int argc,char *argv[]);

The first is the number of parameters that are automatically fetched (+1), and the second is the argument list (a list of strings);

This is generally used for console or DOS,

When we double-click the filename to open the file under Windows, the file name (including the path) is passed through these parameters.

1. Gets the default value for the first parameter of the main function:

It would be 1; We didn't give it a parameter? It turns out to be the first parameter of the file name.

#include <stdio.h>

int main(int argc,char* argv[])
{
  printf("%d\n",argc); /* 1 */
  getchar();
  return 0;
}

2. Get the first parameter (that is, the filename):

#include <stdio.h>

int main(int argc,char* argv[])
{
  printf("%s\n",argv[0]);
  getchar();
  return 0;
}

3. Create and get a list of parameters for the main function:

If it is in the console or DOS, after the program name input is, now?

C++builder menu-> Run-> Parameters ...-> enter test parameters (for example: 11 22 33) in Parameters, separated by spaces.

This dialog box can also be entered from Project-> project Options-> Debuger.

#include <stdio.h>

int main(int argc,char* argv[])
{
  int i;
  for (i = 1; i < argc; i++) {
    printf("%s\n",argv[i]);
  }

  printf("---\n");

  while (--argc) {
    printf("%s\n",argv[argc]);
  }

  getchar();
  return 0;
}

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.