Working with character (char) in C


Reading time: 30 minutes

C uses char type to store characters and letters. However, the char type is integer type because underneath C stores integer numbers instead of characters.In C, char values are stored in 1 byte in memory,and value range from -128 to 127 or 0 to 255.
In order to represent characters, the computer has to map each integer with a corresponding character using a numerical code. The most common numerical code is ASCII, which stands for American Standard Code for Information Interchange.

How to declare characters?

To declare a character in C, the syntax:

char char_variable = 'A';

Complete Example in C:

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    char character = 'Z';
    printf("character = %c\n",character);
    printf("character = %d,Stored as integer\n", character);
    return 0;
}

Output

character = Z
character = 90, hence an integer

C library functions for characters

The Standard C library #include <ctype.h> has functions you can use for manipulating and testing character values:

How to convert character to lower case?

  • int islower(ch)

Returns value different from zero (i.e., true) if indeed c is a lowercase alphabetic letter. Zero (i.e., false) otherwise.

char ch = 'z';
if(islower(ch))
    printf("Lower case");     
else
    printf("upper case");

Output:

Lower case

How to convert character to upper case?

  • int isupper(ch)

A value different from zero (i.e., true) if indeed c is an uppercase alphabetic letter. Zero (i.e., false) otherwise.

   char ch = 'Z';
 if(isupper(ch))
   printf("upper case");     
 else
     printf("lower case");
// Output: upper case

Check if character is an alphabet?

  • isalpha(ch)

Returns value different from zero (i.e., true) if indeed c is an alphabetic letter. Zero (i.e., false) otherwise.

   char ch = 'Z';
 if(isalpha(ch))
   printf("alphabet");     
 else
     printf("Numeric" );
 //output: alphabet

Check if character is a digit

  • int isdigit(ch);

Returns value different from zero (i.e., true) if indeed c is a decimal digit. Zero (i.e., false) otherwise.

  char ch = '1';
if(isdigit(ch))
  printf("numeric");     
else
    printf("alphabet" );
 // output: numeric

Check if character is a digit or alphabet

  • int isalnum(ch);

Returns value different from zero (i.e., true) if indeed c is either a digit or a letter. Zero (i.e., false) otherwise.

   char ch = '/';
 if(isalnum(ch))
   printf("numeric or ap=lphabet");     
 else
     printf("other symbol" );
 //output: other symbol 

Check if character is a punctuation

  • int ispunct(ch)

Returns value different from zero (i.e., true) if indeed c is a punctuation character. Zero (i.e., false) otherwise.

   char ch = '!';
 if(ispunct(ch))
   printf("punctuation");     
 else
     printf("other symbol" );
// output: punctuation

Check if character is a space

  • int isspace(ch)

Retuns value different from zero (i.e., true) if indeed c is a white-space character. Zero (i.e., false) otherwise.

   char ch = ' ';
if(isspace(ch))
  printf("space");     
else
    printf("other symbol" );
//output: space
  • char tolower(ch) & char toupper(ch)

The value of the character is checked other if the vlaue is lower or upper case otherwise it is change and value is returned as an int value that can be implicitly casted to char.

 char ch = 'A';
 ch =  tolower(ch);
 printf("%c",ch ); // prints   a
 ch = toupper(ch);
 printf("%c",ch ); //// prints   A

Find size of character using Sizeof()?

To get the exact size of a type or a variable on a particular platform, you can use the sizeof operator. The expressions sizeof(type) yields the storage size of the object or type in bytes.

In the below example the size of char is 1 byte, but the type of a character constant like 'a' is actually an int, with size of 4.

  char a = 'a';
  printf("Size of char : %d\n",sizeof(a));
  printf("Size of char : %d\n",sizeof('a'));

Output

Size of char : 1
Size of char : 4

Note:

  • All the function in Ctype work under constant time

What are the different characters supported?

The characters supported by a computing system depends on the encoding supported by the system. Different encoding supports different character ranges.

Different encoding are:

  • ASCII
  • UTF-8
  • UTF-16

ASCII encoding has most characters in English while UTF has characters from different languages.

The following table illustrates the characters in ASCII code:
char_ASCII-1

Question

Consider the following C++ code:

#include <stdio.h>
#include <ctype.h>
int main ()
{
  int i=0;
  char str[]="Test String.\n";
  char c;
  while (str[i])
  {
    c=str[i];
    if(islower(c))
     str[i] = toupper(c);
    if(ispunct(c))
    {
        printf("punctuation!!!!!!");
        return 0;
    }
    i++;
  }
  printf("%s",str);
  return 0;
}

What will be the output of the above code?

punctuation!!!!!!
TEST STRING
test string
Run time error
The code will print "punctuation!!!!!!" and return when "." is encountered