-1
    int sum1 = aboveMain(matrice);
    printf("%d nice\n", sum1);
    int sum2 = underMain(matrice);
    printf("%d nice\n", sum2);

For some reason this works perfectly fine, prints the intended values 11 and 19. Yet the moment i delete the printf line after the int sum1 declaration like so...

    int sum1 = aboveMain(matrice);
    int sum2 = underMain(matrice);
    printf("%d nice\n", sum2);

It returns 30, which means its been adding them both. Yet this seems to make 0 sense to me, can anybody please explain to me why this is happening? thanks

heres the entire code btw

#include <stdio.h>
#define ROWS 3
#define COLS 3


int aboveMain(int matrice[][COLS]);
int underMain(int matrice[][COLS]);
    
void main() {
    
    int matrice[3][3] = {{1,2,3},
                         {4,5,6},
                         {7,8,9}};

    int sum1 = aboveMain(matrice);
    printf("%d nice\n", sum1);
    int sum2 = underMain(matrice);
    printf("%d nice\n", sum2);
}


int aboveMain(int matrice[][COLS]) {
    int sum, i, j;
    for(i=0;i<3;i++) {
        for(j=0;j<3;j++) {
            if(i < j) 
                sum+= matrice[i][j];    
            
        }
    }
    return sum;
}



int underMain(int matrice[][COLS]) {
    int sum, i, j;
    for(i=0;i<3;i++) {
        for(j=0;j<3;j++) {
            if(i > j) 
                sum+= matrice[i][j];    
            
        }
    }
    return sum;
}
  • 1
    `sum` is not initialized to 0, therefore the behaviour of your code is undefined, resulting in undetermned values in your output. – Jabberwocky Apr 07 '22 at 10:26
  • thank you :) could you perhaps elaborate what causes this undefined behavior? is the sum from the second function going to the sum from the function previous to it? – userAsker9999 Apr 07 '22 at 10:28

1 Answers1

2

sum is not initialized to 0 in your functions,with the C language the initial value of local variables is undetermined. Therefore the bahaviour of your code is undefined.

int aboveMain(int matrice[][COLS]) {
  int sum = 0;             // you need to initialize sum to 0 here
  int i, j;   
  for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
      if (i < j)
        sum += matrice[i][j];

    }
  }
  return sum;
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • Ah. Ninja'ed. ;-) {tipping my hat} – DevSolar Apr 07 '22 at 10:28
  • thank you :) could you perhaps elaborate what causes this undefined behavior? is the sum from the second function going to the sum from the function previous to it? – userAsker9999 Apr 07 '22 at 10:31
  • @userAsker9999 the behaviour is **undefined**. If you don't initialize `sum`, it's initial value is basically whatever junk is in memory, and that junk depends on what has happened before the function was called. – Jabberwocky Apr 07 '22 at 10:33
  • my apologies i didnt know what undefined meant had to google it. – userAsker9999 Apr 07 '22 at 10:35
  • @userAsker9999 google _c undefined behavior_ – Jabberwocky Apr 07 '22 at 11:24
  • @userAsker9999: Or here on SO: [Undefined, unspecified, and implementation-defined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior) – DevSolar Apr 07 '22 at 12:23