Question Details

No question body available.

Tags

c integer-overflow

Answers (5)

May 25, 2026 Score: 2 Rep: 1,557 Quality: Low Completeness: 60%

Confidence is high that the data types in the original code were too small for the data, and an overflow ensued which resulted in faulty results. The sum is 6,000,000,000, but the highest unsigned value that can be held in a 32-bit integer is 4,294,967,295. Using the long long data type enables usage of 64-bit integers which in turn enables/permits much higher values, and this is likely the most straightforward solution for you.

Please note that the format specifier for printf() is also modified to accommodate the new data type. Usage of stdint.h is recommended if your compiler has it. This allows for more precision in specifying data types. Ie: uint64_t for an unsigned 64-bit integer.

In answer to another query, the problem will persist if you access the array via pointer -- but not if you use a 64-bit integer pointer. For this issue, it's all about the data type and having the right type in all the relevant places.

This code has been successfully tested in Visual Studio 2026 by me just now.

#include 

int main() { / All relevant data types now long long (64-bit). The best optimization? / long long arr[] = {2000000000, 2000000000, 2000000000}; long long n = 3; long long sum = 0;

for(int i = 0; i < n; i++) { sum += arr[i]; }

/ Note that we've updated the format specifier to printf() -- specifically "long long decimal" to mirror our types */ printf("Average = %lld\n", sum / n); return 0; }
May 25, 2026 Score: 2 Rep: 21 Quality: Low Completeness: 40%

Yes — this is caused by integer overflow.

But a normal int on most systems is a 32-bit signed integer: Range: -2147483648 to 2147483647

So 6000000000 exceeds the maximum value and overflows, producing an incorrect negative number.

Use a larger integer type like long long.

May 25, 2026 Score: 1 Rep: 313,729 Quality: Medium Completeness: 60%

You are right saying

I suspect it's integer overflow, but I'm not sure.

To be sure you can output the maximum value that can be stored in an object of the type int.

#include 
#include 

int main( void ) { printf( "INTMAX = %d\n", INTMAX ); printf( "Used value = %d\n", 2000000000 ); }

The program output might look like

INTMAX    = 2147483647
Used value = 2000000000

So you need to use an integer type that is able to store the sum of elements of the array.

You could for example select type long int. It indeed could store the sum if to use a compiler that defines the size of the type long int greater than the size of the type int. For example if to use gcc then you will have

#include 

int main( void ) { printf( "sizeof( int ) = %zu\n", sizeof( int ) ); printf( "sizeof( long int ) = %zu\n", sizeof( long int ) ); }

The program output might look like

sizeof( int )    = 4
sizeof( long int ) = 8

However not all compilers define the type long int such a way. If to use for example the MS VS C compiler then the output of the above program will be

sizeof( int )    = 4
sizeof( long int ) = 4

So it will be more safer to use type long long int to store the sum of elements of the array.

Another remark relative to your code. Using the magic number 3 as

int n = 3;

makes your program less flexible and error prone. it would be much better to write

const sizet N = sizeof( arr ) / sizeof( arr );

Taking into account all remarks the program can look the following way

#include 

int main( void ) { int arr[] = { 2000000000, 2000000000, 2000000000 }; const size_t N = sizeof( arr ) / sizeof( arr );

long long int sum = 0;

for ( size_t i = 0; i < N; i++ ) { sum += arr[i]; }

printf( "Average = %lld\n", sum / N ); }

The program output is

Average = 2000000000

Pay attention to that the function main has no return statement. If it is absent then when the control achieves the closing brace } then by default the function returns 0.

May 25, 2026 Score: 1 Rep: 72,786 Quality: Low Completeness: 50%

You should keep the averate and the remainder and never calculate the sum

int main(void)
{
    int arr[] = {2000000000, 2000000000, 2000000000};
    int n = 3;
    int avg = 0;
    int rem = 0;
    int i = 0;

for (i = 0; i < n; i++) { avg += arr[i] / n; rem += arr[i] % n;

avg += rem / n; rem %= n; }

printf("Average = %d\n", avg);

return 0; }

Is this definitely caused by integer overflow?

Yes - and it is Undefined Behaviour

How can I fix the code to correctly calculate the average of large numbers (without using double)?

Answer above

Would the same problem occur when using a pointer to traverse the array?

Of course yes - it does not mater how you access the array

May 25, 2026 Score: 1 Rep: 2,281 Quality: Low Completeness: 50%

If the number of elements is known in advance, then the average can be represented by the following formula:

(ndiv1 + rem1 + ndiv2 + rem2 + ..)/n = (div1 + div2 + ..) + (rem1 + rem2 + ..)/n

Let me suggest an option with a minimum number of operations:

int main() {
    int arr[] = {2147483647, 2147483647, 2147483647};
    int n = sizeof arr / sizeof *arr;
    int div = 0, rem = 0;

for (int i = 0; i < n; i++) { div += arr[i] / n; rem += arr[i] % n; }

printf("Average = %d\n", div + rem / n); }