How to print words with special characters in c language

Asked

Viewed 80 times

-1

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

int main ()
{
    char palavra[10];
    
    setlocale(LC_ALL,"Portuguese_Brazil");
    printf("açúcar\n");
    gets( palavra);
    printf("a palavra digitada foi: %s", palavra);
    
    return(0);
}

primeira saída com esse código saída utilizando setlocale(LC_ALL, "PORTUGUESE_BRAZIL.UTF-8");

  • in the first printf the word sugar is printed correctly on the screen, using the setlocale function, but if in gets I write "sugar", for example, error when printing on the screen in the second printf. When I test with common words it works, but if the user inserts a word with special error characters in the text. How to solve this?

  • 3

    Never use gets(), for no reason, even in exercises. gets() was discontinued in C99 and removed from the standard library completely in C2011 because it was a great security breach. Use fgets(). Ex: fgets(palavra, sizeof(palavra), stdin);

  • The code is good. I test here on my pc and it works all right. Both the printf with açúcar works well, just like what is read, whether it be with gets or fgets although as already said, should not use gets. A small video to exemplify

  • Why doesn’t it work in mine? I tried in dev c++ and visual Studio code

  • Show us what is returning, so we can guide you. Don’t worry if question is closed, comments remain open.

  • So the output that I have is just the first sugar printed correctly on the screen in the second of the error, this using the code exactly as it is posted here. Now if I put setlocale(LC_ALL, "Portuguese_brazil.UTF-8"); there reverses, the first sugar error and the second appears correctly.

  • Take a print because we have to see what’s going on.

  • first output: https://i.stack.Imgur.com/fX5FB.jpg , using UTF-8 : https://i.stack.Imgur.com/swsWx.jpg

  • You’re running on CMD, right? Try to run the code on powershell. This is a Java question, but see if you can help https://answall.com/questions/168103/howto present and accentuate%C3%A7%C3%A3o-correct-no-return-do-cmd see this also https://answall.com/questions/142618/windows-cmd-setar-string-com-acentua%C3%A7%C3%A3o and see specific windows function Setconsoleoutputcp()

Show 4 more comments

1 answer

0

Replace the function gets for fgets. The function gets() is unstable, can cause problems. I do not recommend using.

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

int main ()
{
    char palavra[10];
    
    setlocale(LC_ALL,"Portuguese_Brazil");
    printf("açúcar\n");
    fgets(palavra,10,stdin);
    printf("a palavra digitada foi: %s", palavra);
    
    return(0);
}
  • thanks for the tip, did not know how to use this function "fgets"; but and to make the typed word comes out with punctuation? i searched a little and used setlocale(LC_ALL, "Portuguese_brazil.UTF-8"); it causes the word inserted in the variable "word" to come out correct if it is special character, but in the first printf("sugar"); of the error, I am trying to find a solution for the two to come out correct.

Browser other questions tagged

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