Menu Close

The Character Data Type and Character Variable

Posted in C Programming

1.What is Character Data Type ?

In C programming, the character data type is a primitive data type used to store a single character. The character data type is represented using the keyword ‘char’ and is typically one byte in size. It can store characters, such as letters, digits, punctuation marks, and other special characters.

A character variable is a variable used to store a value of the character data type. It is declared using the ‘char’ keyword, followed by the variable name. For example, to declare a character variable named ‘myChar’ and assign it the value ‘A’, you can use the following code:

1.1 Syntax:

char myChar = 'A';

In the above code, ‘myChar’ is the variable name, ‘char’ is the data type, and ‘A’ is the value assigned to the variable.

Character variables are commonly used in C programming for various purposes, such as input/output operations, string handling, and other applications that require character manipulation.

1.2 The Memory of char Data Type

Characters are stored in 8 bits or 1 byte of memory and can be either signed or unsigned. There is no separate “character type” in the C language. The ‘char’ type is essentially the smallest integer type, and as such, it can be signed or unsigned, just like any other integer type.

How many memory storage for a char Data Type.

  • char  1 byte −128 to 127
  • signed char 1 byte −128 to 127
  • unsigned char 1 byte 0 to 255

Both signed and unsigned characters are stored as a single character based on their ASCII value. For example, the character ‘A’ will be stored as 65. We do not need to specify the keyword ‘signed’ to use a signed char in C programming language.

Related:   Why Are Integers Stored in Memory in Two's Complement Form ?

 

//Both are same
signed char name = ‘a’;
char name = ‘a’;

n C programming language, we need to use the ‘unsigned’ keyword to specify an unsigned character. If the ‘unsigned’ keyword is not used, the character is assumed to be signed by default.

For example, to declare an unsigned character variable named ‘myUnsignedChar’ and assign it the value ‘B’, you can use the following code:

unsigned char myUnsignedChar = 'B';

In the above code, ‘myUnsignedChar’ is the variable name, ‘unsigned char’ is the data type, and ‘B’ is the value assigned to the variable.

1.3 Character Input and Output

the character data type is stored using one byte (8 bits) in computers. In C programming language, characters are treated as integers, so the character data type is essentially an integer with a width of one byte. Each character corresponds to an integer value (determined by its ASCII code), such as ‘B’ corresponds to the integer 66.

The default range for character data type varies across different computer systems. Some systems use a range of -128 to 127, while others use a range of 0 to 255. Both ranges can cover the ASCII character range from 0 to 127.

The ASCII table contains 128 character values from 0 to 127, while the remaining 127 characters are referred to as the extended ASCII character set.

Example 1.3.1  Characters are Input and Output as Integers

char c = 66; // equals to
char c = 'B';

As long as the integer value is within the range of the character data type, integers and characters can be interchanged and assigned to variables of the character data type. In the example above, the variable ‘c’ is of the character data type and assigned an integer value of 66.  This is equivalent to assigning the character ‘B’ to the variable ‘c’.

#include <stdio.h>
#include <stdlib.h>

int main()
{
   char a = 'B';
   char b = 66;
   printf("a=%d|b=%d\n", a,b);
   return 0;
}

Example 1.3.2.  Characters Input as Integers and Output as Characters

 
#include <stdio.h> 
#include <stdlib.h> 
int main() 
{ 
   char a = 'B'; 
   char b = 66; 
   printf("a=%c|b=%c\n", a,b); 
   return 0; 
} 

1.4 Mathematical Operations Between two Variables of Character Data Type

 

char a = 'B'; // equals to : char a = 66;
char b = 'C'; // equals to : char b = 67;
printf("%d\n", a + b); // Output: 133

In the above example, adding two character variables a and b is treated as adding two integers. The placeholder %d is used to print the decimal integer value of the result, which is 133.

Related:   Data Types in C Program

In the signed character extended set, the values range from -128 to -1, while in the unsigned character it ranges from 128 to 255. When you try to assign a value of 128 to a signed character, it will wrap around like a clock and take the value of -128.

char Variables mathematic operations
char Variables mathematic operations

Similarly, if you assign a value of 256 to an unsigned char, it will wrap around and take the value of 0.

Example 1.4.1 Overflow of unsigned char.

 

#include <stdio.h>
#include <stdlib.h>

int main()
{
   unsigned char a = 256;
   printf("chartype a=%c\n", a);
   printf("integertype a=%d\n", a);
   return 0;
}

Leave a Reply