1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How scanf identify our inputs?

Discussion in 'C' started by sebasr, Feb 28, 2007.

  1. sebasr

    sebasr New Member

    Joined:
    Feb 28, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I came a question in a C exam. The Q is,

    How does the scanf identify the 255 entered from terminal
    a) 0xff
    b)0xff,0x0,0x0
    c)or like hex for 2 then5 then5

    These are the options given.
    Anyone knows some explanation pls share

    Seban.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    It depends on what you are storing the value 255. If its 8 bit (1 byte) it will be a option if its more bytes then b can also be a probable but I would still go with a.
     
  3. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    If you press the keys, '2', '5', and '5' on the keyboard, scanf will deal with them according to your format specifier(s). Using that, it will store them (or fail), as a binary value, in the specified variables of the specified type. Your question, as phrased, is almost meaningless. All values are binary, in the computer. There is no hex or decimal or anything else. Those are representations for YOUR brain. The binary may be interpreted in differing ways, e.g., float, integer, gating pattern or mask. That's why we have typing (explicit or implicit).

    Incidentally, virtually no keyboard in common use would produce the binary value, 2, for key '2', or 5 for key '5'. There are layers of interpretation in play.
     
    Last edited: Mar 1, 2007

Share This Page