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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-08-30 18:49:06
forward
1414 people have browsed it

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;
}
Copy after login

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;
}
Copy after login

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.
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template