Home > other >  How do I get my integer arithmetic into a long long?
How do I get my integer arithmetic into a long long?

Time:10-14

As part of exercise 2-3 in Ritchie and Kernighan's C programming language, I've written a program that converts hexadecimal inputs into decimal outputs. I want it to be able to handle larger numbers, but it seems to be doing integer arithmetic somewhere. When you enter something like "DECAFCAB" it spits out a large negative int. I figured out that I need to add the "LL" suffix to my literals, which I did, but it's still not working. Any help please? Sorry if this is a dumb question or a typo, but I've been at it for an hour and can't figure it out. :(

#include <stdio.h>
#define MAX_LINE 1000 

void getline(char s[])
{
    int i;
    char c;

    for(i = 0; i < MAX_LINE-1 && (c=getchar()) != EOF && c != '\n';   i)
        s[i] = c;
    s[i] = '\0';
    printf("\n%s", s);
}

long long htoi(char s[]) // convert the hex string to dec
{
    long long n = 0;
    int i = 0;

    if(s[i] == '0') // eat optional leading Ox or OX
          i;

    if(s[i] == 'x' || s[i] == 'X')
           i;

    while(s[i] != '\0')
    {
        if((s[i] >= '0' && s[i] <= '9'))
            n = 16LL * n   (s[i] - '0'); // here is the arithmetic in question
        else if(s[i] >= 'A' && s[i]<= 'F')
            n = 16LL * n   (s[i] - 'A'   10LL);
        else if(s[i] >= 'a' && s[i] <= 'f')
            n = 16LL * n   (s[i] - 'a'   10LL);
        else {
                printf("\nError: Encountered a non-hexadecimal format: the '%c' character was unexpected.", s[i]);
                printf("\nHexadecimal numbers can begin with an optional 0x or 0X only, and contain 0-9, A-F, and a-f.\n\n");
                return -1;
             }
          i;

    }
    return n;
}

main()
{
    char input[MAX_LINE];
    long long hex_output;

    while(1){
        getline(input);
        hex_output = htoi(input);
        if(hex_output >= 0)
        printf("\nThe value of the hexadecimal %s is %d in decimal.\n\n", input, hex_output);
    }
}

CodePudding user response:

You told printf to expect an int when you made the placeholder %d. To make it expect (and therefore read the entirety of a) long long, modify it to %lld.

The reason it looks like a plain int is that with varargs functions like printf, it doesn't know what the argument sizes are, and the format string is the only way to figure it out. When you say to expect plain int, it reads sizeof(int) bytes from the argument, not sizeof(long long) bytes (it's not necessarily byte-oriented, but that's how much data is read), and (on a little endian system with 4 byte int and 8 byte long long) you see (roughly) the result of the argument with the top 4 bytes masked off.

CodePudding user response:

The problem you are experiencing comes from treating a (conventionally) "unsigned" hexadecimal integer value as "signed". Resorting using to a larger built-in data type will get you past the problem with going from 31 to 32 bits, but this masks the actual problem. (If you extend to 64 bits, you will encounter the same problem and be back asking, "why doesn't this work.")

Better is to write code that doesn't require ever wider registers. There will always be a maximum width, but the answer to this OP is to use an "unsigned long".

#include <stdio.h>

unsigned long htoi( char s[] ) { // convert the hex string to dec
    unsigned long n = 0;
    int i = 0;

    if(s[i] == '0') // eat optional leading Ox or OX
          i;

    if(s[i] == 'x' || s[i] == 'X')
          i;

    for( ; s[i]; i   ) {
        unsigned int dVal = 0; // don't copy/paste complex statements. 
        if((s[i] >= '0' && s[i] <= '9'))
            dVal = s[i] - '0'; // simple

        else if(s[i] >= 'A' && s[i]<= 'F')
            dVal = s[i] - 'A'   10; // simple

        else if(s[i] >= 'a' && s[i] <= 'f')
            dVal = s[i] - 'a'   10; // simple

        else {
            // less verbose
            printf("\nError: '%c' unexpected.", s[i] );
            return 0; // NB: Notice change!!
        }
        n = (16 * n)   dVal; // simple...
    }

    return n;
}

int main() {
    // simplified, stripping out user input.
    char *hexStr = "0xDECAFCAB";
    unsigned long hex_output = htoi( hexStr );

    // Notice the format specifier to print an ordinary (unsigned) long
    printf( "\nThe value of the hexadecimal %s is %u in decimal.\n\n", hexStr, hex_output );

    return 0;
}
The value of the hexadecimal 0xDECAFCAB is 3737844907 in decimal.

When K&R wrote the original book, there was no such thing as "long long", but there was "unsigned long".

  •  Tags:  
  • c
  • Related