0

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 ç, é, è ...

  • As you say `printf etc. work. They use what is called a "string" in C, so it's not clear what your problem is. Read [ask] and follow the advice. – too honest for this site Apr 09 '18 at 10:59
  • You use *wide* input and output, and wide character strings. And rather than force some locale, you tell the C library to use the user's current one. See for example the example program I wrote for [this answer](https://stackoverflow.com/a/45534069/1475978). – Nominal Animal Apr 09 '18 at 11:08
  • You can use wide characters, but it's easier to use ordinary characters and UTF-8, at least for Portuguese. – Arndt Jonasson Apr 09 '18 at 11:29

6 Answers6

1

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;
}
0

Attention:

setlocale(LC_ALL,"Portuguese");

works for terminal output (e.g. printf)

If you are interested in read files from disk, using special characters (e.g. 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.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • This is wrong. ANSI code pages are virtually never the correct answer. UTF-8 everywhere is, but Window’s support for it is shaky at best. – Demi Sep 13 '20 at 03:08
  • You can say that using UTF-8 is preferably than ANSI (I agree)... but, my comments are directly related to Windows/DOS users and the problems they have with special characters, printing and reading text from files. It is not wrong, it works with GCC (MinGW and CodeBloks 17, and a lot of people use it!). If you have a problem, just solve it. – F.Osorio Sep 14 '20 at 04:32
0

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?

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.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

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

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0
#include <iostream>
#include <locale>
using std::string;
using std::cout;
int main()
{
    setlocale(LC_ALL, "pt_BR.UTF-8");
    cout << "Agora pode usar : á é í ó ú à è ì ò ù ã õ â ê î ô û ç";
    cout << "Agora pode usar : Á É Í Ó Ú À È Ì Ò Ù Ã Õ Â Ê Î Ô Û Ç";
}

screenshot

cigien
  • 57,834
  • 11
  • 73
  • 112
  • 1
    Please add an explanation (in English) of how this answer solves the OP's problem. – cigien Sep 25 '21 at 02:25
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 25 '21 at 02:31
-1

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");
}
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • If I compile it manually it works... but compiling with devc++ gives me a error. Which IDE and compiler do you use? – Dimitri9494 Apr 09 '18 at 21:54
  • ArchLinux, IDE - vim , gcc 7.3.1 20180312 . I don't use devc++ as mu ide, so there's no much i can help you with that. Maybe use -std=c99 as suggested here: https://stackoverflow.com/questions/17584856/getting-a-dev-c-built-program-to-output-unicode-characters-to-the-windows-comm – KamilCuk Apr 11 '18 at 11:57
  • what do you use as ide to compile gcc? You jusr use vim as editor? – Dimitri9494 Apr 12 '18 at 07:57
  • I use linux. For small programs I edit in vim and run gcc the compiler from command line (no ide). For big projects I use eclipse (and/or qt-creator, if i need to create a gui) as IDE and cmake as makefiles generator and I setup batch compilation using cmake+gcc or clang, so i can use travis or gitlab-ci or docker-hub to automate tests and/or deployments. For windows devc++ is a good choice, I used CodeBlocks once, for big projects I would go probably with eclipse+mingw on windows, cause i am more used to eclipse, but that's just me and it's way harder to setup. – KamilCuk Apr 14 '18 at 14:54