Microcontroller Embedded C Programming Lecture 24| Char data type and variable definition

 

Char data type and variable definition

 

 

In this article, let’s discuss one of the integer data types, Char.

 

Integer data type:  char

  • This is an integer data type to store a single character value or ASCII code value or 1 byte of a signed integer value (+ve or -ve value).
  • A char data type variable consumes 1 byte of memory.
  • Char happens to be the smallest integer data type of 1 byte.
  • There is no other special meaning for the char data type, and it is just another integer data type.

The name is used as Char, that’s because the char data types are majorly used to store the characters, that is, ASCII codes of the characters. That’s why the name is given as Char.

Apart from that, there is no special meaning for that, and it happens to be the smallest integer data type of one byte. 

 

Range of Char data type

  • Char range: -128 to 127 
    • A char data type will be used to store 1 byte of signed data.
    • For example, a temperature value. If it is negative sometimes or if it is positive sometimes, then it is called a signed data, and you can use the Char data type to store the temperature value.
  • Unsigned  char range: 0 to 255
    • An unsigned char data type will be used to store 1 byte of unsigned data. 
    • For example, if the distance between A and B is 100 kilometers, then we can use a variable of type unsigned char to store the unsigned data. 

 

Let’s see an example.

Figure 1. Example 1
Figure 1. Example 

 

Here we can use unsigned char data type to represent temperature value. cityXTemperature is a variable, and unsigned char is a data type, and the value 25 is stored in the variable.

Char datatype and variable definition
Figure 2. Example- 1

 

Here, cityXTemperature is a variable of type unsigned char to hold the integer value 25. So, here we are talking about integers. You cannot store 25.5. Why? Because that’s a real number. For real numbers, we have separate data types. Those are float data types, and about that will see later. So, all discussions are actually of integer data types.

 

That leads to our following discussion, which is a variable definition. The variable definition always includes the data type name followed by the variable name. So, this is an example of a variable definition.

Char datatype and variable definition
Figure 3. Variable definition

 

Here we can see that, first, the data type is written. Unsigned char is a data type that is given by your ‘C’ standard. This is a standard keyword unsigned char. And cityXTemperature is a variable name. So, this you can use whatever you want. But there are some rules to writing the variable name, so that will see later. 

A variable definition always includes two parts. One is the data type, and the second is the variable name, and the variable name should be followed by the data type.

Before using a variable, you should always define it using the appropriate data type. 

You can select the data type according to your program logic and need.

So here, the first line is a variable definition, and the second line is variable initialization, where we are storing value 25 to the variable.

 

Let’s see another example.

Char datatype and variable definition
Figure 4. Example-2

 

In this example, we store Sun’s temperature value in a program. Given Sun’s surface temperature is 5,505 degree Celsius. So, we can consider sunTemperature as unsigned data. So, if we assume Sun’s temperature never goes negative. 

5505 is greater than 255. So, we cannot use unsigned char because its range is from 0 to 255. In this case, we can safely use the unsigned short int data type.

unsigned short sunTemperature = 5505;

unsigned short is a data type, and sunTemperature is a variable name, to hold the value sun’s temperature, that is 5505. So, that’s about variable definition. 

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.