Home  >  Article  >  Backend Development  >  In C language, is it possible to pass parameters in main() function?

In C language, is it possible to pass parameters in main() function?

WBOY
WBOYforward
2023-08-30 18:49:061326browse

In C language, is it possible to pass parameters in main() function?

Yes, we can give parameters in main() function.

Command line parameters in C are specified after the program name on the system command line, and these parameter values ​​are passed to the program during program execution.

argc and argv are the two parameters that can be passed to the main function.

But when you run the program from the terminal, the main() function is actually called by the operating system (or shell program).

Grammar

The syntax is explained as follows -

int main(int argc, char *argv[]){
   //Code
   return 0;
}

Example

Real-time demonstration

#include<stdio.h>
int main(int argc, char *argv[]){
   int i;
   for (i = 0; i < argc; i++) {
      printf("Arg %d: %s</p><p>", i, argv[i]);
   }
   return 1;
}

Output

Arg 0: G:\CP\CP programs\main with arguments.exe
Explanation:
The program that prints all the arguments passed to your program, including the program name itself.

The above is the detailed content of In C language, is it possible to pass parameters in main() function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete