What is the
char type in C and how should it be used.
Before we dive into these, let's have a quick glance at C types.
Types are useful to us because they allow us to handle values in a specific way.
In the backend though, C uses types to determine how much memory to allocate for a given value.
C is very particular about memory and it needs to know the type of every value.
This special type refers to printable and control character - basically every key on your keyboard!
The syntax for declaring a variable as a
char looks like this
char character = 'Z';
Note that single quotes
''are used instead of double quotes
Under the hood, all
char type translate to an integer. So you declare character like this
char c = 48
char c = '0'
You can view the a table of all characters and their integer representation here
C provides a library for handling characters in an exciting way 🔥
You can use that library by including the
ctype.h library at the top of your script like this
Here are a few utilities the library gives you for free...
char c = 'a'; if (islower(c)) // do something
islower returns 1 if a character is lower case, and 0 if it's not.
char c = 'A'; if (isupper(c)) // do something
isupper returns 1 if a character is upper case, and 0 if it's not.
char c = 'A'; if (isalpha(c)) // do something
isalpha returns 1 if a character is an alphabet, and 0 if it's not.
char c = '4'; if (isdigit(c)) // do something
isdigit returns 1 if a character is a decimal number, and 0 if it's not.
char c = 'y'; if (isalphanum(c)) // do something
isalphanum returns 1 if a character is a digit or alphabet, and 0 if it's not.
Bet you can guess what these functions do
You can also convert characters to uppercase and lowercase using
char c = 'A'; // Switch case if (isupper(c)) c = tolower(c); else if (islower(c)) c = toupper(c));
Emojis are also chars!
char damn = '🥵'