Character Data Types in C

Character Data Types in C Language with Examples

In this article, I will discuss Character Data Types in C Language with Examples. Please read our previous article discussing Integer Data Types in C Language. At the end of this article, you will understand everything about character data type in c language with Examples.

Character Data Types in C Language

The character data type is divided into two types: one is signed data type, and the second one is unsigned data type.

Character Data Types in C Language

Both Signed and unsigned data types occupy only one byte of memory. Unsigned means it will accept only positive values, and signed means it will accept both positive and negative values. 

Using 1 byte of memory, what minimum and maximum values can we store?

To understand this, look at the memory allocation process. Here I am taking 1 byte of memory. 1 byte equals 8 bits. It takes only binary values, i.e., 0 and 1. Now, if we place zeros in all eight places, the value will be zero, which is the minimum we can store in a 1-byte memory location, as shown in the image below.

Using 1 byte of memory what is the minimum and maximum value we can store?

If we place all ones in all the 8 bits, the value is 255. So, the maximum integer value we can store in 1 byte is 255, as shown in the below image.

Using 1 byte of memory what is the minimum and maximum value we can store?

So, using 1 byte of memory, the minimum integer value we can store is 0, and the maximum integer value we can store is 255.

Unsigned Character Range in C Language:

As we already discussed, unsigned means it will accept only positive values. And the range 28 equals 256. As positive value starts with 0, so, the unsigned character data type range is from 0 to 255.

Signed Character Range in C Language:

Now, let us understand the range of Signed character data types. The signed data type accepts both positive and negative values. So, we need to divide 28 = 256 by 2. 256/2, the value is 128. So negative values start with -1, -2, and up to -128, and the positive values start from 0 up to 127.

We are using character data type to store symbols like a, b, A, B, or some special symbols. Then, how can we represent such symbols in integers? Why character data type representation in integer. So, while working with Character Data Types in C Language, we need to understand the following four questions.

  1. Why does character limit representation in integers?
  2. How can we store symbols in one-byte memory? 
  3. What is a character system?
  4. What is ASCII?

Consider the diagram below. It is a simple program, and we name this program Program.c, and inside the main method, we declare one integer local variable and assign it a value of 10 and the remaining instructions are also there as it is. We can call it source code.

Character Data Types in C Language with Examples

In our previous article, we discussed that whatever program we have written uses any high-level programming language that the system cannot understand. This is because the system can understand only binary language. But you have written an English statement. We should convert all these high-level instructions into low-level ones. Who will convert? The Answer is the compiler.

The compiler is a predefined program. We need to pass the source code to the compiler, and the compiler will then generate the binary instructions code in the form of zeros and ones. So, the compiler needs to convert all these high-level instructions to machine level. Consider 10, and it will convert into binary, i.e., 1010, which is possible using the number system. So, using the number system concept we can convert the decimal value to binary value.

But here the problem is how it is converted #, <, >, a, I, A, etc. symbols into binary. If the decimal value is there, we can use a number system to convert it into binary. But how can we convert characters (a, b, A, B) and special symbols (#, <. >, etc.) into binary? The answer is the character system. The character system was introduced only for computer programming languages.

What is a character system?

Using a character system, we can represent one language into integer constants. For example, the English language contains capital letters, small letters, digits, special symbols, etc., and using a character system, we can represent all the above characters and symbols into integer constants. This is called a character system.

How Many Character Systems Are Available?

A list will come up if you search on Google. A number of character systems are available. The first computer was introduced into the market by IBM. IBM has its own character system. The famous one is the ASCII character system, and every programming language only follows the ASCII character system. Using the ASCII character system, let us see how to represent one particular language.

What is ASCII? What it stands for?

Now let us understand the English language ASCII Code. ASCII stands for Americans Standard Code for Information Interchange. A standard code means it is a fixed code, no one can change the value and no one can modify the value. It is used to interchange information from high-level language to low-level language.

How ASCII represents?

To understand how ASCII represents the English language, please have a look at the below diagram,

What is ASCII? What it stands for?

As you can see in the above image, capital A is represented by a constant integer value 65, and this is the fixed value and no one can change this value. The next one is for capital B, which is 66; for capital C, it is 67; and so on; for capital Z, it is 90. The value of small a is 97, and small b is 98, and so on up to small z, whose value is 122.

For digit 0, the ASCII value is 48; for 1, the value is 49 and for 9, the ASCII value is 57. You can construct any number using the digits 0 to 1, so they have given ASCII for 0 to 9 only.

For special characters, if it is a space, the value is 32; for #, the value is 35; and so on for every symbol. So, every character, digit, or special symbol is represented by a constant integer value in the character system, not only in the ASCII character system but in any character system available in the market.

So, for every language, like English, Hindi, and Odia, there is a character system. Here, the above diagram represents the English language using the ASCII character system, and these are the standard values.

How can we store a symbol in one-byte memory?

How can we store a symbol into one-byte memory?

Just count all the values, so the total will be 26 capital alphabets; we have in English 26 small letters and the next 10 numbers and not more than 150 special symbols. So here, if you add all these, then this is less than 256. At most, any language you can take in this world has 256 symbols. So, ASCII decided that if we assign the values for these symbols from 0 to 255, you can represent any character in the language using one byte of memory.

