Thread: Help with defining variables

  1. #1
    Registered User
    Join Date
    Feb 2019

    Help with defining variables

    Hello everyone, I need some help with working with hexadecimal variables in C. I don't know exactly what is going wrong, as the program runs without an error, but I need to input a 16 digit hexadecimal number into the program, and then separate the digits 2 at a time. The problem is that it successfully separates the 8 digits to the right, but then for whatever reason, the left 8 digits aren't even defined int the variable. Here is my code for reference:

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    int main(void) {
        int i, j;
        int m = 0;
        unsigned long int hexedit;
        unsigned long int hex = 0x10387cfe38383800;
        printf("%lx\n", hex);
        for (i = 1; i <= 8; i++) {
            hexedit = ((hex >> m) &0xff);   // shifts digits and                                                                             separates 2 digits
            printf("%lx ", hexedit);
            m += 8;
        return EXIT_SUCCESS;
    My output is below as well:

    0 38 38 38 0 38 38 38

    The last 4 outputs should be the last 8 digits of the input from right to left, but instead, it's just repeating the correct 4 outputs. The outputted unedited value shows it just stops at first 8 digits.

    I'm not sure if it's the format specifiers (admittedly, idk what the correct specifier would be), but this is something I haven't really seen before. Any help would be appreciated, thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Each hex digit stores up to 4 bits of information; 16 hex digits therefore store up to 64 digits of information, so if you should use unsigned long long.

    ... or you could just use a string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Defining the variables
    By Satya in forum C Programming
    Replies: 4
    Last Post: 11-16-2018, 07:27 AM
  2. Defining variables in C++ as late as possible?
    By Absurd in forum C++ Programming
    Replies: 11
    Last Post: 09-18-2014, 09:34 AM
  3. defining macros or static variables
    By l2u in forum C++ Programming
    Replies: 15
    Last Post: 08-05-2008, 06:28 AM
  4. Defining Variables
    By Beaner in forum C++ Programming
    Replies: 3
    Last Post: 03-10-2005, 05:50 PM
  5. Globaly defining variables
    By PaloDeQueso in forum C Programming
    Replies: 5
    Last Post: 03-20-2002, 09:38 PM

Tags for this Thread