FibonacciCoder
FibonacciCoder

Reputation: 121

Not getting any output

The content of my main.c file

#include <stdio.h>
#include "max_subarray_data.h"

int main()
{
    init_change();
    for(int i=0;i<n;i++)
    {
        printf("%d %d\n",i,change[i]);
    }
}

The content of max_subarray_data.h

extern int price[];
extern int change[];
extern int n;

void init_change();

The content of my max_subarray_data.c

int price[]={100,113,110,85,105,102,86,63,81,101,94,106,101,79,94,90,97};
int n=(int)(sizeof(price)/sizeof(int))-1;
int change[(int)(sizeof(price)/sizeof(int))-1];

void init_change()
{
    for(int i=0;i<n;i++)
    {
        change[i]=price[i+1]-price[i];
    }
}

Why am i getting no output ?

Upvotes: 1

Views: 98

Answers (1)

Ingo Leonhardt
Ingo Leonhardt

Reputation: 9884

If it's not the missing '\n' in printf() it may be because your array change is one element too small: as array size you use the same expression as for setting n, so n-1 is the largest possible index. But then you loop from 1 to n and assign something to change[n]. That invokes Undefined Behaviour, so anything might happen.

Upvotes: 3

Related Questions