Home > front end >  How to use tolower or toupper with a special character such as Å
How to use tolower or toupper with a special character such as Å

Time:01-10

While programming in C, I encountered a problem regarding the tolower and toupper functions. I want to print an upper case 'Å', and then using the tolower function to print a lower case 'Å' ('å').

First, I declare a variable int x, and give it a value of 143 (because in the ASCII, Å is given that integer). When I then print out x using printf("%c", x) I will get an 'Å'. I also want to print an 'å' (a lower case Å), and to do so, I used the tolower function. However, I do not get an 'å', I only get 2 Å's and I do not understand why. If someone could explain it to me I will be grateful. Also, I am new to programming. Thus I might not use the correct terms at all times, but hopefully you will understand!

Here is my code:

#include <stdio.h>
#include <ctype.h>

int main(void){

int x = 143;

printf("Upper case is %c\n", x);
printf("Lower case is %c\n", tolower(x));

return 0;
}

Output:

Upper case is Å Lower case is Å

I also tried this, but the output was the same:

#include <stdio.h>
#include <ctype.h>

int main(void){

int x = 143;
int y;

y = tolower(x);

printf("Upper case is %c\n", x);
printf("Lower case is %c\n", y);

return 0;
}

CodePudding user response:

Have a look at: tolower

Return value

Lowercase version of ch or unmodified ch if no lowercase version is listed in the current C locale.

Since you get the unmodified value, that means no lowercase version was found.

To fix the problem, a solution might be to set the appropriate C locale (via setlocale).

Furthermore, i would suggest to read the following two articles, since the value 143 is not ASCII but Extended ASCII (and to get a better understanding what setlocale is good for):

  • Related