Microcontroller Embedded C Programming Lecture 22| Data types in ‘C’

 

Data types in ‘C’

 

 

 

Let’s learn about ‘C’ data types and variables in this article.

 

Data types: 

  • Data type is used for declaring the type of a variable. 
  • In C programming, data types determine the type and size of data associated with variables. 
  • Before storing any value in a variable, first programmer should decide its type.

 

Let’s explore the various data types available in the ‘C’ programming language.

Representing real world data

  • Data as numbers(Integer or real numbers)
  • Data as characters
  • Data as strings(Collection of characters)

Strings are nothing but collections of characters. For example, your name is a type of data, which is called string data. So, data can be in the form of numbers. A number can be again integer, or it can be a real number.

 

Let’s see some examples.

Figure 1. Example
Figure 1. Example

 

  • Roy’s age is  44 years, 6 months, and 200 days 

Here, data is nothing but a number. Like 44, 6, and 200 are numbers that we have to use in our program to manipulate the data.

  • The temperature of the city X is +27.2 degrees. 

27.2 is a real number here, and the X is a character. 

  • I got an ‘A’ grade in school assignment

Here the data is grade, that is A. ‘A’ is a character. 

All these are different types of data available in the real world.

 

How to represent data types in the program?

Based on the real-world data, we have two significant data types in ‘C.’

Figure 2. ‘C’ data types
Figure 2. ‘C’ data types

 

‘C’ data types:

  1. Integer data types:
    • Integer data types are used to represent whole numbers. 
    • Example: 10, 20, 30, etc.. 
  1. Float data types: 
    • Float data types are used to represent real numbers. 
    • Example: the temperature value is 24.5. So, if a programmer wants to give more attention to the variation of temperature, then he has to consider a temperature value as a real number. 

 

Integer data types

Integer data are represented by different integer data types. 

1. Integer data types for signed data: 

The real-world data can sometimes be signed, or sometimes it may be unsigned. For example, temperature value. A temperature can be negative sometimes, or it can be positive sometimes. That’s why a temperature value is considered a signed data because that data can be positive or negative. 

For example, consider the distance between point A to Point B.  Distance value cannot be negative. It will always be positive. So, that type of data is called unsigned data.

 

Figure 3. Integer data types(for signed data)
Figure 3. Integer data types(for signed data)

Integer data types for signed data are char, short int, int, long int, and long long int. All these are different types of integer data types available in ‘C.’ 

Remember that instead of calling short int, you can also call it just short. If you call it as short, then it is assumed that you are referring to short int.  So, you need not write or mention long int; mention it as long. A long is always assumed as long int.  And the same way, you need not mention long long int; you can just mention long long.

 

2. Integer data types for unsigned data: 

If your data is unsigned, then you can use these integer data types(Figure 4). Everything is the same, and you have to use the keyword unsigned before the data type name. Like unsigned char, unsigned short int, unsigned int, etc. So, you have to use the word unsigned. 

Figure 4. Integer data types(for unsigned data)
Figure 4. Integer data types(for unsigned data)

 

In the following article, let’s discuss storage sizes of different data types and value ranges. 

 

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.