C doesn't define sizes exactly. C types such as int and long are defined as being "at least 16 bits'' and "at least 32 bits'' respectively.
If we want to define variables which have a specific size independent of platform, then we can use types such as int32_t, int64_t defined in sys/types.h header file.
So we would assume that the following code should work:
#include <stdio.h>
#include <sys/types.h>
int64_t num;
num = 0xabcdef1234567890;
printf("num = %llx\n", num);
Intended O/P: abcdef1234567890
BUT on some platform/compiler combination the output can come out to be 34567890.Such as on IBM/AIX, compiler will give a warning
1506-207 (W) Integer constant <value> out of range.
Whereas GCC/LINUX will print the intended output.
The reason is that the integer literal 0xabcdef1234567890 wasn't explicitly specified to be of type int64_t.
A solution to this is to specify literal as 0xabcdef1234567890LL but a better way of doing this is to use macros in stdint.h (included in inttypes.h)
#include <stdio.h>INT64_C will itself append "LL" or appropriate size specifier to literal depending on platform.
#include <inttypes.h>
int64_t num;
num = INT64_C(0xabcdef1234567890);
printf("num = %" PRIx64 "\n", num);
1 comment:
Hello sir,
Thank u for the information.Actually I want crc function which takes integers and give crc in intger or hexadecimal. i am not getting how to do that. Actually there are functions which takes the arguments as unsigned char.How to convert the integers to unsigned char? also I've a function of crc generation which calculates for integer only. but it does not givethe value for more than 2 digits.How to convert the same for 3 and 4 digits.Should we use some bytes conversion? Please give me some info about this fast. I'm doing a project in vc++ ie conversion of protocols.
Kavitha
Post a Comment