Printing a character in place of a number

Asked

Viewed 962 times

3

#include<stdio.h>
#include<string.h>

int main()
{
    char str[50];
    int i, l = 0;

    printf(" We will count the number of letters\n");
    printf("-------------------------------------\n");

    printf("Tell me the word: \n");
    scanf("%s", str);

    for( i= 0; str[i] != '\0'; i++){
        l++;
        printf("The letter %d is %d\n", i, str[i]); /* não entendi por que é
        mostrado o valor de cada letra mas não a letra em si */
    }
    printf("|The number of words is: %d\n", l);
    return 0;

}

inserir a descrição da imagem aqui

This is an activity that I found on the internet where I have to find the size of a string without using the library function. What I don’t understand is as a comment in the code. I would also like to know why when I put " 0" instead of ' 0' the program does not work: it enters an infinite loop.

3 answers

4

A string is nothing more than a string, in C, it means an array of 'chars' basically and in the end is added the character ' 0'. Note that it scans the input and puts it in an array of 50-position chars, where the first will be filled with the words written + 0 to indicate the end of the string and the rest of the positions are filled with junk values, this is why it has the ' 0'.

Suppose the input is "Test"

str[0] = ’T'; str[1] = 'and'; str[2] = ’s'; str[3] = ’t'; str[4] = 'and'; str[5] = ' 0'; str[6] = waste; . . . str[49] = waste;

The difference between using double quotes and single quotes. Single quotes means it is a char while double quotes which is a string. A char has a numeric value associated with a letter, this is in the ASCII table, if you use %d it will print the numeric value, the correct is to exchange it for %c or %s.

printf("The letter %d is %c\n", i, str[i]);

4

Why inside the printf(), your second %d is wrong. %d is only for printing numbers.

%c is for printing characters; and

%s is for printing string(strings).

Then the correct would be: (I did not compile)

printf("The letter %d is %c\n", i, str[i]); 

You were printing the relative ASCII table of those characters.

3


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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.