i’m wondering how can I compare in C language, a number I put on argv[2] and a int number in my code:
EX: prog.exe file.txt 74
========================
int n;
scanf ("%d", &n);
if (n > argv[2])
{
[...]
}
How can I compare those different kind of data?
Any command line parameters passed to your app are stored in
argvas character pointers (aka “C strings”). You need to convert the string to an integer via any of the dozens of methods (simplest isatoi) before comparing.If you are writing serious production code, avoid using
atoias it is difficult to distinguish between failure and strings evaluating to the number0. You should instead usestrtolwith proper error checking.