Thread: Scanf an integer - C

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    70

    Scanf an integer - C

    Hey.

    Suppose I want to scanf an int - here it doesn't work if I just do:

    Code:
    int in;
    scanf("%i", &in);
    Why is it?
    What's the difference between that and:

    Code:
    float f;
    scanf("%f", &f);
    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    694
    I'm not sure what you're getting at. Both of those seems to "work".

    However, if you mean to only allow decimal input for the integer then it's better to use %d instead of %i since %i accepts inputs like 010 as octal (hence decimal 8, not 10), which is probably not what the user intended. Although %i is sometimes useful, especially for hex input in the for 0xab.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    70
    @John - we are now discussing this issue at the other thread.
    Thank you!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf and integer problems
    By jericjones45 in forum C Programming
    Replies: 5
    Last Post: 02-27-2010, 07:12 AM
  2. Replies: 5
    Last Post: 06-12-2007, 02:18 PM
  3. determining if an integer is a legal integer
    By LiquidBlaze33 in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2005, 07:06 PM
  4. Replies: 2
    Last Post: 02-20-2005, 01:48 PM
  5. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM

Tags for this Thread