-4
#include <stdio.h>

int main() {
    int x;  int counter;
    while (scanf_s("%d", &x) != 0)
    {
        puts("Enter the a signed number : ");
        if (x % 2 == 0) { counter=counter++; }
    }
    printf(" %d pair numbers", counter);
}

I get

uninitialized local variable counter used.

The program is supposed to keep asking for numbers until it gets zero and tells us the amount of pairs given.

G.Khalaf
  • 13
  • 2
  • 8
  • What's unclear about the warning? Where do you get it? Just read the text and understand it! – too honest for this site Sep 11 '17 at 22:43
  • 1
    `int x, counter = 0;` – Elliott Frisch Sep 11 '17 at 22:43
  • 1
    The solution is very clearly to initialize the local variable (`counter`) before you start incrementing it in the loop. Read your code **and** the error message, which tells you *uninitialized local variable **counter** used*. – Ken White Sep 11 '17 at 22:43
  • It actually *is* easy here. You can choose between two variables that could be uninitialized. 50% chance... – tofro Sep 11 '17 at 22:44
  • 2
    You are trying to use `counter` to increment it. What do you think the value of `counter` is when the loop executes for the first time? – dtell Sep 11 '17 at 22:44
  • 1
    @MichaelGeary: No. The problem is `counter = counter++` (which is also wrong). – Ken White Sep 11 '17 at 22:45
  • 1
    `scanf_s("%d", &x) != 0` is "problematic". What if an error is encountered? Read the documentation of the functions you use! – too honest for this site Sep 11 '17 at 22:46
  • 1
    Ok I removed counter = counter++ and wrote counter++ and initialized it to 0 but the problem now is even when I Put 0 it keeps asking me for a number !! – G.Khalaf Sep 11 '17 at 22:51
  • 1
    scanf_s does not return the integer that you read in, read the documentation to see what it returns. To exit do something like `(while x != 0)` and call scanf from within the loop – Mitch Sep 11 '17 at 22:52
  • `scanf_s` is optional by the standard and does not add any safety to your code. Use the normal `scanf` function. – too honest for this site Sep 11 '17 at 22:57
  • @G.Khalaf I think that the problem is that due to this prompt "Enter the a signed number : " the compiler does not know whether to deal with a number of with the number.:) – Vlad from Moscow Sep 11 '17 at 22:59
  • Um..."how to solve uninitialized local variable used"? Well, *initialize* it! – AnT stands with Russia Sep 11 '17 at 23:04
  • @G.Khalaf: "problem now is even when I Put 0 it keeps asking me for a number". But what steps did you take to stop it from asking? What part of your code is supposed to terminate the cycle when you enter 0? – AnT stands with Russia Sep 11 '17 at 23:07

2 Answers2

0

You need to initialize counter to 0 before trying to increment it. Also counter = counter++; is redundant (and undefined!). Just use counter++

Mitch
  • 3,342
  • 2
  • 21
  • 31
  • 2
    The problem with `counter = counter++;` is not that it is redundant, but that it causes undefined behavior. – ad absurdum Sep 11 '17 at 22:54
  • @DavidBowling true, but if it did behave as OP expected it would still be redundant – Mitch Sep 11 '17 at 22:57
  • 2
    @Mitchel0022: And if C was Java, it was well defined. That is pointless. It **is** UB and done! – too honest for this site Sep 11 '17 at 22:58
  • @DavidBowling fair point – Mitch Sep 11 '17 at 23:02
  • @Mitchel0022-- as is often the case in such questions, there is more than one UB here; the UB to which I referred in my comment has to do with sequence points. [See this famous SO question](https://stackoverflow.com/questions/949433/why-are-these-constructs-using-undefined-behavior) – ad absurdum Sep 11 '17 at 23:02
-1

The problem you are facing is cause by your use of the variable counter without initializing it. This is exactly what the compiler is telling you.

When you try to execute counter=counter++; for the first time, counter has no definied value. You might think by int counter; it gets initialized with 0, but this is wrong in C.

The next problem is the line counter=counter++; itself. If you want to increment counter, just use counter++. Depending on the compile you are using, the use of counter=counter++ should give you at least a warning. On my machine using Apple LLVM version 8.1.0 (clang-802.0.42) I get

warning: multiple unsequenced modifications to 'counter' [-Wunsequenced]

Then you try to loop until you read 0. But scanf() (use this instead of the Microsoft specific scanf_s()) does not return what it has read from stdin but the number of input items assigned. It returns 0 in the event of a matching failure.

So here is what to do:

  1. Initialize counter
  2. Replace counter=counter++ with counter++
  3. Use another loop breaking condition than the return value of scanf()

One approach could be the following:

#include <stdio.h>

int main() {
    int x=0;
    int counter=0;

    do{
        printf("Enter the a signed number: ");
        scanf("%d", &x);
        if (!(x % 2)){
            counter++;
        }
    } while(x);

    printf(" %d pair numbers\n", counter);
}
dtell
  • 2,488
  • 1
  • 14
  • 29
  • Although you advised what `scanf` does not return, the answer fails to consider what it *does* return. Moreover it treats the end cue `0` as data before quitting. – Weather Vane Sep 11 '17 at 23:06
  • thanks a lot but what do I do to pause the screen after hitting 0 ? – G.Khalaf Sep 11 '17 at 23:07
  • @WeatherVane: citing the BSD Library Manual `scanf` "[..]return the number of input items assigned" as written in the answer. But you are right, no details are provided. – dtell Sep 11 '17 at 23:12
  • "Depending on the compile you are using…" - It is undefined behaviour, nothing else! The compiler is free to do anything it wants, including code to format your harddrive. No need for a diagnostic message, though. And while MS indeed have their own set of `_s` functions, `scanf_s` could as well be the standard function. For `%d` it just does not make any difference. – too honest for this site Sep 11 '17 at 23:14
  • @Olaf: I just mentioned that some compiler might give warnings, others might not. – dtell Sep 11 '17 at 23:19
  • @datell: You missed the point! The problem is not the warning! – too honest for this site Sep 11 '17 at 23:21
  • At least OP's code check the return value of the input with `while (scanf_s("%d", &x) != 0)` (which should have been `while (scanf_s("%d", &x) == 1)`. This answer does not check the return value of `scanf("%d", &x);` and then uses `x` in `if (!(x % 2)){` leading to a potential use of the variable `x` without a certain value. Best to check the return value of input functions. "Use another loop breaking condition than the return value of scanf()" is not good advice. OP should use the return value _and_ something like `x` compared to 0. – chux - Reinstate Monica Sep 12 '17 at 15:23