CHARACTERS IN C
To store characters and letters, C uses character types. However, because C stores integer numbers rather than characters behind it, the character type is integer. The range of char values in C is -128 to 127 or 0 to 255, and they are stored in 1 byte of memory.
The computer must use a numerical code to translate each integer to a corresponding character in order to represent characters. ASCII, which stands for American Standard Code for Information Interchange, is the most widely used numerical code.
Declaration of Characters in C:
Here, the character data type is declared using the keyword char, the variable name is variable_name (you may use any name you choose, such as a, b, c, alpha, etc.), and the line terminator (end of line) character is ; .
Syntax:
char character_name;
Example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char char_var = 'J';
printf("character = %c\n",char_var);
printf("character = %d Saved as an integer\n", char_var);
return 0;
}
OUTPUT:
![CHARACTERS IN C](https://static.tutorialandexample.com/c/characters-in-c1.png)
We can manipulate and analyses character values using the functions in the #include <ctype.h> section of the Standard C library. The following are the some of the functions:
Converting char to Lower Case:
If c is a lowercase letter of the alphabet, then the function returns a number that is not zero (i.e., true). Otherwise, it will be zero (false).
Syntax:
islower(ch)
Example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch = 'j';
if(islower(ch))
printf("LOWER CASE");
else
printf("UPPER CASE");
}
OUTPUT:
![CHARACTERS IN C](https://static.tutorialandexample.com/c/characters-in-c2.png)
Converting char to Upper Case:
If c is a upper case letter of the alphabet, then the function returns a number that is not zero (i.e., true). Otherwise, it will be zero (false).
Syntax:
isupper(ch)
Example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch = 'J';
if
(isupper(ch))
printf("UPPER CASE");
else
printf("LOWER CASE");
}
OUTPUT:
![CHARACTERS IN C](https://static.tutorialandexample.com/c/characters-in-c3.png)
Checking if character is an Alphabet:
If c is an alphabetic letter, then the function returns a value that is not zero (i.e., true). Otherwise, it will be zero (false).
Syntax:
isalpha(ch)
Example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch = 'J';
if
(isalpha(ch))
printf("ALPHABET");
else
printf("NUMERIC");
}
OUTPUT:
![CHARACTERS IN C](https://static.tutorialandexample.com/c/characters-in-c4.png)
Checking if Character is a Numeric:
If c is a numeric digit, then the function returns a value that is not zero (i.e., true). Otherwise, it will be zero (false).
Syntax:
isdigit(ch)
Example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch = '10';
if(isdigit(ch))
printf("NUMERIC");
else
printf("ALPHABET");
}
OUTPUT:
![CHARACTERS IN C](https://static.tutorialandexample.com/c/characters-in-c5.png)
Checking if Character is a Numeric or Alphabet:
If c is either digit or alphabet, then the function returns a value that is not zero (i.e., true). Otherwise, it will be zero (false).
Syntax:
ispunct(ch)
Example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch = '.';
if(ispunct(ch))
printf("PUNCTUATION");
else
printf("OTHER SYMBOL");
}
OUTPUT:
![CHARACTERS IN C](https://static.tutorialandexample.com/c/characters-in-c6.png)
Checking if Character is a Punctuation:
If c is punctuation, then the function returns a value that is not zero (i.e., true). Otherwise, it will be zero (false).
Syntax:
isspace(ch)
Example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch = ' ';
if(isspace(ch))
printf("SPACE");
else
printf("OTHER SYMBOL");
}
OUTPUT:
![CHARACTERS IN C](https://static.tutorialandexample.com/c/characters-in-c7.png)
Checking if Character is Punctuation:
If c is Space, then the function returns a value that is not zero (i.e., true). Otherwise, it will be zero (false).
Syntax:
isspace(ch)
Example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch = ' ';
if(isspace(ch))
printf("SPACE");
else
printf("OTHER SYMBOL");
}
OUTPUT:
![CHARACTERS IN C](https://static.tutorialandexample.com/c/characters-in-c8.png)
Finding the size of the characters:
Use the sizeof() operator to determine a type's or a variable's precise size on a specific platform. The result of the sizeof (type) expression is the object's or type's storage size in bytes.
In the example below, the character constant 'j' is typed as an int with a size of 4, even though the char variable's size is 1 byte.
Example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a = 'j';
printf("Size of the character : %d\n",sizeof(a));
printf("Size of character : %d\n",sizeof('j'));
}
OUTPUT:
![CHARACTERS IN C](https://static.tutorialandexample.com/c/characters-in-c9.png)
Different Characters Supported is:
The encoding that a computer system can support determines the characters that it can support. Different character ranges are supported by various encodings.
Various encodings include:
- ASCII
- UTF-8
- UTF-16
The majority of the characters in ASCII encoding are in English, whereas UTF include characters from several languages.
The ASCII code for the characters is shown in the table below:
![CHARACTERS IN C](https://static.tutorialandexample.com/c/characters-in-c10.png)