3

The compiler throws an 'unused variable error', when I try to declare any type of variable and assign it a value. Below I use 'float' as the variable type and try and assign it to 1.5.

#include <stdio.h>
#include <cs50.h>

int main(void)
{
    printf("How long is your shower?\n");
    int time = GetInt();

    float flow = 1.5;
}

The compiler throws this error:

~/workspace/pset1/ $ make water
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wshadow    water.c  -lcs50 -lm -o water
water.c:10:11: error: unused variable 'flow' [-Werror,-Wunused-variable]
    float flow = 1.5;
          ^
1 error generated.
make: *** [water] Error 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
David Hancock
  • 1,063
  • 4
  • 16
  • 28
  • It seems that this issue is already explained here : [enter link description here](http://stackoverflow.com/questions/19750690/wunused-variable-compiler-says-error) – P.Bra Sep 26 '16 at 07:01
  • `#include ` isn't part of C, is not shown, nor involved in your issue. You want to remove it from your example code. – alk Sep 26 '16 at 07:12

3 Answers3

6

flow isn't used by your program - it is not involved in any side effects, you just assign a value to it and discard it. Good compilers warn against such unused variables.

By using -Werror you turned the warning into an error.

Lundin
  • 195,001
  • 40
  • 254
  • 396
3

It's actually a warning and not an error but because of the -Werror flag you see it as an error.

Long story short, if you use the variable it won't return the error anymore.

#include <stdio.h>
#include <cs50.h>

int main(void)

{
    printf("How long is your shower?\n");
    int time = GetInt();

    float flow = 1.5;
    printf("Flow: %.2f, time: %d", flow, time);
}
Wolph
  • 78,177
  • 11
  • 137
  • 148
1

Seems legit, you are not using the variable anywhere. Try printing it out;

printf("%.2f", flow);
Laazo
  • 467
  • 8
  • 22