Return the typed name

Asked

Viewed 59 times

2

The program only returns the first letter of the typed name. I think it has something to do with the "%c", but I still don’t understand how this formatting works or maybe the data types in C.

#include <stdio.h>

int main() {
    char nome;
    printf("Digite seu nome:\n");
    scanf("%c", &nome);
    printf("Seu nome é %c", nome);
}
  • %c is letter, %s is text

2 answers

3


Next, you have to transform the variable 'name' into an array or a string. In this case, this array will be a character array. (By the way, char rightly means character, so it returns only the first character of the variable 'name').

Then swap %c for %s for strings and const*char (characters in sequence [arrays]) and swap char nome; for char nome[20];. In doing this you limit to printing a 20 character array in the 'name variable'.

Your code should look like this:

#include <stdio.h>

int main() {
    char nome[20];
    printf("Digite seu nome:\n");
    scanf("%s",nome);
    printf("Seu nome é %s", nome);

  return 0;
}

ps: It is always important to put (Return 0;) in 'int main' functions, for the program to understand that it ended there and return an internal value to function, this value n is printed. And I removed the "&", because if I am not mistaken it is not necessary for arrays (char [x]), if any error occurs put it back where it was!

  • Ah, yes. I get it. You can show how it would be with string?

  • @Lucas: in C a string is an array of characters plus the terminator character ' 0'.

  • not just what the above remark said. A string in the "C" programming must be called by the #include<string> and then contains its own terms and a very unique way of messing with it. I suggest you google some programming sites, I’m not the most qualified to help with this part.

  • @Cesar Czermak: in C the operations on strings are defined in <string. h>. In C++, which is not what the questioner specifies, since C is different from C++, is what is used #include <string> and that we use for the definition of strings and their operations..

  • truth... I forgot to put the ". h" I apologize for that! I gave a crazy, it was malz... And look I don’t even know C++ yet hahaha

0

Instead of creating the "name" variable as done, do it as follows:

chat name[X];

X is a living number to choose (it can also be a variable set before)

You should also exchange the scanf(...) for this:

fgets(name);

Finally, instead of the printf, use this:

fputs(name);

Therefore, I believe that the following code should work:

int main(){ char nome[50]; printf("Enter your name: "); fflush(stdin); fgets(name); printf("His name is: "); fputs(name); }

*I tried to include the library, but the text got weird, anyway, it just uses stdio. h, and maybe stdlib. h (but I believe the latter not).

Explaining your problem: when you declare a char as "char c;", you are reserving ONLY the memory space of a character, when you declare as shown earlier, you declare "X" memory spaces for characters, so we call strings. Working with strings is often different from working with normal characters, so it is recommended to use other functions such as fputs and fgets. One more detail, logo ates scanf(...) or fgets(...), it is good to use a "fflush (stdin);", that in Windows, for Linux, the command is different.

Browser other questions tagged

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