C Data Types

In this tutorial, we will learn about basic data types in c programming such as int, float, char, double etc.

Data types basically used to declare type and size of the value associated with variables. For example,

int testvariable;

In above snippet, testvariable is name of variable and int is type of the value stores in the variable. The size of testvariable is 4 bytes since integer can contain 4 bytes.

Basic data types in c language

Please have a look on commonly used data types in c programming languages. We generally use these datatypes in our program. Here is list below with Type, Size (in bytes) and Format specifier for each data type.

TypeSize (bytes)Format Specifier
intat least 2, usually 4%d, %i
char1%c
float4%f
double8%lf
short int2 usually%hd
unsigned intat least 2, usually 4%u
long intat least 4, usually 8%ld, %li
long long intat least 8%lld, %lli
unsigned long intat least 4%lu
unsigned long long intat least 8%llu
signed char1%c
unsigned char1%c
long doubleat least 10, usually 12 or 16%Lf
Table of different data type, size and format specifiers

let’s discuss about each data type in detail so you will have better explanation on these.

int data types

Integer data type can have all the positive and negative numbers including zero but not decimal values. For example, 0, -3, 12, 200 are valid integer numbers but 1.3, 2.0, -3.4 are not valid integer.

For integer data type, we can use int keyword to declare a variable. We can also also declare multiple integer type variables at once. For example,

int rollnumber, marks;

Here, rollnumber and marks are variables of type integer.

The size of int data type is usually 4 bytes (32 bits). And, it can have 232 distinct states from -2147483648 to 2147483647.


float and double data types

float and double datatypes are basically used to define real numbers. For example,

float percentage;
double cost;

We can also represent floating-point numbers in exponential format. For example,

float normalizedFactor = 22.442e2;

So here once question is clicking in our mind, what is main difference between float and double data type?. Let’s understand the different between both these data type.

Float:- the size of float (single precision float data type) is 4 bytes.
Double:- the size of double (double precision float data type) is 8 bytes


char data types

The keyword char is basically used to declare character data type variable For example,

char alphabet = 'h';

The size of the a character data type variable is 1 byte.


void data types

When you writing a program and doesn’t have any value to return the function, you can use void keyword just before function name. For example,

void testfunctino() {
    /* code goes here */
}

In the above example, we don’t have any value to return to this function that’s why we used void keyword. void is kind of “nothing” or “no type” data type. For example. if your function not returning anything, the return type should be void which says there is no value to return.

NOTE: you can’t create any type of variables of void type 🙂


short and long data types

When you want to use any large number in your program, you should use a data type specifier is long. Let’s see in below example,

long x;
long long y;
long double z;

Here variables x and y can store integer values. And, z can store a floating-point number.

In your program, when you are you will use number between [−32,767, +32,767], you can use short data type.

short smallnumber;

If you want to check size of any variable, please use sizeof() operator. It will give you size the variable in byte. See in below program.

#include <stdio.h>      
int main() {
  short a;
  long b;
  long long c;
  long double d;

  printf("size of short = %d bytes\n", sizeof(a));
  printf("size of long = %d bytes\n", sizeof(b));
  printf("size of long long = %d bytes\n", sizeof(c));
  printf("size of long double= %d bytes\n", sizeof(d));
  return 0;
}

signed and unsigned data types

In C language, signed and unsigned are type modifiers. It means you can change the data storage of any data type using them. Let’s see in below example,

unsigned int x;
int y;

As we learned above, an integer variable can hold zero, negative and positive number, but here we alter the integer data storage using unsigned modifier so now it can hold only zero and positive values.

Consider size of int is 4 bytes, so the y  variable can hold values from -231 to 231-1 (zero, positive and negative) , whereas variable x can hold values from 0 to 232-1 (only zero and positive number).


Derived Data Types In C Language

Data types that are being derived from fundamental data types are called as derived types. For example:

arrays, pointers, function types, structures are considered as derived data types.