-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);
}
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?
– Lucas
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. Usefgets()
. Ex:fgets(palavra, sizeof(palavra), stdin);
– Augusto Vasques
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 withgets
orfgets
although as already said, should not usegets
. A small video to exemplify– Isac
Why doesn’t it work in mine? I tried in dev c++ and visual Studio code
– Lucas
Show us what is returning, so we can guide you. Don’t worry if question is closed, comments remain open.
– Augusto Vasques
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.
– Lucas
Take a print because we have to see what’s going on.
– Augusto Vasques
first output: https://i.stack.Imgur.com/fX5FB.jpg , using UTF-8 : https://i.stack.Imgur.com/swsWx.jpg
– Lucas
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()
– Augusto Vasques