I am learning C language and I am facing a problem I don't seem to be able to resolve on my own. I have a simple loop where I add up ascii values of all characters in each word.
char char_array[100]; int lexicographical_values[20]; int i, j = 0; for( i = 0; i <strlen(char_array); i++) { if(char_array[i] == ' ') j++; lexicographical_values[j] += (int)char_array[i]; } Then, if I output the lexicographical_values array in a loop
printf("%d word is: %d \n", i, lexicographical_values[i]); I get correct figures for each word (ex: dd = 200 and so on)
But if I actually look at each element's value in the array, I get big numbers that are far from being correct.
The question is, how do I get correct values and how does printf obtain the correct values?
Thanks
chartointis unnecessary (just like you didn't use one when comparing to' ', which is anintconstant).(int)sincechartype is integral and there will be implicit conversion, but apart from using uninitialized memory, the biggest problem is your assumption that achar's value represents its ASCII value. This need not be true.