Prasad:
Can anyone please tell me how to store a 13 digit number in C language
?
You haven't specified the radix of the number system. However, I think it's
safe to assume that you're human and that you've got 10 fingers, so we'll
go with that one for a minute.
I'm not sure if Standard C provides an unsigned integer type which must
have at least 44 value representation bits. (If it does, then it's probably
"int unsigned long long").
I can think of a simple method of achieving what you want (although
somewhat inefficient), which is the use of binary-coded decimal. The radix
must be below 257 for the following to work on any implementation:
(Unchecked code, sloppily written, probably won't even compile.)
#include <limits.h>
#define RADIX (10)
#define DIGIT_BIT (4) /* A better way than hardcoding 4? */
#define DIGIT_PER_BYTE (CHAR_BIT/DIGIT_BIT)
#define DIGIT_BIT_MASK (~(UINT_MAX << DIGIT_BIT))
typedef struct ThirteenDigitNum {
char unsigned digits[13/DIGIT_PER_BYTE + !!(13%DIGIT_PER_BYTE)];
} ThirteenDigitNum;
typdef struct DigitAccessInfo {
unsigned byte_index;
unsigned shift_by;
unsigned mask;
};
void SetDAI(DigitAccessInfo *const p,unsigned const index)
{
p->byte_index = index / DIGIT_PER_BYTE,
p->shift_by = CHAR_BIT * index % DIGIT_PER_BYTE;
p->mask = DIGIT_BIT_MASK << shift_by;
}
unsigned GetDigit(ThirteenDigitNum const *const p,unsigned const index)
{
DigitAccessInfo dai; SetDai(&dai,index);
return (*p->digits[dai.byte_index] & dai.mask) >> dai.shift_by;
}
void SetDigit(ThirteenDigitNum const *const p,unsigned const index,
unsigned const val)
{
DigitAccessInfo dai; SetDai(&dai,index);
*p->digits[dai.byte_index] &= ~dai.mask;
*p->digits[dai.byte_index] |= val << dai.shift_by;
}