Skip to content

Instantly share code, notes, and snippets.

@lbwanghr
Last active April 5, 2024 01:40
Show Gist options
  • Save lbwanghr/4c1fa8930d08f067f38dde475483ba5d to your computer and use it in GitHub Desktop.
Save lbwanghr/4c1fa8930d08f067f38dde475483ba5d to your computer and use it in GitHub Desktop.
We often encounter a situation where there are several parameters in main function, so how to use the executable file with parameter(s)?
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(int argc, char* argv[]){
if(argc!=3){
printf("Bad argument!\n");
return -1;
}
double a = atof(argv[1]);
double n = atof(argv[2]);
double result = pow(a,n);
printf("result = %.2f\n",result);
return 0;
}
**************************************
when I want ot use the .exe file
win+R -> cmd ->
C:\Users\Administor>cd c:\
c:\>Mypower.exe 2.2 3
result = 10.65
That's a not bad message.
@lbwanghr
Copy link
Author

How do we use the return value of main function?

int ret=system("c:\\Mypower.exe 2.2 3");

So when any problems occur, we could supervise the status with variable ret.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment