Print a character instead of a number

#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;

}

insert the description of the image here

This is an activity I found on the internet where I have to find the size of a string without using the library function. what I didn'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 doesn't work: it goes into an infinite loop.

Author: Maniero, 2016-12-21

3 answers

Is showing the numeric value and not the character because that's what you ordered to do. %d has a value taken and printed as a decimal number. If you use %c you are having the same value printed as a character. O printf() it is a form of presentation, you say how you want the values to be presented. Have to choose the suitable format for your need.

C is a weakly typed language, so you can access a value such that want.

"\0" is a string, '\0' it's a character. You must compare a character with another character, you can not compare with string which is actually a string terminated with a NULL, that is, \0, so "\0" are actually two characters is the \0 which is inside the quotes plus another \0 which is the Terminator of the string, which actually does not even make sense to have, since the first Terminator already[17]}string , yet masters are placed there, for the sake of coherence.

What this condition is doing is precisely looking for the character Terminator to know that the string is over. Contrary to what you can imagine the string does not have 50 characters, it has as many characters until it finds the Terminator. It may end up before 50, or later, which will take up an unreserved memory space and probably bring problems. Understand that if you do not want burst the placeholder, 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);
}

See working on ideone. E no repl.it. also I put on GitHub for future reference .

 3
Author: Maniero, 2020-11-18 15:15:30

A string is nothing more than a string, in C it means an array of 'chars' basically and at the end the character '\0'is added. Note that it scans the input and puts it in an array of chars of 50 positions, where the first ones 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 the reason to have the '\0'.

Suppose the input is "Test"

Str [0] = 'T'; str[1] = 'e'; str [2] = 's'; str[3] = 't'; str [4] = 'e'; str [5] = '\0'; str[6] = garbage; . . . str [49] = garbage;

A difference between using double quotes and single quotes. Single quotes means that it is a char while double quotes that is a string. A char has a numeric value associated with the letter, this is in the ASCII table, if you use %d it will print the numeric value, the correct is to change it to %C or %S.

printf("The letter %d is %c\n", i, str[i]);
 4
Author: Filipe Santiago, 2016-12-21 23:28:55

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

%c is for printing characters; and

%s is for printing string.

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.

 4
Author: Amzero, 2016-12-21 23:59:59