c - Unexpected output after linking -


i have 2 c files:

main.c

#include <stdio.h>  int sum(int n);  double array[2] = { 0.001, 1.0001 };  int main() {     int val = sum(2);     printf("%d\n", val);     return 0; } 

sum.c

extern int array[2];  int sum(int n) {     int i, ret = 0;      (i = 0; < n; i++) {         ret += array[i];     }      return ret; } 

i compile , link files , run executable, getting unexpected output:

306318409 

why happening?

c standard section 6.2.7/2 says

all declarations refer same object or function shall have compatible type; otherwise, behavior undefined.

so have happened. that's when lie compiler.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -