Characters with tilde in a char type arrangement


I have this code:

include <stdio.h>

define  TAM 64

int main(int argc, const char **argv) {
    char cadena[TAM] = {0};

    while (fgets(cadena, sizeof(cadena), stdin) != NULL)
        printf("%s\n", cadena);

When I run it in the GNU / LiNUX console and enter characters like "á" and then print it, I print it correctly. But if string is an array of char, that is, the range of values is [2 ^ (8-1), 2 ^ (8-1)] = [-128, 127] , with which should not be able to store and visualize the tilde, no? Because it is ASCII and we should use unsigned char .


asked by pelaitas 19.10.2017 в 17:42

1 answer


You're confused about it.

If you enter á , you are actually entering more than one char (if you have your console configured for UTF8 , which is the norm today).

Here comes into play the topic of coding , which indicates the relationship between a numeric code (not necessarily char ) and the spelling of a character.

You can do a simple check:

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

#define  TAM 64

int main( int argc, const char **argv ) {
  char cadena[TAM] = {0};

  while( fgets( cadena, sizeof( cadena ), stdin ) != NULL )
    printf( "bytes: %zu, texto: %s\n", strlen( cadena ), cadena );

You can find extensive information on the wikipedia .

answered by 19.10.2017 в 18:04