Viktor
Viktor

Reputation: 11

How to print out special characters like å, ä, ö in C?

If I try to print out "Ä" for example I get this character instead: õ. How do I fix this?

Here is my code:

#include <stdio.h>
#include <stdlib.h>
int main (){

char name[20];

printf("Enter first name: ");
scanf("%s", name);

if(strcmp(name, "Carl") == 0){
    printf("Carl är bra!");
}
else{
    printf("Kung!");
}
return 0;
}

(By the way I'm using code::blocks)

Upvotes: 1

Views: 2576

Answers (1)

selbie
selbie

Reputation: 104514

On Windows, set the console mode to UTF16 and use wprintf with a wide string literal instead of printf.

#include <io.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int main() {

    _setmode(_fileno(stdout), _O_U16TEXT);  // this is windows specific

    wprintf(L"Carl är bra!");
    return 0;
}

However, as I mentioned in the comments about preserving unicode chars in source. Better to just inline unicode chars with \uNNNN escape sequences. With print statements like the following.

    wprintf(L"Carl \u00e4r bra!");   //0x00E4 is 'ä'

Upvotes: 3

Related Questions