guiromero
guiromero

Reputation: 77

Having issues with printing an array in c

I'm having issues on printing an array, that's what I'm doing:

#include <stdio.h>

#define DIM 5

int main () {

    double data[DIM] = {128.5, 131.4, 133.2, 127.1, 130.9};

    printf("%lf", data[DIM]);

    return 0;
}

The answer is always 0.000000.

I've also tried to put the values separately, like this:

#include <stdio.h>

#define DIM 5

int main () {

    double data[DIM];
    data[0]=128.5;
    data[1]=131.4;
    data[2]=133.2;
    data[3]=127.1;
    data[4]=130.9;

    printf("%lf", data[DIM]);

    return 0;
}

And still the answer is always 0.000000.

Could someone please help. Thank you in advance!

Upvotes: 0

Views: 1029

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 311018

In this call

printf("%lf", data[DIM]);

you are trying to output a non-existent element of the array with the index DIM while the valid range of indices for the array is [0, DIM). This record means that 0 is included in the range as a valid index and DIM is excluded from the range.

As a result the call of printf invokes undefined behavior.

Also you should use the conversion specifier f instead of lf. The length modifier l does not have an effect used with the conversion specifier f. So it is just redundant.

You can not output a whole array except character arrays (by means of the conversion specifier s).

To output the whole array you need to use a loop as for example

for ( size_t i = 0; i < DIM; i++ )
{
    printf( "%.1f ", data[i] );
}
putchar( '\n' );

Here is a demonstrative program.

#include <stdio.h>

#define DIM 5

int main(void) 
{
    double data[DIM] = { 128.5, 131.4, 133.2, 127.1, 130.9 };
    
    for ( size_t i = 0; i < DIM; i++ )
    {
        printf( "%.1f ", data[i] );
    }
    putchar( '\n' );
    
    return 0;
}

The program output is

128.5 131.4 133.2 127.1 130.9 

Upvotes: 1

MusicMan
MusicMan

Reputation: 134

As 4386427 and 500 - Internal Server Error pointed out, there are two issues at work here.

  1. You are trying to print an out-of-bounds index. When you make an array of length 5, indexes go from 0 to 4.
  2. More importantly, there is no specific "print array" function that I am aware of. Your best bet is to create a loop that prints each element of the array.
void printDoubleArray(double arr[], int length)
{
    printf("[");
    if (length > 0)
    {
        printf("%f", arr[0]);
    }
    for (int i = 1; i < length; i++)
    {
        printf(", %f", arr[i]);
    }
    printf("]\n");
}

Upvotes: 3

Rob Latham
Rob Latham

Reputation: 5223

C, the language itself, does not have array bounds checking. You want the element one past the end? sure thing. You want the element a million past the end? sure you can ask for that too (who knows what will happen but you can ask for it)

If your compiler supports it, you can get more robust error reporting. Not C standard but helpful nonetheless.

GCC: Compile with -fsanitize=address and at run-time the sanitizer will catch this overrun:

=======================
==220715==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffed8db4988 at pc 0x559ba54babcc bp 0x7ffed8db4920 sp 0x7ffed8db4910
READ of size 8 at 0x7ffed8db4988 thread T0
    #0 0x559ba54babcb in main /tmp/overrun.c:9

clang:

In addition to the run-time sanitizer (-fsanitize=address), Clang can also point out your problem at compile time:

    printf("%lf", data[DIM]);
                  ^    ~~~
overrun.c:7:5: note: array 'data' declared here
    double data[DIM] = {128.5, 131.4, 133.2, 127.1, 130.9};
    ^
1 warning generated.

Upvotes: 0

Related Questions