It is showing the numeric value and not the character because that is what you had done. The %d
take a value and print as a decimal number. Use %c
is having the same value printed as a character. The printf()
is a form of presentation, you say how you want the values to be presented. You have to choose the appropriate format for your need.
C is a weakly typed language, so you can access a value however you want.
"\0"
is a string, '\0'
is a character. You must compare a character with another character, you cannot compare with string which is actually a string ending with a null, i.e., \0
, therefore "\0"
are actually two characters is the \0
which is inside the quotation marks plus one other \0
which is the terminator of string, which actually doesn’t even make sense to have, since the first terminator already closes the string, even so amos are placed there, for the sake of coherence.
What this condition is doing is just looking for the character terminator to know that the string ended. Contrary to what you can imagine string no 50 characters, it has so many characters until you find the terminator. It could end before 50, or later, which will take up an unreserved memory space and probably cause problems. Understand that if you do not want to burst the reserved space, your string may have a maximum of 49 valid characters, since the latter will be reserved for the terminator.
#include<stdio.h>
#include<string.h>
int main() {
char str[50];
printf(" We will count the number of letters\n");
printf("-------------------------------------\n");
printf("Tell me the word: \n");
scanf("%s", str);
int i;
for (i= 0; str[i] != '\0'; i++) printf("The letter %d is %c\n", i, str[i]);
printf("|The number of words is: %d\n", i);
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.