1
#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.

5
  • Lookup how to parse Strings as numbers. Commented Feb 4, 2017 at 20:47
  • 4
    what's wrong with int val=myatoi(argv[2]); ? Commented Feb 4, 2017 at 20:49
  • ... and after you check argc value. Commented Feb 4, 2017 at 20:52
  • You overlooked the 0 output from the line printf("%d",val); Commented Feb 4, 2017 at 20:54
  • just do int val = atoi(argv[2]); Commented Feb 4, 2017 at 20:54

2 Answers 2

2

You can directly pass argv[2] to myatoi() or use a variable:

 char *str = argv[2];
 int val=myatoi(str);

You are also better off adding an input check before using argv like:

if (argc != 3) {
   printf("Expected 2 args\n");
   exit(1);
} 

Your myatoi() suffers the similar problems like the atoi() standard function -- lacks the ability to detect and report bad inputs. Consider what happens if argv[2] is "xyz123" for example.

Sign up to request clarification or add additional context in comments.

1 Comment

what should i do for check for bad input such as xyz123 @usr
1

Your function myatoi is broken: you should move the return statement out of the for loop.

Here is a corrected version:

int myatoi(const char *str) {
    int i, res = 0;
    for (i = 0; str[i] != '\0'; ++i) {
        res = res * 10 + str[i] - '0';
    }
    return res;
}

Be aware that your function only handles strings composed exclusively on digits. No sign, no spaces no other characters are allowed. You could use more flexible standard functions such as atoi or strtol declared in <stdlib.h>

Here is a simplified version of your code:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    if (argc == 3) {
        int val = atoi(argv[2]);
        for (int j = 0; j < val; j++) {
            printf("Hello, %s\n", argv[1]);
        }
        return 0;
    } else {
        printf("error: 2 arguments expected\n");
        return 1;
    }
}

2 Comments

thanks but i should not use built in functions for this program.is any other way of doing it,instead of my code @chqrlie
@raju: If you cannot use the library function atoi(), use the modified myatoi() I posted above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.