0

I programmed a simple clock and found that while hours and seconds are okay, minutes are not if I declare the variables on a single line (minutes starts at 16 and not at 0 as expected).

The problem is solved if I declare the variables on separated lines. I'm still curious though, anybody knows why?

Here's the code:

#include <stdio.h>
#include <windows.h>
//h=hours, m=minutes, s=seconds.
int main(){
    int h, m, s = 0; //THIS IS THE LINE: WHY "m" STARTS AT 16 AND NOT 0?
    int delay = 1000;
    while(1){
        s++;
        if(s>59){
            m++;
            s=0;
        }
        if(m>59){
            h++;
            m=0;
        }
        if(h>12){
            h=1;
        }
        printf("\n %02d:%02d:%02d", h, m, s);
        Sleep(delay);
        system("cls");
    }
}
user438383
  • 5,716
  • 8
  • 28
  • 43
catiz
  • 9
  • 1
  • 5
    Only `s` is initialized. `h` and `m` are not, so `h++` and `m++` , `h>12`, `m>59` will give indeterminate values. – Eugene Sh. Sep 15 '21 at 15:34
  • A better example might be `int* h, m, s = 0;` All the members aren't initialized, just as they aren't all pointers. This is why many style guides say you should not declare multiple variables on one line. – Tim Randall Sep 15 '21 at 15:45
  • 1
    **Conclusion**: don't declare several variables on one line, it's hard to read andit's error prone. – Jabberwocky Sep 15 '21 at 15:46
  • 1
    `int h, m, s;` ... you can now use the "multi-assignment" (my wording, the Standard regards it as several chained simple assignments) `h = m = s = 0;` to set all 3 variables to `0`. – pmg Sep 15 '21 at 15:49
  • And I thought all the reasons for ***never*** putting more than one variable on a line were exhausted. You just found another one. – Andrew Henle Sep 15 '21 at 16:07

3 Answers3

6

Your variables are uninitialized - they can take any value, and using them before initialization is (usually) indeterminate behavior.

A line of code like:

int h, m, s = 0;

does not define each variable to be zero - only the third one. It is equivalent to:

int h;
int m;
int s = 0;

To fix, initialize all of your variables as zero:

int h = 0, m = 0, s = 0;
Daniel Kleinstein
  • 5,262
  • 1
  • 22
  • 39
4

You haven’t initialized h or m, so their initial values might whatever was in that memory already, or something else entirely. C does not automatically initialize variables to 0.

When you declare multiple variables on one line, the value only applies to the variable it comes after. So

int a, b, c = 5;

only sets c to 5, while

int a = 1, b = 2, c = 3;

initializes all of them. In your case, you just need to use

int h = 0, m = 0, s = 0;

You compiler likely has an option for warnings (-Wall in gcc), which should warn you about using an uninitialized variable.

tjcaul
  • 383
  • 1
  • 9
  • 1
    Re “their initial values are whatever was in that memory already”: Uninitialized automatic objects have *indeterminate values*, meaning whatever “value” they appear to have in any particular use does not necessarily come from the memory reserved for them. The C standard says it may be different (including a trap value) in each use of the object. So a compiler may use whatever happened to be in one register in one use, another register in another use, or memory in another use. – Eric Postpischil Sep 15 '21 at 15:54
  • I didn’t realize that. Thanks. I will update my (now redundant) answer. – tjcaul Sep 15 '21 at 15:56
2

int h, m, s = 0; // THIS IS THE LINE: WHY "m" STARTS AT 16 AND NOT 0?

Well, I guess, because C is not English. The "= 0" part applies just to that one variable name s, not to the whole line.

Although, you know, English doesn't necessarily apply modifiers to the whole line, either. If I say "There were three people standing on the streetcorner: A man, a woman, and a boy named Brad", you would not infer that the man and the woman were named Brad, too, would you?

[Footnote: Now I've got that old song "Me and you and a dog named Boo" stuck in my head. :-) ]

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • 1
    *Now I've got that old song "Me and you and a dog named Boo" stuck in my head.* LOL. A self-inflicted [ear worm](https://www.youtube.com/watch?v=dQw4w9WgXcQ) – Andrew Henle Sep 15 '21 at 16:09