Loading...
C Data Types

C Data Types

In this tutorial, you will learn about basic data types in C programming such as int, float, char etc.


Introduction

Data types specify how we enter data into our programs and what type of data we enter. C language has some predefined set of data types to handle various kinds of data that we can use in our program. These datatypes have different storage capacities.

C language supports 2 different type of data types:

  1. Primary data types:

    These are fundamental data types in C namely integer(int), floating point(float), character(char) and void.

  2. Derived data types:

    Derived data types are nothing but primary datatypes but a little twisted or grouped together like array, stucture, union and pointers. These are discussed in details later.

Data type determines the type of data a variable will hold. If a variable x is declared as int. it means x can hold only integer values. Every variable which is used in the program must be declared as what data-type it is.


Data Types

Data types are used to identify the size of variable and type of datastored in it.

Example :

int points;

In the above example, we have declare a variable points of int int type.

There are four types of datatypes in C programming :

  1. Basic data types : int, float, double and char
  2. Enumeration data type : enum
  3. Derived data type : pointer, array, structure and union
  4. Void data type : void

Basic Data types

These are the basic datatypes in C programming :

Type Size (bytes) Format Specifier Range
int 2 or 4 %d, %i -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
char 1 %c -128 to 127 or 0 to 255
float 4 %f
double 8 %lf
short int 2 %hd -32,768 to 32,767
unsigned int 2 or 4 %u 0 to 65,535
long int 4 or 8 %ld, %li -2,147,483,648 to 2,147,483,647
long long int 8 %lld, %lli -263 to 263-1
unsigned long int at least 4 %lu 0 to 4,294,967,295
unsigned long long int at least 8 %llu 0 to 18,446,744,073,709,551,615
signed char 1 %c -128 to 127
unsigned char 1 %c 0 to 255
long double at least 10, usually 12 or 16 %Lf

Int

Int datatype is used to store an integer.

We have to use int keyword for declaring an integers (whole number both postive and negative including 0) before variable name.

Example :

int marks;

In the above example, marks is a variable of int type.

We can also declare multiple variables of same type at once in C programming.

Example :

int city_code, area_code;

The size of int is range from 2 bytes (16 bits) to 4 bytes (32 bits). And, it can take 232 distinct states from -2147483648 to 2147483647.

Note : If you want to store the integer value that crosses the above limit for int, then use "long int" and "long long int" to store the integer value.


float and double

Float datatype is used to store decimal numbers with single precision where as Double datatype is used to store double precision.

We have to use float keyword to declare a float type variable and double keyword to declare a double type variable.

Example :

float grade;
double percentage;

In C programming, floating-point numbers can also be represented in exponential.

Example :

float expo-number = 22.235d1;

What's the difference between float and double?

Both float and double are used to store floating-point number but the size of float is 4 bytes and the size of double is 8 bytes.


char

Char datatype is used to store a single character within a single quotation.

We have to use char keyword to declare a character datatype.

char datatype use 1 byte memory to store a single character.

Example :

char grade = 'B';

Note : It is necessary to write character inside the single quote.


Note : To increase the size of any of the basic datatype except char we have to use long and long long before datatype.
To decrease the size of the any of the basic datatype except char , we have to use short keyword before datatype.


signed and unsigned

signed and unsigned are type modifiers in which signed datatype can store 0, positive or negative values. unsigned datatype can only store 0 or positive value.

Example :

unsigned int a;
int c;

In the above example, the variable a can store only zero or positive values because we have used the unsigned modifier.The variable b can store zero, positive values or negative values because the datatypes are already modified as signed by default.

Note : The size of the unsigned datatypes are always greater than signed datatype.

Note : We cannot create variables of void type.


Void

Void means "nothing" or "no-type" i.e. It is an empty datatype that has no value and can be used in functions and pointers.
We can assume void as absent.

Note : void is mainly used to represent the function when it is not returning anything.


We will learn about remaining data types in later tutorials.


Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of Data Types in C.

Keep Learning : )

In the next tutorial, you'll learn about C Basic Input/Output.

- Related Topics