-1

I have the following code:

#include <stdio.h>

int main()
{
    int student = 1;
    float mark, total = 0;

    while (mark != -1.00) {
        printf("Enter marks for Student %d (or -1 to stop): ", student);
        scanf("%f", &mark);
        total += mark;
        student++;
    }

    return 0;
}

When I try to compile it with gcc -O -Wall main.c I get the warning:

main.c: In function ‘main’:
main.c:8:17: warning: ‘mark’ is used uninitialized [-Wuninitialized]
    8 |     while (mark != -1.00) {
      |            ~~~~~^~~~~~~~
main.c:6:11: note: ‘mark’ declared here
    6 |     float mark, total = 0;
      |
Jorengarenar
  • 2,705
  • 5
  • 23
  • 60
Alex
  • 9
  • 1
    just print the value of `mark` before the `while` loop begins, run the program multiple times then you'll see that it may contains arbitrary value - hence it is required to initialize it before using it. The warning is given because you are using `mark` as a condition and it has been left uninitialized. – Agrudge Amicus Sep 25 '21 at 10:58
  • 1
    Welcome to SO. What is our question? Do you think you get the message but initialize `mark`? Do you want to know how to initialize variables? Do you want to know why using uninitialized variables is bad? We don't know your level of knowledge and which part of the message puzzles you. – Gerhardh Sep 25 '21 at 12:51
  • `mark` is not initialized in the moment `while`'s test tries to fetch it. – alinsoar Sep 25 '21 at 12:51

2 Answers2

2

At the beginning variable mark has no initialized value, so its value is unknown.

In next line you are comparing it in while loop. But you haven't read the value of mark from user yet, so the value being compared is something unknown.

I suspect you wanted to use a do { ... } while loop:

do {
    printf("Enter marks for Student %d (or -1 to stop): ", student);
    scanf("%f", &mark);
    total += mark;
    student++;
} while (mark != -1.00);

You should also check if mark isn't -1.00 before adding to total, thus your loop should be:

while (1) {
    printf("Enter marks for Student %d (or -1 to stop): ", student);
    scanf("%f", &mark);
    if (mark != -1.00) {
        total += mark;
        ++student;
    } else {
        break;
    }
}

And while you are dealing with floats, you might want to keep in mind the basic problem of their limited precision

Jorengarenar
  • 2,705
  • 5
  • 23
  • 60
  • 1
    You should also test that mark is not -1 before adding to total and doing student++ – Ptit Xav Sep 25 '21 at 11:02
  • The value of `mark` is indeterminate (per C 2018 6.7.9 10), and using it does not qualify for the rule that would make the behavior not defined by the C standard (C 2018 6.3.2.1 2) because its address is taken. An object with indeterminate value may behave as if its value is different at each use; it is not properly said to hold “random garbage from memory.” – Eric Postpischil Sep 25 '21 at 11:33
  • @EricPostpischil Better? – Jorengarenar Sep 25 '21 at 11:50
  • regarding: `if (mark != -1.00) {` the literal `-1.00` is a `double` literal, What should be used is a `float` literal: `if (mark != -1.00f) {` Note the trailing `f` – user3629249 Sep 25 '21 at 19:01
  • Re “the comparison `mark != -1.00` isn't the best practice”: The comparison is fine here. Floating-point does not randomly inject errors; the arithmetic and the roundings are well specified (although unfortunately some programming languages work to undo that by injecting latitude into the computations, taking them away from the IEEE-754 specification). When a user enters `-1` or something equivalent, `scanf("%f", &mark);` will store −1 in `mark`. – Eric Postpischil Sep 26 '21 at 15:11
  • Re “Usually in such situation you would do `mark - (-1.00) < epsilon`, where `epsilon` is acceptable error”: [There is no general solution for comparing floating-point numbers that contain errors from previous calculations.](https://stackoverflow.com/a/21261885/298225) Not only is there no general value that should be used for `epsilon`, there is not even a general form for the comparison. In some applications, an absolute error is appropriate. In some, a relative error is appropriate. In some, neither is appropriate. So this should not be recommended as “usually in such situation.” – Eric Postpischil Sep 26 '21 at 15:13
  • @EricPostpischil And they tell me I'm over pedantic. Yes, it's fine here, I wrote it in last sentence of that point. OP is clearly a newbie and from that perspective I wrote the answer, but OK, it might introduce confusion later down the line, I'll change this point to only mention 0.30000000000000004 – Jorengarenar Sep 27 '21 at 01:01
0

while (mark != -1.00)

mark is used not initialized here thus the warning. It is undefined behaviour.

0___________
  • 60,014
  • 4
  • 34
  • 74