1

I have started relearning C (self study), and ran into a problem with an if statement not always executing inside of a loop. What's odd is that on it's own, the function executes correctly, but unless I add a printf() statement referencing the variable in the loop then it won't attempt to test the if statement.

Specifically, the line printf("%d ", aCounter); in the while loop in the main(void) function is what's required for it to attempt test the if statement. I had a similar problem using a for loop (as you can see it's commented out). I am confused as to why it doesn't test the if statement without the printf() mentioned. And yet it does successfully find: 1, 6, 24, 28, and 496 with the printf() statement. It only finds "1" without it.

#include <stdio.h>

int isPerfect(unsigned int n);

int main(void) {
    /* for (unsigned int i = 1; i <= 1000; i++) {
        if (isPerfect(i) == 1) {
            printf("%d is perfect\n", i);
        }
    }
    */
   int aCounter = 0;
   while (aCounter < 1000) {
        aCounter++;
        if (isPerfect(aCounter) == 1 ) {
            printf("%d is perfect\n", aCounter);
            for (int aTemp = 1; aTemp < aCounter; aTemp++) {
                if (aCounter % aTemp ==0) {
                    printf("%d is a factor of %d\n", aTemp, aCounter);
                }
            }
        }
        printf("%d ", aCounter);
        //aCounter++;
   }
   
   int a = 6;
   if (isPerfect(a) == 1 ) {
       printf("%d is perfect\n", a);
   }

    printf("%d is perfect: %d\n", 6, isPerfect(6));
    printf("%d is NOT perfect: %d\n", 8, isPerfect(8));
    printf("%d is perfect: %d\n", 6, isPerfect(6));

}

int isPerfect(unsigned int n) {
    int aNumber;
    for (int i =1; i <= n; i++) {
        if (n % i == 0) {
            aNumber += i;
            if (aNumber == n)
            {
                return 1;
            }
        }
    }
    return 0;
}
Drew
  • 13
  • 2
  • 6
    `aNumber` is uninitialized in the `isPerfect` function. This is undefined behavior because using the `+=` operator requires that this variable has a deterministic initial value. – paddy Aug 17 '21 at 03:21
  • 2
    In other words, do `int aNumber = 0;` – enzo Aug 17 '21 at 03:26
  • The posted code does not cleanly compile!. When compiling, always enable the warnings (the posted code outputs a lot of warnings). For `gcc`, at a minimum use: `-Wall -Wextra -Wconversion -pedantic -std=-gnu11` Note: other compilers use different options to produce the same thing – user3629249 Aug 17 '21 at 21:10

1 Answers1

4

I'm not sure about the expected output of your program, but I can see that inside the isPerfect(unsigned int n) function, we have a statement aNumber += i; which basically does is aNumber = aNumber + 1;, i.e, update the value of aNumber by 1. But as we can see, in the very first statement inside this function where aNumber is defined, it isn't initialized, so updating its value to 1 seems pointless as it's previous value is unknown. Initializing its value , let's say int aNumber = 0; will do the job.

But uninitialized variable in C need not always contain a garbage value.

automatic (local) variables are not guaranteed to be zero, can contain garbage but global variables and static variables are guaranteed to be zero

please refer to this post StackOverflow post for more details.

.

Siddhant
  • 626
  • 1
  • 7
  • 20