Learn C Data Types with Best Examples

In this tutorial, we will learn on how on the data types in C program works? The basic data types is consist of char, int, float, double, signed char, unsigned int, signed char and long double etc.

In addition, To engage in C Language, make sure you have knowledge about Data Types in C. To describe the variable in C language, we will need to define the type of data.

What is Data Type in C?

A type of data which stored in C program is called a data types. It’s used to define a functions or variables in C language. It’s necessary for a compiler to know the type of an inbuilt data going to the program.

In normal terms, a data types attribute to inform a computer how to read the value.

Also read or visit: Python Datatype With Examples

Data Types in C Example

For example the scenario in a bank company. The bank company stores different data of their employee like employee_name, employee_id, employee_age, employee_address, employee_salary, employee_phoneno, and etc.

These data consist of alphabet, numbers and etc. A way to make the enormous data is easy to programs. The information consist of various types:

  • employee_id: integer
  • employee_name: string
  • employee_age: integer
  • employee_address: string
  • employee_salary: double
  • employee_phoneno: integer

Basic Data Types in C

Data TypesQuick Access
PrimaryInt, Float, double, char
Derivedfunctions, arrays, pointers
EnumeratedEnums
VoidEmpty Value, null
BoolTrue or False

What is Primary Data Types in C?

The primary data types are also called an essential data types because in C programming language they are already existed.

Read also: Structure of C Program with Example

In addition, the primary data types is divided into 4 types.

1. Integer or Int

  • Int: Its indicate to a positive and negative whole numbers (without decimal), such as 1, 10, 100, 1000, 10000, 100000 and etc.

For example:

#include <stdio.h>   
void main() 
{ 
 int x = 18; 
 printf("The value of an integer is: %d \n", x); 

return 0;
} 

Output:

The value of an integer is: 18

2. Character or Char

  • Char: Its indicate for all the ASCII character with single quote such as lowercase ‘a’, uppercase ‘A’ and etc,

Lets have a look in the example below:

#include <stdio.h>   
void main() 
{ 
 char x = 'y'; 
 printf("The value of a character is: %c \n", x); 


return 0;
}

If we execute the example code above the output is like this:

The value of a character is: y

3. Floating-point or float

  • Float: Its indicate for all real number values or decimal points, like 1.25, 20.19, 200.24, etc.

Now, Lets have a look in the example below:

#include <stdio.h>   
int main() 
{
 float x = 12.4521; 
 printf("The value of the float x is: %f \n", x); 
 
 return 0;
} 

Output:

The value of the float x is: 12.452100

4. Double or double

  • double: Its used if the range reach the numeric values that does not come either floating-point or integer data type. 

For example:

#include <stdio.h>   
int main() 
{ 
 double x = 89.56732; 
 printf("The value of double x is: %lf \n", x); 
} 

Output:

The value of double x is: 89.567320

Try here to run online the code example above: Free Online Compiler of C

A data types is need to be specific keywords for representing them:

Keyword UsedData Type
intInteger
floatFloating-point
doubleDouble
charCharacter
voidVoid

Data Type Modifiers In C

In a C program, the modifiers is a C keywords that it can change the necessary data types. It can reveal if how much a memory will be able to assigned to a variable.

You might interested to read this tutorial: Learn C Basic Syntax Rules with Examples

Furthermore, to modify the memory assigned in a variable, we need is the modifiers that are prefixed with a specific data types. In C language there are 4 data types modifiers:

  • short
  • long
  • signed
  • unsigned

In addition, for these modifiers its make the memory recommended for primary data types.

Size Of Data Types In C

Each size of a data type is specify in bytes(8 bits) or in bits . In every data types in C is similar with a different range of values specified as below:

unsigned char%c0 to 2558
char%c-127 to 1278
signed char%c-127 to 1278
int%d, %i-32,767 to 32,76716 or 32
unsigned int%u0 to 65,53516 or 32
signed int%d, %i-32,767 to 32,767 (same as int)16 or 32
short int%hd-32,767 to 32,76716
unsigned short int%hu0 to 65,53516
signed short int%hdSame as short int16

Reminders: The format specifiers is used when printing the value of a variable along with the printf() functions.

C Data Type Value in Out Of Range

If we try adding a value which is out in the range of the data type. It might result an error from the C compiler.

Lets have a look the given example below:

#include <stdio.h>
int main() 
{
 // the allowed maximum value in the int is 32,767
 int x = 100000;
 printf(x);
 return 0;
}

Output:

warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]

Derived Data Types in C

The derived data types is a primary data types which are unified together. We can unified many elements of common data types. These data types are specify by the user.

The following list are the example of derived data types in C:

  • Array
  • Pointers
  • Structure
  • Union

Array

In C program, an array in C is a set of multiple values of a common data types and it is stored in a stacked memory location. The array are contains of chars, int, doubles, long, and etc.

Read also: Python Variables With Examples

Syntax of Array in C

data_type array_name[array_size];

Lets have a look an example of array in C

#include<stdio.h>   
int main(){ 
int i=0; 
int grades[5];//declaration of array 

grades[0]=90;//initialization of array 
grades[1]=95; 
grades[2]=94; 
grades[3]=97; 
grades[4]=93; //traversal of array 
for(i=0;i<5;i++){ 
printf("%d \n",grades[i]); } 
return 0; 
}

Output:

90
95
94
97
93

Data Type in C Pointers

In a C program, these pointer data type is used to store a variable’s in a storage. The pointers grant users to perform dynamic memory allocation. They also support passing variables through reference.

Furthermore, a null is a pointer without an address while the pointer without data type is called void pointer. It can specified by using a symbol asterisk (“*”) operator.

Lets have a look an example of a pointer in C

int main(void) { 
int *pointer1; 
int *pointer2; 
int x = 30; 
int y = 100; 

pointer1 = &x; 

pointer2 = &y; 

printf("%d", *pointer1); 
printf("\n%d", *pointer2); 

printf("\n%d", pointer1); 
printf("\n%d", pointer2);  

int minus = pointer2 - pointer1; 
printf("\n%d", minus); 
return 0; } 

Output:

30
100
-271561236
-271561232
1

Conclusion

To conclude, we learned about various C data types with basic examples and easy to understand the codes. We also explained the examples of every data type. We hope that this details about C data types will be able to help you for creating the profitable programs in C programming language.

In the previous tutorial, we discussed the Learn C Basic Syntax Rules with Examples and for the next tutorial, we will discuss the “C Variables.

Leave a Comment