C - when I use the site. h scanf does not work well

Asked

Viewed 582 times

0

I’m creating a database.

I used the locale.h and setlocale(LC_ALL, "Portuguese") for accents and cediles to appear on the windows command line.

Because of this when I make one scanf of an accentuated or ceded character and I do the respective printf it prints a nonexistent symbol in the alphabet and the corresponding value in decimal is a negative number.

If the special character is not entered by scanf (or fgets) and you already have the program 'prints it' in the code

I’ve done some research on the subject and found a lot of information about the various types of coding and Unicode but nothing about why this happened.

I leave my code:

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

int main() {

    setlocale(LC_ALL, "Portuguese") ;

    /* Para ver a tabela ASCII */

    int x ;

    for(x = 0; x <= 255; x++) {

        printf("[%d]: %c\n", x, x);
    }

    char ch ;

    printf("Introduzir caractere: ")
    scanf("%c", &ch) ;
    printf("Caractere introduzido: %c\n", ch);
    printf("Valor na tabela ASCII: %d\n", ch);
}

by entering, for example, a "ç" the character you print is " " and the decimal value is -121. The same applies to other accented characters.

Why is this happening?

  • Here without the setlocale(LC_ALL, "Portuguese"); worked normally to read and display the ç. Already when placed, the same thing happens: .

  • The decimal is -121 because it is being seen as char instead of unsigned char. You are testing directly on your console or running an IDE ?

  • But even using unsigned char it assumes the same symbol... I’m using Code Blocks.

  • Confirm that you have Codeblocks working with the correct encoding, in addition to using setlocale as already mentioned in the comments

No answers

Browser other questions tagged

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