Datatypes in C language | Different types of Datatypes in C
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 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,
- Integer Datatype [ Denoted by int ].
- Character Datatype [Â Denoted by char ]. Â Â
- 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
1 |
int num = 10; |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
#include<stdio.h> int main() {      int number1 = 10;     int number2 = -500;     int number3 = 0;      // Printing the above numbers     printf("number1 : %d \t number 2 : %d \t number3 : %d \n", number1, number2, number3);      return 0; } |
Output:
1 |
number1 : 10Â Â Â Â number 2 : -500Â Â Â Â Â Â Â Â number3 : 0 |
The Integer datatype can further divided into two types.
- Signed Integer Data
- 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:
1 2 3 4 5 6 7 8 9 10 |
#include<stdio.h> int main() {      int number1 = -10;      // Printing the above number     printf(" number1 : %d \n", number1);      return 0; } |
Output:
1 |
number1 : -10 |
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:
1 2 3 4 5 6 7 8 9 10 |
#include<stdio.h> int main() {      unsigned int num = -10;      // Printing the above number     printf(" num : %u \n", num);      return 0; } |
Output
1 |
num : 4294967286 |
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.
1 |
char ch = 'V' |
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:
1 2 3 4 5 6 7 8 9 10 11 |
#include<stdio.h> int main() {      // Creating a Character variable 'ch'     // And Assigning value 'V' to it     char ch = 'V';      // print the variable 'ch' using the '%c' format specifier.     printf("The character 'ch' Value is : %c \n", ch);     return 0; } |
Output
1 |
The character 'ch' Value is : V |
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:
1 2 3 4 5 6 7 8 9 10 |
#include<stdio.h> int main() {      // Create a float variable 'f'     float pi = 3.143;      // Print the variable 'pi' using the '%f' format specifier     printf("The value of 'pi' Value is : %f \n", pi);     return 0; } |
Program Output:
1 |
The value of 'pi' Value is : 3.143000 |
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:
1 2 3 4 5 6 7 8 9 10 11 |
#include<stdio.h> int main() {      // Create a float variable 'num' with 10 precision digits     float num = 44.1234567891234567;      // Print the variable 'num' using the '%f' format specifier     // Let's see how many digits are printed after the decimal point.     printf("The value of 'pi' Value is : %f \n", num);     return 0; } |
Output:
1 |
The value of 'pi' Value is : 44.123455 |
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:
1 2 3 4 5 6 7 8 9 10 |
#include<tstdio.h> void main() {         int i = 10;         char ch = 'a' ;         float f = 12.36 ;         printf(" Value of i  : %d n",i);         printf(" Value of ch : %c n",ch);         printf(" Value of f  : %f n",f); } |
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:
Datatype | Format Specifier | Size | Range or Limits |
---|---|---|---|
char | %c | 1 Byte | -128 to 127 |
int | %d | 4 Bytes | -2147483648 to 2147483647 |
float | %f | 4 Bytes | 1.175494e-38 to 3.402823e+38 |
double | %lf | 8 Bytes | 2.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.
Nice article. You describe Qualifiers in a very good way.