How can we say that one-byte memory?

256 is nothing but a 2 power 8 value. 2 power 8 is nothing but a one-byte memory. This is the only reason every character we can represent using one byte of memory in a programming language.

Character Data Types Examples in C Language

Now, we will see some of the examples of character data types in C language. First, let us understand the unsigned character and signed character in the form of circles.

Understanding signed char data type circle in c Language.

If it is a signed character the limits are from -128 to +127. Let us write all these limits in the form of a circle, and based only on these circles, we will see how programs will execute.

Whether it is a positive value or a negative value, the counting always starts with 0. Positive value counting starts from 0, 1, 2, and so on, up to 127 in a clockwise direction, and here the maximum positive value is 127. Negative values counting starts from -1, -2, -3, and so on up to -128 in an anti-clockwise direction, as shown in the below image.

Understanding signed char data type circle in c Language.

Note: In the declaration of the variable, if you are not specifying whether the variable is a signed variable or unsigned variable by default, it is a signed variable and can accept both positive and negative values.

Understanding unsigned char data type circle in c Language.

If it is an unsigned character, the limits are from 0 to 255, and the unsigned char data type accepts only positive values. In the case of unsigned char, the circle starts from 0, 1, 2, and so on and ends with 255 i.e. the maximum positive value is 255, as shown in the below image.

Understanding unsigned char data type circle in c Language
Example to understand character data type in c language:

Following is a simple example of a c program using char data type. Here, inside the main function, we declare one character variable with the name CH (you can give any name as per your choice) and assign this variable with the value A. In the C programming language, we represent characters by using single quotes. Then we print the character in the console. To print the character in the console, we need to use the format specifier as %c. %c is the format specifier for the character, and it will print the value A in the console. Next, we have also written the %d format specifier for the character variable CH. In this case, what will it print? Here, it will print character A’s corresponding ASCII value, which is 65.

#include <stdio.h>
int main()
{
    char CH = 'A';
    printf("%c", CH);
    printf(" %d", CH);
    return 0;
}

Output: A 65

Now we will see some tricky questions on character data type, mostly asked in interviews.

#include <stdio.h>
int main()
{
    char CH = 258;
    printf("%c", CH);
    printf(" %d", CH);
    return 0;
}

In the above example, we have initialized the character variable CH with a value of 258. Yes, we can store integers on the character data type. The above CH variable is, by default, a signed character. So, to understand what value it will store, we need to understand the signed char circle and see the actual value of 258. As 258 is a positive value, the count will start from 0, 1, and so on in a clockwise direction. In the circle, when it reaches 127, the next value is -128 (in count, it will be 128), the next is -127 (in count, it will be 129), and in the same way, -1 will be for 255, the next value in the circle is 0 which is 256, 1 for 257, and 2 for 258. So, in the variable, instead of 258, it will store.

Example to understand character data type in c language

So, in the output for the %d format specifier, it will print 2, and for the character specifier, it will print some unknown value i.e. 2 corresponding unknown characters it will print from the ASCII character system, and when you run the above code, you will get the following output.

Example to understand character data type in c language

Now, we will see how to write a program in which we will input one character, and it has to print that corresponding ASCII value. In this program, we are going to work with the scanner function. Using the scanf function, we are taking input from the end-user in the C programming language.

Character Data Types in C

In the C programming language, character data types are primarily used to store characters (letters, numbers, symbols). The most common character data type in C is char. Here’s a brief overview:

char:
  • The char data type is used to store a single character.
  • It typically requires 1 byte of memory space and has a range that can vary depending on whether the char is signed or unsigned.
  • A signed char typically ranges from -128 to 127, while an unsigned char ranges from 0 to 255.
  • Syntax: char ch = ‘A’;
signed char:
  • Specifically, it denotes a signed character type.
  • It can represent negative values as well as positive ones.
  • Syntax: signed char sch = -10;
unsigned char:
  • It represents only positive values and thus has a different range than the signed char.
  • Useful when working with ASCII values and byte manipulations.
  • Syntax: unsigned char uch = 255;
wchar_t:
  • Stands for “wide character type”.
  • Used for characters that require more memory to represent than a single byte (like international characters).
  • The size of wchar_t is compiler-dependent but typically larger than a standard char.
  • Syntax: wchar_t wch = L’अ’;

In addition to these types, C also allows for the creation of arrays of characters (char[]) which are used to represent strings. However, it’s important to remember that in C, strings are null-terminated; that is, they end with the special ‘\0’ (null character) to indicate the end of the string.

When dealing with characters in C, it’s crucial to understand ASCII values (for regular char types) and Unicode (often for wchar_t) to work with character encoding and processing effectively.

In the next article, I will discuss the Sizeof() function and limits.h header file in C language with Examples. In this article, I try to explain character data types in C language with examples, and I hope you enjoy this article.

1 thought on “Character Data Types in C”

  1. Kya aapka YouTube PR Hindi m channel h c fully details m explain k liye free or other programming language and database k liye

Leave a Reply

Your email address will not be published. Required fields are marked *