Question in reading and printing names with char

Asked

Viewed 48 times

-1

Eai guys. I’m trying to create a code that reads, stores and prints names up to 5
letters(Characters)
. I’m sure there must be a more agile way than the one I made, just below:
OBS: I want to be able to read and print names with ATE 5 LETTERS. I know there are more ageist ways with Stings but I want to learn like this.

    #include<stdio.h>
    #include<stdlib.h>
    main (){
        char nome[4];
        printf("Qual o seu nome?\n");  
        scanf("%c%c%c%c ", &nome[0],&nome[1],&nome[2],&nome[3],&nome[4]); 
        printf("O seu nome é:%c%c%c%c%c",nome[0],nome[1],nome[2],nome[3],nome[4]); 
    }
  • Satisfactory answer, thank you

2 answers

2


This is the usual:

    #include<stdio.h>
    int main (void)
    {
        char nome[6];
        printf("Qual o seu nome? ");  
        int res = scanf("%5s", nome );
        printf( "[scanf() retornou %d]\t", res);
        if ( res == 1 ) printf("O seu nome: \"%s\"\nprimeira letra: '%c'\n",nome, nome[0]); 
        printf("\n"); 
        return 0;
    }

This is called string in C, and is a vector with a zero at the end --- null terminated, in the literature. scanf() read these things with the specifier %s %5s limits the size in 5 letters. Since you have such a zero at the end you must declare the name with 6 or it will not fit. The letters are accessed by the index. See in the example.

Always test the return of scanf(), which can be -1, 0, 1.. up to the total of specifiers you used. Ex: scanf(%5s,%d,%d,%f" ... ) can read up to 4 fields (in this case separated by commas) and then scanf() can return -1 in case of error, or 0, 1, 2, 3 or 4 as you were able to read this number of fields.

I suggest not jumping off the line after the prompt when reading something: in general people feel more comfortable reading the question and typing the answer in the same line...

Example output:

so>  gcc -o tst -Wall x078b.c
so>  ./tst
Qual o seu nome? abcde
[scanf() retornou 1]    O seu nome: "abcde"
primeira letra: 'a'

so>  ./tst
Qual o seu nome? abcdef
[scanf() retornou 1]    O seu nome: "abcde"
primeira letra: 'a'

so>  ./tst
Qual o seu nome? ^Z
[scanf() retornou -1]
so>   

-1

Good evening, friend. As you say there are better ways to do this reading, but as you opted for a specific one I will help.

    #include <stdio.h>
    #include <stdlib.h>

    void main(){
    char nome[6];
    printf("Qual o seu nome?\n");  
    scanf("%c%c%c%c%c", &nome[0],&nome[1],&nome[2],&nome[3],&nome[4]); 
    printf("O seu nome e: %c%c%c%c%c\n",nome[0],nome[1],nome[2],nome[3],nome[4]); 

} 

Explanation:

When declaring the char arrangement you should have put 'name[6]' instead of 'name[4]'. It should be done so, because when declaring a char arrangement it should be taken into account that the last position will be reserved to the character' n'.

Example:

'name[4]' would be stated as follows: ’D', 'a', 'v', 'i'

'name[6]' would be stated as follows: ’D', 'a', 'v', 'i', ’d', ' n'

Note that in the second example you have the 5 spaces for the letters and a space for the ' n' (important character in a char arrangement). The first example the letter 'i' basically "invades" the place of ' n', this is not something positive.

There have also been some other syntax problems in the code.

It’s them:

Line 3: a return type or a void should be declared for the main function

Line 6: a space was placed after declaring '%c'. Correct mode: "%c%c%c%c".

Line 6: only four '%c' was placed and there should be five.

  • 1

    I think you confused the \0, null character, with the \n, character that breaks the line. And one detail is that if you read all vector positions using the %c, then you should put the \0 by hand (vetor[6] = '\0';).

  • Yes, well noted. I got confused. Thanks for reporting

Browser other questions tagged

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