# How would you do this conversion?

This is a discussion on How would you do this conversion? within the C Programming forums, part of the General Programming Boards category; Say you have the argument to main in the form of ./program 447 758. How would the separation of those ...

1. ## How would you do this conversion?

Say you have the argument to main in the form of ./program 447 758. How would the separation of those two numbers into integers be done for example to gain the result of.

Code:
```first_num[0] = 4;
first_num[1] = 4;
first _num[2] = 7;

second_num[0] = 7;
second_num[1] = 5;
second_num[2] = 8;```
Its driving me mad, i know its simple but can't seem to grasp it. Actual code would be extremely appreciated. Thanks

2. For starters try to come up with an algorithm for doing this task, coding it will become a lot simpler afterwards.

3. those argurment are string already

argv[1][0] will be 4 argv[1][2] will be 7 and so on

4. Originally Posted by creeping death

argv[1][0] will be 4 argv[1][2] will be 7 and so on
actually it will be '4', '7' etc

to convert these chars to int -
argv[1][0] - '0' should be used

5. Originally Posted by vart
actually it will be '4', '7' etc
que?
to convert these chars to int -
argv[1][0] - '0' should be used

6. The numeric value of each character will be the '4' and '7'. To make that into an integer, one needs to subtract the value of '0'.

--
Mats

7. well, thats what i said in my first post ...

8. Originally Posted by creeping death
well, thats what i said in my first post ...
not even close

9. just because i did not put ' ' does not mean that i imply that its already an integer, and you putting it does not imply it to be a character. i did mention that it is a "string"...which is an array of characters.

10. Solved, i stumbled upon this

C: How can I split a number into digits? - Yahoo! Answers

and then did this
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 5

int main (int argc, char *argv[])
{
int val;
int whole1; /*stores the whole number*/
int seperated_whole1[MAX];/*seperate digits of whole1*/
int i = MAX - 1;/*for use in the seperation loop*/

whole1 =  atoi(argv[1]);
/*
*This loop splits the whole1 integer into seperate digits for the array in preperation for
*individual manipulation
*/

while (whole1 > 0)
{
seperated_whole1[--i] = whole1 % 10;
whole1 /= 10;
}

printf("%d %d %d %d\n", seperated_whole1[0], seperated_whole1[1], seperated_whole1[2], seperated_whole1[3]);
return 0;
}```
I hope someone who has the same problem comes across this

11. Originally Posted by creeping death
just because i did not put ' ' does not mean that i imply that its already an integer, and you putting it does not imply it to be a character. i did mention that it is a "string"...which is an array of characters.

yes, yes... and that you come to C programming does not imply that you are talking about C language...

bla-bla-bla...

12. Originally Posted by vart
yes, yes... and that you come to C programming does not imply that you are talking about C language...

bla-bla-bla...

yeah , talking *about* C language NOT talking *in* C language.