Command-Line Input with argc and argv in C

 Command-Line Input with argc and argv in C


Command-line arguments can be passed to C programs when executing them from the command line. The argc and argv parameters of the main function are used to access these arguments.


#include <stdio.h>


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

    printf("Number of arguments: %d\n", argc);


    printf("Arguments:\n");

    for (int i = 0; i < argc; i++) {

        printf("%d: %s\n", i, argv[i]);

    }


    return 0;

}

When executing the program from the command line, you can pass arguments after the program name:



./program arg1 arg2 arg3

The program will print the number of arguments and list them with their respective indices.


No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...