+ 1
Attempting to use macros to define unsigned long in C. Should I use typedef or const instead? Is this bad practice?
I am attempting to define long integers with 32 and 64 bits using macros. My preprocessor includes: #define FOUR_BYTES 4294967295 #define EIGHT_BYTES 18446744073709551613 When I compile the code, I receive an error for the EIGHT_BYTES variable stating that the int is so long that it is unsigned. When commented out and recompiled, the FOUR_BYTES variable is printed with half the digits and hyphens between the output. How could I better define the unsigned long and unsigned long long?
4 Respuestas
0
NULL if you are using number without decimals with #define preprocessor, default data type int is considered if you don't specify it..
that's why value for Eight_byte goes out of max value limit... to make it behave as unsigned long, use suffix UL as below and you will be done:
#define a 12345678987654UL
note UL at the end of value
+ 2
@Ketan Lalcheta
I found and fixed the issue. My compiler didn’t pick it up until I enabled warnings. I was trying to use scanf(“%d”, &d) to store ULL inputs, then using printf(“%llu”, d). It accepted a decimal as input and attempted to output the value as an unsigned long long.
Just goes to show that we should ask questions regarding what we’re trying to accomplish, rather than assuming the error lies in just a few lines of code ;)
Lesson learned: Always post the full code!
+ 1
Ketan Lalcheta
I attempted that a few times and the compiler still outputs my data incorrectly. I tried the suffixes, as well as using int64_t, uint64_t, and int_least64_t.
I tried printing the unsigned long variable by itself, and the large number outputs correctly, so the error must be occurring in another part of my program where I use the variable (my fault for assuming where the error was rather than posting the entire code. Whoops). Still, your answer is correct and the problem seems to be something else entirely on my end. I’ll go ahead and mark your answer as accepted! :)
+ 1
NULL hope you will be able to fix the issue.. if not , share your code to have a look... UL works on sololearn app as well... I have checked and verified this..