Can scanf read from a stored variable?

This is a discussion on Can scanf read from a stored variable? within the C Programming forums, part of the General Programming Boards category; I'm working on a problem that requires the addition of two large integers. It does a looping thing until it ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    19

    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. #2
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    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.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    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.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Quote Originally Posted by laserlight View Post
    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. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Quote Originally Posted by Nightowl View Post
    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...
    Last edited by azrael; 03-19-2009 at 11:50 PM.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by Nightowl View Post
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf to read int but read in char?
    By maybabier in forum C Programming
    Replies: 3
    Last Post: 04-11-2009, 12:58 AM
  2. bytes lost with partial read in UDP
    By mynickmynick in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-31-2009, 02:06 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. retrieve double variable in scanf
    By cfriend in forum C Programming
    Replies: 1
    Last Post: 11-25-2004, 02:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21