Type Here to Get Search Results !

Char data type in C

In this article, I am going to discuss Character Data Types in C programming with Examples. You can read my previous article where I've discussed All Data Types in C programming in short

www.dheerajpatidar.com
At the end of this article, you will understand everything about character data type in c language.

Character Data Types in C Language

Char Data Type is used to store single character in variable. char type of variable acquires 1 byte of memory in memory.

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

www.dheerajpatidar.com

Both signed char data type and unsigned char data type acquire 1 byte of memory. Unsigned means it will accept only positive values and the signed means it will accept both positive as well as negative values. Whatever the type either signed or unsigned, the character occupies only one byte.


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

To understand this, look at the memory allocation process. Here I am taking 1 byte of memory. 1 byte equals 8 bits. And it takes only binary values i.e. 0 and 1. 

Now, if we place zeros in all 8 places then the value will be zero which is the minimum we can store in a 1-byte memory location as shown in the below image.

www.dheerajpatidar.com

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.

www.dheerajpatidar.com

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 as well as 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.

Now the question arises, 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:

  • Why does character limits representation in integers?
  • How can we store symbols into one-byte memory nothing but why character occupies one-byte memory?
  • What is a character system?
  • What is ASCII?

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

www.dheerajpatidar.com

whatever program we have written using 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. 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 which is in the form of zeros and ones. So, the compiler needs to convert all these high-level instructions into machine level. 

Consider 10, it will convert into binary i.e. 1010 and this is possible by 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 we can convert characters (a, b, A, B) and special symbols (#, <. >, etc.) into binary? The answer is the character system. Only for computer programming languages, the character system was introduced.


What is a character system?

Using a character system, we can represent one entire 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 if you search on google. A number of character systems are available. The first computer was introduced into the market by IBM. 

IBM is having its own character system. Now, the famous one is the ASCII character system, and every programming language follows the ASCII character system only. Let us see how using the ASCII character system, we can represent one particular language.


What is ASCII ?

ASCII stands for American Standard Code for International Interchange This ASCII System Consist of Total 256 (0 to 255) Characters. Through ASCII System we can Easily Access Characters in Computer.

ASCII System Having Number from 0 to 255 for Each if the Character. This system is Applied for All Programming Languages.


Why Char Data Type Range or Limit represent in integer?

Because we are representing a character using Character system Called ASCII. ASCII Consist of total 256 (0 to 255) character. Due to which each character having perticular number. We Can Say that it is a Predefined Enumeration of All Characters in ASCII for All Computer Programming Languages.


Why Any Character Required 1 byte memory in Computer?

character Required 1 Byte memory for Allocation Because total characters in any programming language is definitely not more than 256.

Means 256 is equal to 2^8 and 2^8 is 1 byte. That's why one character Required 1 Byte memory in Computer.


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 on these circles only we will see how programs will execute.

Either positive value or negative value always the counting 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 in up to -128 in an anti-clockwise direction as shown in the below image.

www.dheerajpatidar.com

Note: In the declaration of 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, so on and ends with 255 i.e. the maximum positive value is 255 as shown in the below image.

www.dheerajpatidar.com

Example to understand character data type in c language:

#include <stdio.h>

int main()

{

    char CH = 'A';

    printf("%c", CH);

    printf(" %d", CH);

    return 0;

}


Output: A 65


One more program:

 

#include <stdio.h>

int main()

{

    char CH = 258;

    printf("%c", CH);

    printf(" %d", CH);

    return 0;

}

 

Output: 2


Write a program to display ASCII value of character input by end-user.

 

#include <stdio.h>

int main()

{

    char CH;

    printf("Enter a Character : ");

    scanf("%c", &CH);

    printf("ASCII Value is %d", CH);

    return 0;

}


Output: 

Enter a Character : h
ASCII Value is 104


Conclusion

This is all about Character Data Type in C. If you have any query please comment that question so that I will reply your query as soon as possible.

I will soon publish all char data type related programs, so keep in touch with me.

Thanks for visit!



< Previous Post
  Next Post >


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.