1. ## Can scanf read from a stored variable?

I'm working on a problem that requires the addition of two large integers. It does a looping thing until it accomplishes its goal. However, once the integer's number of digits exceeds 10, we run into problems, the integer starts going haywire and making up values, and is no longer is accurate.

So, I was thinking about storing it as an array or something.

However, the user is only required to enter 1 integer. The second integer is the reverse of the user's input.

So can I use scanf or something like it to read from a variable that is already stored, so I can put it into an array using a for loop?

thanks!

2. Well, the maximum storage size for an integer is (2^31)-1, or half of 4294967296. Roughly 2 billion.

That would be your integer accuracy problem . . .

I'm afraid that I don't understand everything else that you're saying.

3. Right, right.

Well, I wanted to see if scanf, or another similar function, could read from a source other than the user's input.
Like, if the the variable were somehow on the buffer, you could use a for loop to get each digit into an array, using something like this:

Code:
```	printf("# digits: ");
scanf("%d", &num_digits);

printf("first number: ");

for(i=num_digits-1; i>= 0; i--)
scanf("%1d", &first_number[i]);```
but i want it to take that data from a variable that's already been stored. the problem is that I want to separate each digit, and put it into a separate array location...

4. Originally Posted by azrael
but i want it to take that data from a variable that's already been stored. the problem is that I want to separate each digit, and put it into a separate array location...
The point is that the input is too large to fit into the integer variable, so you should be reading it into an array to begin with.

5. Read it into a character array like this . . .

(note that some of this is invalid, but changed for easier reading)
Code:
```char number[1024];
scanf("%s", number);
/* and now you've got a nice, shiny array of characters that represent the number */
/* check that they're all numbers . . . */
int x;
int valid = 1;
if(number[0] == '-' || isdigit(number[0]) {
for(x = 1; x < strlen(number); x ++) {
if(!isdigit(number[x])) { valid = 0; break; }
}
}
else valid = 0;
/* and there you have it. */```
Note that you could use something more like the GMP if you really wanted to.

6. Originally Posted by laserlight
The point is that the input is too large to fit into the integer variable, so you should be reading it into an array to begin with.
Hm. Well, I know I can make that user input one go into an array...But the revered variable is a bit more tricky.

I think I would have to rework the whole algorithm I had worked out..

7. Originally Posted by Nightowl
Read it into a character array like this . . .

(note that some of this is invalid, but changed for easier reading)
Code:
```char number[1024];
scanf("%s", number);
/* and now you've got a nice, shiny array of characters that represent the number */
/* check that they're all numbers . . . */
int x;
int valid = 1;
if(number[0] == '-' || isdigit(number[0]) {
for(x = 1; x < strlen(number); x ++) {
if(!isdigit(number[x])) { valid = 0; break; }
}
}
else valid = 0;
/* and there you have it. */```
Note that you could use something more like the GMP if you really wanted to.
I believe I'm allowed to assume input is valid, although I usually do put some error checking in, mostly with command line arguments. Cool stuff, though. that isDigit function is kinda cool. Wish I had known about it before, hahaha.

I could use atoi on that, so I could still perform operations, right?

what's GMP?

EDIT: Clicked the link. looks a bit involved for one assigment...

8. Originally Posted by Nightowl
Read it into a character array like this . . .

(note that some of this is invalid, but changed for easier reading)
Code:
```char number[1024];
scanf("%1023s", number);
/* and now you've got a nice, shiny array of characters that represent the number */
/* check that they're all numbers . . . */
int x;
int valid = 1;
if(number[0] == '-' || isdigit(number[0]) {
for(x = 1; x < strlen(number); x ++) {
if(!isdigit(number[x])) { valid = 0; break; }
}
}
else valid = 0;
/* and there you have it. */```
Note that you could use something more like the GMP if you really wanted to.
At least use safe code.
Changed code to pass buffer size to scanf.