Example explanation of the line parameters (argc, argv) of the main function

Directory

foreword

1. Problem description

2. The meaning of line parameters

3. Topic application

1. Code (focus on Chinese comments)

2. Execute the test


Foreword

When doing the problem set–plurality of CS50 Week3, I encountered two line parameters (int argc, string argv[]) in the main function, which caused the code to be unclear at once. It was only after I learned what they meant that I completed the assignment. This article explains the role of argc argv through the plurality example of CS50.

1. Problem description

The homework is as simple as finding the person with the highest number of votes.

All candidate lists are stored in an array, but the names of these people need to be entered through argc and argv.

2. Line parameter meaning

  • arg: argument, parameter.
  • argc: argument count, the number of parameters.
  • argv: argument vector, parameter array.

argv[0] points to the full path name when the program is running;

argv[1] points to the first character string after the program executes the program name in the DOS command;

argv[2] points to the second string after the executable program name;

argv[argc] is NULL.

3. Question application

1. Code (focus on Chinese comments)

#include <cs50.h>
#include <stdio.h>
#include <string.h>

// Max number of candidates
#define MAX 9

// Candidates have name and vote count
typedef struct
{
    string name;
    int votes;
}
candidate;

// Array of candidates
candidate candidates[MAX];

//Number of candidates
int candidate_count;

// Function prototypes
bool vote(string name);
void print_winner(void);

int main(int argc, string argv[])
{
    // Check for invalid usage
    if (argc < 2)
    {
        printf("Usage: plurality [candidate ...]\\
");
        return 1;
    }

    // Populate array of candidates
    candidate_count = argc - 1;
    if (candidate_count > MAX)
    {
        printf("Maximum number of candidates is %i\\
", MAX);
        return 2;
    }
    for (int i = 0; i < candidate_count; i ++ ) //Initialize the candidates array.
    {
        candidates[i].name = argv[i + 1]; //The list of candidates should start from argv[1], because argv[0] is the executable file name, and from argv[1] is through the command line The first variable argument entered.
        candidates[i].votes = 0;
    }

    int voter_count = get_int("Number of voters: ");

    // Loop over all voters
    for (int i = 0; i < voter_count; i ++ )
    {
        string name = get_string("Vote: ");

        // Check for invalid vote
        if (!vote(name))
        {
            printf("Invalid vote.\\
");
        }
    }

    // Display winner of election
    print_winner();
}

// Update vote totals given a new vote
bool vote(string name)
{
    for (int i = 0; i < candidate_count; i ++ )
    {
        if (strcmp(name, candidates[i].name) == 0)
        {
            candidates[i]. votes + +;
            return true;
        }
    }

    return false;
}

// Print the winner (or winners) of the election
void print_winner(void)
{
    int max = 0;
    for (int i = 0; i < candidate_count; i ++ )
    {
        if (max < candidates[i].votes)
        {
            max = candidates[i].votes;
        }
    }
    for (int i = 0; i < candidate_count; i ++ )
    {
        if (max == candidates[i].votes)
        {
            printf("%s\\
", candidates[i].name);
        }
    }
    return;
}

2, execute test

“Plurality” in the command line corresponds to argv[0], “Zhang” corresponds to argv[1], “Li” corresponds to argv[2], and “Sun” corresponds to argv[3]. The method of passing actual parameters to the main function through the command line is actually implemented, which increases the flexibility of the required candidate list.

Reference article:

https://blog.csdn.net/Eastmount/article/details/20413773?ops_request_misc=%7B%22request%5Fid%22%3A%22168492962916800211560794%22%2C%22scm%22%3A%2220140713.1 30102334..%22%7D &request_id=168492962916800211560794 &biz_id=0 &utm_medium=distribute.pc_search_result.none-task-blog-2~all~baidu_landing_v2~default-4-20413773-null-null.142^v87^contro l,239 ^v2^insert_chatgpt &utm_term=main’s argc &spm=1018.2226.3001.4187icon-default.png?t=N4N7https://blog.csdn. net/Eastmount/article/details/20413773?ops_request_misc=%7B%22request%5Fid%22%3A%22168492962916800211560794%22%2C%22scm%22%3A%2220140713.130102334. .%22%7D & amp; request_id=168492962916800211560794 & amp ;biz_id=0 & amp;utm_medium=distribute.pc_search_result.none-task-blog-2~all~baidu_landing_v2~default-4-20413773-null-null.142^v87^control,239^v2^insert_chatgpt & amp;utm_term =main’s argc & amp;spm=1018.2226.3001.4187https://blog.csdn.net/qq_35635374/article/details/124483841?ops_request_misc=%7B%22request%5Fid%22%3A%221684929629168002 11560794%22%2C%22scm %22%3A%2220140713.130102334..%22%7D &request_id=168492962916800211560794 &biz_id=0 &utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend ~default-2-124483841 -null-null.142^v87^control,239^v2^insert_chatgpt & amp;utm_term=main’s argc & amp;spm=1018.2226.3001.4187 Series article catalog prompt: Here you can add the catalog of all articles in the series, the catalog needs Manually add TODO by yourself: After finishing writing, organize the article catalog Series article catalog Preface 1. The meaning of the two formal parameters argc and argv in the main function 2. The calling principle of the two formal parameters argc and argv in the main function 1. Using ROS The main function of a simple function package illustrates the argc parameter 2. The main function of a simple function package in ROS illustrates the argv parameter. ! This article first discusses the meaning and adjustment of the two formal parameters argc and argv in the main function in the process https://blog.csdn.net/qq_35635374/article/details/124483841?ops_request_misc=%7B%22request%5Fid%22%3A%2216849296291680021156079 4% 22%2C%22scm%22%3A%2220140713.130102334..%22%7D & amp; request_id=168492962916800211560794 & amp;biz_id=0 & amp;utm_medium=distribute.pc_search_result.none-task-blog -2~all~sobaiduend~ default-2-124483841-null-null.142^v87^control,239^v2^insert_chatgpt&utm_term=main argc&spm=1018.2226.3001.4187