I think I'm having an integer overflow issue and I'm not sure how to fix it. I just started C coming from Python and JS and this is all new to me.
I put a really simple example below of what is happening. I'm passing an argument from the main function to a different function to multiply by 3, but when it's passed, the number overflows. The math works in the main function.
#include <stdio.h>
long long calc(number) {
return number * 3;
}
int main(void)
{
long long digits = 1111111111111;
long long result = calc(digits);
printf("calc result: %lld\n", result);
long long mainTimes3 = digits * 3;
printf("main result: %lld\n", mainTimes3);
return 0;
}
I'm getting the error message
main.c:3:11: warning: type of ‘number’ defaults to ‘int’ [-Wimplicit-int]`
The printf is showing
calc result: 438711637
main result: 3333333333333
The compiler warning hints at the problem - since you didn't explicitly define the type of the number parameter, the compiler assumes it's an int. Therefore number * 3 is also an int, and an integer overflow occurs.
To solve the issue, explicitly define it as the type you intended, long long:
long long calc(long long number){
/* Here ---^ */
return number * 3;
}
I think I'm having an integer overflow issue and I'm not sure how to fix it. I just started C coming from Python and JS and this is all new to me.
Then one of the things you need to accustom yourself to is that in C, you must explicitly specify the data type of every variable, every function parameter, and every structure or union member, and the return type of every function. And note well that array types comprise the data type of the array elements, and pointer types comprise the data type of the object or function to which the pointer points. Every expression in C99 and later has a data type that can be traced back, possibly in light of type conversions, to one or more explicit type declarations and / or to constants and literals whose data types are conveyed by their form.
Primordial C had provisions for default typing (to type int), and for backwards compatibility, this was carried forward into the first version of the C standard. Some compilers still support that for (still) backwards compatibility, but since C99, standard C does not provide for default typing. Compilers that support it in (otherwise) C99 or later mode are thereby implementing an extension.
Thus the problem with this function:
long long calc(number){ return number*3; }
is that the data type of parameter number has not been declared. That is what the warning is telling you.
A secondary problem is that that makes the function definition a K&R-style one, which has been disfavored for years and is removed in C23. Although you could fix the typing issue without converting from K&R style, it would be both easier and better to convert to ANSI style by putting the desired data type in the function parameter list, like so:
long long calc(long long number) {
return number * 3;
}
Final note: at your level of C experience, you should not consider any warning emitted by your compiler to be ignorable. Make sure you understand and fix each one, even though the compiler may produce an executable when you haven't.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With