How to read a charde a file and convert to integer

0

The following code snippet should show the integer value of the single character stored in the file. when I put a letter like 'a' for example it works. but when I put this symbol: þ. and several others it shows -2 on the screen. and this symbol according to the ASCII table has value 231. The code is this:

#include <stdio.h>

int main() {
    FILE *ptr;
    char c;
    ptr = fopen("teste.txt", "r");
    fscanf(ptr, "%c", &c);
    printf("%d", c);
    return 0;
}

The only thing stored in test.txt is the symbol þ.

    
asked by anonymous 10.10.2015 / 18:54

1 answer

0

In the

printf("%d", c);

char is automatically converted to int without having to do anything.

This is part of the "integer promotions" rule (see 6.5.2.2p6 in Standard C11 (English))

    
10.10.2015 / 19:23