How can I correctly handle special Portuguese characters like: ç, é, è and so on... using string in C?
I found how to do it with the printf but scanf, fgets and so on... I do not now how to properly storege on a string this kind of characters...
#include <locale.h>
int main (void){
setlocale(LC_ALL,"Portuguese");
printf("This is a example! Portuguese caracters ç é");
}
EDIT:
Tried this code as suggested bellow:
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <wchar.h>
int main() {
int a = 0;
setlocale(LC_ALL,"Portuguese");
wprintf(L"Exemplo de ç\n");
return 0;
}
Compiling it from the console manually : gcc -o main.exe main.c works. But using devc++ gives me a error:
[Error] converting to execution character set: Invalid argument
If I put only: wprintf(L"Exemplo de\\n"); (without the ç) devc++ now compiles well.
So once comliling it manually works I guess is something related with devc++ compile option... anyone know something about it?
EDIT2:
My main goal with all of this is to ask the user a input. read that input to save to a file. Everytime the program starts I will readu the file to restore the values saved on file.
But in Portuguese the user can type things such as ç, é, è ...
You can use locale.h like other people do. But if you want to save some storage or make your code run faster you can use the ASCII meaning of the character. An example:
#include <stdio.h>
int main()
{
printf("Oo! Special character: %c",141);
return 0;
}
Use wide characters strings for local characters. The following works for me:
#include <wchar.h>
#include <locale.h>
int main (void)
{
setlocale(LC_ALL,"pl_PL.UTF-8");
wprintf(L"This is a example! Polish characters ąśćłźó\n");
}
Attention: setlocale(LC_ALL,"Portuguese"); works for terminal output (eg printf)
If you are interested in read files from disk, using special characters (eg portuguese), and specially if you are using WINDOWS and DOS files (text files in windows/DOS), you should write the files in a ANSI encoding...
How to save text files in ANSI enconding? Use "Notepad++" ( https://notepad-plus-plus.org/ ) that has a menu option "enconding" allowing to select and save files in ANSI encoding. EASY! Problem solved :^)
Have Fun dear fellows from Brazil and other countries that use "bizarre" characters.
If you work on linux or OS/X, just using UTF-8 encoding in both the source file and the terminal will work seamlessly.
With this simple the source code:
#include <stdio.h>
int main() {
printf("Olá! vocês todos moram na França?\n");
return 0;
}
I get this output: Olá! vocês todos moram na França?
Olá! vocês todos moram na França?
In hex and octal:
0000 4F 6C C3 A1 21 20 76 6F O l \303 \241 ! v o 0008 63 C3 AA 73 20 74 6F 64 c \303 \252 s t o d 0020 6F 73 20 6D 6F 72 61 6D o s m o r a m 0028 20 6E 61 20 46 72 61 6E n a F r a n 0040 C3 A7 61 3F 0A \303 \247 a ? \n
As you can see, á
is encoded as \\xC3\\xA1
, ê
as \\xC3\\xAA
and ç
as \\xC3\\xA7
.
How can I correctly handle special Portuguese characters like: ç, é, è and so on... using string in C?
Set locale to use utf8 and form the string literal with a u8
prefix.
#include <stdio.h>
#include <locale.h>
int main (void) {
if (setlocale(LC_ALL, "en_US.utf8")) {
puts("Unable to set locale");
} else {
puts(u8"This is a example! Portuguese characters ç é");
}
}
Output
This is a example! Portuguese characters ç é
A trouble with using wprintf()
is that all output should be w....()
. Once code uses printf()
or wprintf()
, the stream now has an orientation .
Byte input/output functions shall not be applied to a wide-oriented stream and wide character input/output functions shall not be applied to a byte-oriented stream. C17dr § 7.21.2 4
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.