#include <stdio.h>
int main(int argc, char* argv[]) //☆☆☆
{
printf("argc is %d\n", argc);
if(argc >= 2) // if nothing is passed then the value of argc becomes 1.
{
printf("The arguments supplied are : ");
for(int i=0; i<argc; i++)
{
printf("%s\t", argv[]);
}
}
return 0;
}
when prog.exe file will be created then we pass the arguments like
prog.exe harry code in teminal
Now the value in argc is 3, how? → we have wrote 3 words 'filename.exe'(with index 0) ,
'harry'(with index 1) and 'code'(with index 2) that's why the value in argc becomes 3.
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("The value of argc is %d\n",argc);
return 0;
}
inside terminal I wrote
.\code.exe hi hellow how do you do
output was → The value of argc is 7
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("The value of argc is %d\n",argc);
for (int i = 0; i < argc; i++)
{
printf("This argument at index number %d has value of %s \n", i,
argv[i]);
}
return 0;
}
Terminal command → .\code.exe hi hellow how do you do
output:
This argument at index number 0 has value of C:\Users\A--e3\Main\BCA\C_programming\l68\program\code.exe
This argument at index number 1 has value of hi
This argument at index number 2 has value of hellow
This argument at index number 3 has value of how
This argument at index number 4 has value of do
This argument at index number 5 has value of you
This argument at index number 6 has value of do
Usage → this could be used with File I/O functions and we can take and store value of arguments that are given in terminal and store that inside a file.