Remove unnecessary code. Neither the cast nor the multiplication by
sizeof(char) *
needed (it is always 1). It you want to show the scaling by the type usesizeof *result
instead.// char * result = (char *) malloc(sizeof(char) * (i + 1 + negative)); char * result = malloc(i + 1 + negative); // or char * result = malloc(sizeof *reuslt * (i + 1 + negative));
Little helper functions like
char toCharacter(int v)
should bestatic
as they are not meant to be called by outside functions.Agree with @JS1 that code should pass in the buffer. Also recommend passing in the buffer size. Return
NULL
when size is insufficient.char *convertBase(char *dest, size_t size, long long int value, int base);
MAX_LLI_REP 63
assumeslong long
is about 64 bits. C only requireslong long
to be at least 64 bits. Aside from the values being wrong for a 64-bitlong long
as pointed out by @JS1, the value should be based on the size oflong long
and not the assumption it is 64-bits.// #define MAX_LLI_REP 63 #define MAX_LLI_REP (sizeof(long long)*CHAR_BIT + 2)
Should you care about pre-C99 compatibility,
some_negative_int/base
andsome_negative_int%base
have implementation defined results that breaks this code. Converting all to positive values, except whenvalue
is 2's complementLLONG_MIN
, solves this.LLONG_MIN
needs special code - it just depends on what degree of portability you want.if (value < -LLONG_MAX) { // The details of this get into just how portable code needs to be. Special_TBD_Code(); } else { quotient = value >= 0 ? value: -value; ... }
A now rare concern is
-0
, possible whenlong long
is signed magnitude or 1's complement. I do not see a portable solution. The following offers some portability. See http://stackoverflow.com/q/19869976/2410359 https://stackoverflow.com/q/19869976/2410359#if LLONG_MIN == -LLONG_MAX if (value == 0) { long long pz = 0; if (memcmp(&value, &pz, sizeof value) == 0) strcpy(c,"0"); else strcpy(c,"-0"); } #endif
Remove unnecessary code. Neither the cast nor the multiplication by
sizeof(char) *
needed (it is always 1). It you want to show the scaling by the type usesizeof *result
instead.// char * result = (char *) malloc(sizeof(char) * (i + 1 + negative)); char * result = malloc(i + 1 + negative); // or char * result = malloc(sizeof *reuslt * (i + 1 + negative));
Little helper functions like
char toCharacter(int v)
should bestatic
as they are not meant to be called by outside functions.Agree with @JS1 that code should pass in the buffer. Also recommend passing in the buffer size. Return
NULL
when size is insufficient.char *convertBase(char *dest, size_t size, long long int value, int base);
MAX_LLI_REP 63
assumeslong long
is about 64 bits. C only requireslong long
to be at least 64 bits. Aside from the values being wrong for a 64-bitlong long
as pointed out by @JS1, the value should be based on the size oflong long
and not the assumption it is 64-bits.// #define MAX_LLI_REP 63 #define MAX_LLI_REP (sizeof(long long)*CHAR_BIT + 2)
Should you care about pre-C99 compatibility,
some_negative_int/base
andsome_negative_int%base
have implementation defined results that breaks this code. Converting all to positive values, except whenvalue
is 2's complementLLONG_MIN
, solves this.LLONG_MIN
needs special code - it just depends on what degree of portability you want.if (value < -LLONG_MAX) { // The details of this get into just how portable code needs to be. Special_TBD_Code(); } else { quotient = value >= 0 ? value: -value; ... }
A now rare concern is
-0
, possible whenlong long
is signed magnitude or 1's complement. I do not see a portable solution. The following offers some portability. See http://stackoverflow.com/q/19869976/2410359#if LLONG_MIN == -LLONG_MAX if (value == 0) { long long pz = 0; if (memcmp(&value, &pz, sizeof value) == 0) strcpy(c,"0"); else strcpy(c,"-0"); } #endif
Remove unnecessary code. Neither the cast nor the multiplication by
sizeof(char) *
needed (it is always 1). It you want to show the scaling by the type usesizeof *result
instead.// char * result = (char *) malloc(sizeof(char) * (i + 1 + negative)); char * result = malloc(i + 1 + negative); // or char * result = malloc(sizeof *reuslt * (i + 1 + negative));
Little helper functions like
char toCharacter(int v)
should bestatic
as they are not meant to be called by outside functions.Agree with @JS1 that code should pass in the buffer. Also recommend passing in the buffer size. Return
NULL
when size is insufficient.char *convertBase(char *dest, size_t size, long long int value, int base);
MAX_LLI_REP 63
assumeslong long
is about 64 bits. C only requireslong long
to be at least 64 bits. Aside from the values being wrong for a 64-bitlong long
as pointed out by @JS1, the value should be based on the size oflong long
and not the assumption it is 64-bits.// #define MAX_LLI_REP 63 #define MAX_LLI_REP (sizeof(long long)*CHAR_BIT + 2)
Should you care about pre-C99 compatibility,
some_negative_int/base
andsome_negative_int%base
have implementation defined results that breaks this code. Converting all to positive values, except whenvalue
is 2's complementLLONG_MIN
, solves this.LLONG_MIN
needs special code - it just depends on what degree of portability you want.if (value < -LLONG_MAX) { // The details of this get into just how portable code needs to be. Special_TBD_Code(); } else { quotient = value >= 0 ? value: -value; ... }
A now rare concern is
-0
, possible whenlong long
is signed magnitude or 1's complement. I do not see a portable solution. The following offers some portability. See https://stackoverflow.com/q/19869976/2410359#if LLONG_MIN == -LLONG_MAX if (value == 0) { long long pz = 0; if (memcmp(&value, &pz, sizeof value) == 0) strcpy(c,"0"); else strcpy(c,"-0"); } #endif
Remove unnecessary code. Neither the cast nor the multiplication by
sizeof(char) *
needed (it is always 1). It you want to show the scaling by the type usesizeof *result
instead.// char * result = (char *) malloc(sizeof(char) * (i + 1 + negative)); char * result = malloc(i + 1 + negative); // or char * result = malloc(sizeof *reuslt * (i + 1 + negative));
Little helper functions like
char toCharacter(int v)
should bestatic
as they are not meant to be called by outside functions.Agree with @JS1 @JS1 that code should pass in the buffer. Also recommend passing in the buffer size. Return
NULL
when size is insufficient.char *convertBase(char *dest, size_t size, long long int value, int base);
MAX_LLI_REP 63
assumeslong long
is about 64 bits. C only requireslong long
to be at least 64 bits. Aside from the values being wrong for a 64-bitlong long
as pointed out by @JS1, the value should be based on the size oflong long
and not the assumption it is 64-bits.// #define MAX_LLI_REP 63 #define MAX_LLI_REP (sizeof(long long)*CHAR_BIT + 2)
Should you care about pre-C99 compatibility,
some_negative_int/base
andsome_negative_int%base
have implementation defined results that breaks this code. Converting all to positive values, except whenvalue
is 2's complementLLONG_MIN
, solves this.LLONG_MIN
needs special code - it just depends on what degree of portability you want.if (value < -LLONG_MAX) { // The details of this get into just how portable code needs to be. Special_TBD_Code(); } else { quotient = value >= 0 ? value: -value; ... }
A now rare concern is
-0
, possible whenlong long
is signed magnitude or 1's complement. I do not see a portable solution. The following offers some portability. See http://stackoverflow.com/q/19869976/2410359#if LLONG_MIN == -LLONG_MAX if (value == 0) { long long pz = 0; if (memcmp(&value, &pz, sizeof value) == 0) strcpy(c,"0"); else strcpy(c,"-0"); } #endif
Remove unnecessary code. Neither the cast nor the multiplication by
sizeof(char) *
needed (it is always 1). It you want to show the scaling by the type usesizeof *result
instead.// char * result = (char *) malloc(sizeof(char) * (i + 1 + negative)); char * result = malloc(i + 1 + negative); // or char * result = malloc(sizeof *reuslt * (i + 1 + negative));
Little helper functions like
char toCharacter(int v)
should bestatic
as they are not meant to be called by outside functions.Agree with @JS1 that code should pass in the buffer. Also recommend passing in the buffer size. Return
NULL
when size is insufficient.char *convertBase(char *dest, size_t size, long long int value, int base);
MAX_LLI_REP 63
assumeslong long
is about 64 bits. C only requireslong long
to be at least 64 bits. Aside from the values being wrong for a 64-bitlong long
as pointed out by @JS1, the value should be based on the size oflong long
and not the assumption it is 64-bits.// #define MAX_LLI_REP 63 #define MAX_LLI_REP (sizeof(long long)*CHAR_BIT + 2)
Should you care about pre-C99 compatibility,
some_negative_int/base
andsome_negative_int%base
have implementation defined results that breaks this code. Converting all to positive values, except whenvalue
is 2's complementLLONG_MIN
, solves this.LLONG_MIN
needs special code - it just depends on what degree of portability you want.if (value < -LLONG_MAX) { // The details of this get into just how portable code needs to be. Special_TBD_Code(); } else { quotient = value >= 0 ? value: -value; ... }
A now rare concern is
-0
, possible whenlong long
is signed magnitude or 1's complement. I do not see a portable solution. The following offers some portability. See http://stackoverflow.com/q/19869976/2410359#if LLONG_MIN == -LLONG_MAX if (value == 0) { long long pz = 0; if (memcmp(&value, &pz, sizeof value) == 0) strcpy(c,"0"); else strcpy(c,"-0"); } #endif
Remove unnecessary code. Neither the cast nor the multiplication by
sizeof(char) *
needed (it is always 1). It you want to show the scaling by the type usesizeof *result
instead.// char * result = (char *) malloc(sizeof(char) * (i + 1 + negative)); char * result = malloc(i + 1 + negative); // or char * result = malloc(sizeof *reuslt * (i + 1 + negative));
Little helper functions like
char toCharacter(int v)
should bestatic
as they are not meant to be called by outside functions.Agree with @JS1 that code should pass in the buffer. Also recommend passing in the buffer size. Return
NULL
when size is insufficient.char *convertBase(char *dest, size_t size, long long int value, int base);
MAX_LLI_REP 63
assumeslong long
is about 64 bits. C only requireslong long
to be at least 64 bits. Aside from the values being wrong for a 64-bitlong long
as pointed out by @JS1, the value should be based on the size oflong long
and not the assumption it is 64-bits.// #define MAX_LLI_REP 63 #define MAX_LLI_REP (sizeof(long long)*CHAR_BIT + 2)
Should you care about pre-C99 compatibility,
some_negative_int/base
andsome_negative_int%base
have implementation defined results that breaks this code. Converting all to positive values, except whenvalue
is 2's complementLLONG_MIN
, solves this.LLONG_MIN
needs special code - it just depends on what degree of portability you want.if (value < -LLONG_MAX) { // The details of this get into just how portable code needs to be. Special_TBD_Code(); } else { quotient = value >= 0 ? value: -value; ... }
A now rare concern is
-0
, possible whenlong long
is signed magnitude or 1's complement. I do not see a portable solution. The following offers some portability. See http://stackoverflow.com/q/19869976/2410359#if LLONG_MIN == -LLONG_MAX if (value == 0) { long long pz = 0; if (memcmp(&value, &pz, sizeof value) == 0) strcpy(c,"0"); else strcpy(c,"-0"); } #endif
Remove unnecessary code. Neither the cast nor the multiplication by
sizeof(char) *
needed (it is always 1). It you want to show the scaling by the type usesizeof *result
instead.// char * result = (char *) malloc(sizeof(char) * (i + 1 + negative)); char * result = malloc(i + 1 + negative); // or char * result = malloc(sizeof *reuslt * (i + 1 + negative));
Little helper functions like
char toCharacter(int v)
should bestatic
as they are not meant to be called by outside functions.Agree with @JS1 that code should pass in the buffer. Also recommend passing in the buffer size. Return
NULL
when size is insufficient.char *convertBase(char *dest, size_t size, long long int value, int base);
MAX_LLI_REP 63
assumeslong long
is about 64 bits. C only requireslong long
to be at least 64 bits. Aside from the values being wrong for a 64-bitlong long
as pointed out by @JS1, the value should be based on the size oflong long
and not the assumption it is 64-bits.// #define MAX_LLI_REP 63 #define MAX_LLI_REP (sizeof(long long)*CHAR_BIT + 2)
Should you care about pre-C99 compatibility,
some_negative_int/base
andsome_negative_int%base
have implementation defined results that breaks this code. Converting all to positive values, except whenvalue
is 2's complementLLONG_MIN
, solves this.LLONG_MIN
needs special code - it just depends on what degree of portability you want.if (value < -LLONG_MAX) { // The details of this get into just how portable code needs to be. Special_TBD_Code(); } else { quotient = value >= 0 ? value: -value; ... }
A now rare concern is
-0
, possible whenlong long
is signed magnitude or 1's complement. I do not see a portable solution. The following offers some portability. See http://stackoverflow.com/q/19869976/2410359#if LLONG_MIN == -LLONG_MAX if (value == 0) { long long pz = 0; if (memcmp(&value, &pz, sizeof value) == 0) strcpy(c,"0"); else strcpy(c,"-0"); } #endif