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:
- Initialize
counter
- Replace
counter=counter++
with counter++
- 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);
}