c - Incorrect variable value incrementing -
i have following code:
#include <stdio.h> #include <ctype.h> int main(int argc, char **argv) { int ch, lower, upper = 0; printf("enter line of text: \n"); while ((ch = getchar()) != eof) { if (islower(ch)) { ch = toupper(ch); ++upper; } else if (isupper(ch)) { ch = tolower(ch); printf("looking @ lower: %d\n", lower); ++lower; printf("looking @ lower: %d\n", lower); } putchar(ch); } printf("hello\n"); printf("\nread %d characters in total. %d converted upper-case, %d lower-case.", upper+lower, upper, lower); }
for reason upper variable being set correctly, can't work out why lower giving erroneous value. e.g. if type in 'football' says 4195825 converted lower-case, actual output should 1.
i can't see i'm going wrong here.
you haven't initialized lower
. it's value indeterminate.
c11: 6.7.9 initialization (p10):
if object has automatic storage duration not initialized explicitly, value indeterminate.
initialize 0
.
int ch, lower = 0, upper = 0;
Comments
Post a Comment