Datatypes in C language | Different types of Datatypes in C

Spread the love

Introduction:

In the previous article, We discussed about the Escape Sequences in C programming Language. In today’s article, We will learn about the different Datatypes in C language and Limits or Ranges of datatypes.

Datatypes-in-c-programming-language

Datatypes in C Language :

We have different types of Data like Alphabets, Integers, Decimal point data,..etc. In general, We deal with each and every data differently Similarly Computers also have Different types of data. They also follow a different type of storage representations for each type of data.

As the C Programming Language is Strongly Typed Language. We need to give clear information about the Data (Variables, etc)  Like specifying the type of the data, So that compiler will be aware of the datatype. So while creating the variable we need to explicitly specify the datatype of variable. 

The C Language have different types of Datatypes to store different data values. For example, To store the Integer data values, C language have ‘int’ datatype. Here is the list of fundamental datatypes in C. 

C Programming Language have Three Fundamental Datatypes,

  1. Integer Datatype [ Denoted by int ].
  2. Character Datatype [ Denoted by char ].   
  3. Float Datatype [Denoted by float ].  

We also have other datatypes like arrays, Strings and Enums, Which are called derived datatypes. We are going to learn about them in later.

For now, We are going to concentrate on the fundamental datatypes only.

Integer Datatype ( int ) :

The Integer datatype is used to store the Integer data values.

Examples of Integer data values are 10, 20, 100, etc.

So whenever we create a variable to hold integer data, Then we need to use int datatype.

Example of Integer variable creation

In the above code snippet, We created a variable named ‘num’ and the datatype of the ‘num’ is Integer ( ‘int’) and assigned the Integer value 10.

The Integer data can be Positive values or Negative values. The sign in front of the data is going to decide the value type.

The 10 is positive Integer value. and -100 is a Negative Integer value.

Program to demonstrate the Integer datatype:

In this program, We are going to create few integer variables and then display them on the console using the printf function.

Output:

The Integer datatype can further divided into two types.

  1. Signed Integer Data
  2. Unsigned Integer Data

Signed Integer Datatype:

The Signed Integer data will have both positive and negative values.

Ex: -100 , 300 , etc

The Default type of the int datatype is signed Integer.

int a = -10;

Here variable ‘a’ is a signed Integer, So it is able to store the negative integer value. If you print the value of variable ‘a’, C Compiler display the excepted value -10.

We use %d format specifier for Signed Integer data.

Example : Signed Integer Datatype:

Output:

As you can see, We got the desired value -10.

Unsigned Integer Datatype:

The Unsigned Integer datatypes only contains the Positive values. So Negative values are not allowed.

Even if you specify the negative values, They will be type-casted to unsigned value ( i.e Positive value).

We use %u format specifier to represent the Unsigned Integer data.

Example Program to understand the Unsigned Integer Datatype:

Output

As you can see from the above output, The -10 is converted to unsigned integer ( By using the overflow technique). The resulted value is 4294967286

There are few cases, Where our Integer value never goes to negative number, In such cases we can take advantage of the Unsigned Integer datatype.

Character Datatype:

Character Datatype is used to Store Single Character.

Example of character data.
'V' 'N', 's', etc

📢 Only one character is allowed for character datatype.

We can create a character datatype variable using the char keyword.

Here we have created a character variable 'ch' and assigned value 'V' to it.

We can also have unsigned character data as well.

We use %c format specifier to represent the character datatype values. So to display the character data using printf we need to use %c format specifier. Similarly, We need to %c to take the input using the scanf function.

📢 Learn More about Format specifier here – Format Specifiers in C programming – SillyCodes

Character datatype Example Program:

Output

In the above program, We have created a character variable 'ch' and assigned value 'V'. Then we used printf function with %cformat specifier to print the character variable.

The character datatype only accepts one character. If you try to use more than one character, it will result into the compilation error. There are few compiler which shows the compilation warning and takes the last character as the value. For example, if you specify ch = 'VE', Then it will take 'E' as the character.

But It is recommended to use only single character for your character datatype variables. If you need to use the multiple characters then consider using the c language derived datatype called String datatype. String datatype can hold multiple characters and used mostly for string operations. We will learn more about the string datatype in upcoming articles.

Float Datatype in C language:

The Float Datatype is used for Storing Floating point values.

Example of floating point data.
10.56, 78.37, 10.0, etc

Floating point data also called as the real data.

The float keyword is used to create the floating point data variables in C Language.

We use %f format specifier to represent the floating / real data. So use %f format specifier in printf and scanf like formatting output and input functions.

Program to demonstrate Float Datatype:

Program Output:

Float datatype is capable of storing the Six digits after the Point( Decimal point) or Period .
Example:

431.408430 

As you can see we have 6 digit after the period. It is the maximum number of digit you can have with C Floating point data.

If you create a float variable with more than 6 digits after the decimal point, Then C compiler will decrease it to 6 precision points value.

Float Data 6 Digit precision:

Output:

As you can see the above value '44.1234567891234567' converted into the '44.123455', As a 6 precision points floating data

Program with all basics datatypes in C Language:

In the above program, we have one integer variable named i, Which is used to store the decimal value 10. Similarly, we have variable 'ch' to store character 'a'. Here the 'ch'  datatype is char and variable 'f'  is used to store floating point data 12.36.

C Programming Language also have the Qualifiers, We already discussed about the signed and unsigned Integers or characters. We also have Size Qualifiers. 

  • Size Qualifiers – Short and Long. 
  • Sign Qualifiers – Signed and Unsigned.

Size Qualifiers:

  • Size Qualifiers are the talks about the size of the datatype.
  • C Language Integer also have different types based on the size, Like the C language supports short int, int or long int, and long long int.
  • Size Qualifiers helps us to use the storage efficiently, For example if you know your number is not going to go beyond 32000, Then you can go for the short int.
  • If you need to have very very big number, Then you need to use the long long int datatype, Which support very big number. So that you can avoid the Integer Overflow problem.

Sign Qualifiers :

  • When the qualifier signed is used Number may be positive or negative.
  • When the qualifier unsigned is used Number always Positive.
  • If the sign Qualifier is not mentioned, then the default qualifier is signed qualifier. So value may be positive or Negative.
  • The range of signed values are always less than the Unsigned values because in Signed values Leftmost bit is represents the Sign Value.

📢 The Unsigned float and unsigned Double are not allowed in C ( In fact floating point data is represented using the IEEE 754 Document structure.- Real data is not a Number NAT).

Basic Datatypes and Size and Ranges of Datatypes in C Language:

DatatypeFormat SpecifierSizeRange or Limits
char %c1 Byte-128 to 127
int %d4 Bytes-2147483648 to 2147483647
float %f4 Bytes1.175494e-38 to 3.402823e+38
double %lf8 Bytes2.225074e-308 to 1.797693e+308

📢 The above Datatype Sizes and Ranges are my GCC compiler, So they might not be same for your compiler and machine. Note that, Datatypes Sizes and Ranges are Compiler dependent.

We have only listed the basic datatypes in above table, We also discussed about all datatypes and their sizes and Ranges or Limits in followin article, Please check out for more info

Conclusion:

We have discussed the basic datatypes in C programming Language.

Related Reading:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. Glory Pachnanda says:

    Nice article. You describe Qualifiers in a very good way.

  1. June 25, 2021

    […] you can see from the syntax the datatype should be any valid C datatype. And variablename is the name of the variable. You are free to name your variables. But you need to […]

Leave a Reply