Thread: Hex scanf input and printf output.

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    6

    Hex scanf input and printf output.

    I am trying to write a program using unsigned char, that prompts the user to enter a two-digit hex value, then displays the value. Can anyone point me in the right direction? Thanks This is what I have so far.swapnibbles.c Also.
    Code:
    #include <stdio.h>
    
    
    unsigned char swapnibbles(unsigned char data)
    {
        if
            ((data < 0x00) || (data > 0xFF) )
                printf ("\nInvalid Entry\n");
    }
    
    
    unsigned char data (void)
    {
        unsigned char  data;
        scanf ("%0x", &data);
        printf ("0x", data);
    
    
        return data;
    }
    
    
    char main (void)
    
    
    {
        data ();
    
    
        return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Line 15 should read ... scanf("%hhx", &data);
    Line 16 should read ... printf("%x", data);

    Also your test in lines 6 through 8 does nothing since it's impossible for an unsigned char value to hold numbers less than 0 or greater than 255.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    Thanks Tater, got it! What does the %hhx specifically do? I couldn't find that in my book?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by et3ruiz View Post
    Thanks Tater, got it! What does the %hhx specifically do? I couldn't find that in my book?
    Ok, something to learn real quick... Programming textbooks are not complete references to the C language or it's libraries. They are enough to get you started and always the assumption is that you will continue learning after.

    Your compiler should have documentation for it's keywords, libraries, toolchain, etc. If you don't have it ... get it.
    If your compiler doesn't have it... get one that does.

    The first thing you will discover is that your textbooks gave you about 5% of the full disclosure...

    At least half of a programmer's work is "looking stuff up" which is exactly what you need to do with things like the format strings in printf() and scanf()...
    Even if you *think* you know, you should still look it up to be certain (which is exactly what I did when answering your question).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-03-2011, 03:00 AM
  2. printf and scanf
    By silentintek in forum C Programming
    Replies: 1
    Last Post: 10-27-2008, 09:32 PM
  3. Weird read input or bad printf output?
    By ChaoticMachiner in forum C Programming
    Replies: 3
    Last Post: 05-04-2008, 03:40 PM
  4. Weird output of scanf and printf.
    By omnificient in forum C Programming
    Replies: 2
    Last Post: 12-05-2007, 01:28 PM
  5. basic input and output with printf and fgets
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 08-01-2002, 11:02 PM

Tags for this Thread