#include <stdio.h>
#include <string.h>
int myatoi(char *str) {
int i, res = 0;
for (i = 0; str[i] != '\0'; ++i) {
res = res * 10 + str[i] - '0';
return res;
}
}
int main(int argc, char *argv[]) {
int j;
char str[] = " i want to get argv[2] value here";
int val = myatoi(str);
printf("%d", val);
if (argc == 3) {
for (j = 0; j < val; j++) {
printf("Hello, %s \n", argv[1]);
}
} else
if (argc < 3) {
printf("Not Enough Arguments\n");
} else
if (argc > 3) {
printf("Too Many Arguments\n");
}
return 0;
}
How do I pass my value of argv[2] to char str[] so that it converts string to int and print the output.
argv[0] will be file name, argv[1] will be string, argv[2] should be int
example output: ./hello raju 2
Hello, raju!
Hello, raju!
as argv[2] is 2 it should print 2 times.
int val=myatoi(argv[2]);?argcvalue.0output from the lineprintf("%d",val);