#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    8
    Rep Power
    0

    Thumbs down Scanf, wrong input


    Hi,
    I'm trying to make an user input with scanf, and an output with printf. However, it doesn't seem to work, no matter what format specifiers I use:

    Code:
    int main() {
        int a;
    
        scanf("%i", a);
        printf("%i", a);
        return 0;
    }
    For example, the input is 5, and the resulting output is 2147332096..

    How does that make any sense?
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    The problem is that scanf() has to be able to change the value passed to it, which means that you need to pass a pointer to the variable, not just the value of the variable. The C language does not have true pass-by-reference in the sense of a built-in facility; arguments are always passed by value. However, if the value happens to be a pointer, then you can treat it as if it were pass by reference. This is what scanf() requires. So, simply add an ampersand to the a argument and it should work:

    Code:
    int main() {
        int a;
    
        scanf("%d", &a);
        printf("%d", a);
        return 0;
    }
    (Note also that the correct type specifier in the format string for signed decimal integers is 'd', not 'i' - well, actually, both will work, but you normally only would use 'i' if you were expecting input in either hex or octal. to quote Wikipedia: "%i : Scan an integer as a signed number. Similar to %d, but interprets the number as hexadecimal when preceded by 0x and octal when preceded by 0.")

    Unfortunately, this is a tricky error to catch, though most modern compilers will give a warning about it, if you have warnings set to maximum (which is recommended). As for why the variable has those seemingly random values in it, well, it's because the value hasn't been set by scanf() or anything else, so it still has whatever garbage was in memory before the program began.
    Last edited by Schol-R-LEA; June 4th, 2013 at 07:59 PM.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    8
    Rep Power
    0
    A pointer, of course that makes sense. Thank you for your detailed answer! I'm used to C++ where you could simply do something like cin >> a;, so I hadn't thought about that.

IMN logo majestic logo threadwatch logo seochat tools logo