What does main get?

In C, the main function typically takes two parameters: argc (argument count) and argv (argument vector). These parameters allow you to pass command-line arguments to your program.

Here's a simple example to illustrate how to use these parameters:

#include <stdio.h>

int main(int argc, char *argv[]) {
    // Check if there are command-line arguments
    if (argc > 1) {
        // Print the program name
        printf("Program name: %s\n", argv[0]);

        // Print the command-line arguments
        printf("Arguments passed:\n");
        for (int i = 1; i < argc; i++) {
            printf("%d: %s\n", i, argv[i]);
        }
    } else {
        printf("No command-line arguments passed.\n");
    }

    return 0;
}

In this example, argc represents the number of command-line arguments, and argv is an array of strings containing those arguments. argv[0] is the program name, and the subsequent elements (argv[1], argv[2], etc.) are the actual command-line arguments.

Compile and run the program with command-line arguments to see how it works. For example:

./program_name arg1 arg2 arg3

In this case, argc would be 4, and argv would be an array containing the program name and the three arguments.

Keep in mind that the main function's signature must be one of the following:

int main(void)

or

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

The return type can be void or int, and argv is an array of strings (char*).

0
Subscribe to my newsletter

Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jyotiprakash Mishra
Jyotiprakash Mishra

I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.