C Programming Data Types

In this tutorial, you will learn about data types and how to declare a variable in C programming.

In C programming, variables (memory location) should be declared before it can be used. Similarly, a function also needs to be declared before use.

Data types simply refers to the type and size of data associated with variables and functions.

Data type can be either fundamental (provided in C compiler), or derived (derived from fundamental data types).


Fundamental data types

This section focuses on commonly used fundamental data types.


int

Integers are whole numbers that can have both zero, positive and negative values but no decimal values. Example: 0, -5, 10

We can use int for declaring an integer variable.

int id;

Here, id is a variable of type integer.

You can declare multiple variable at once in C programming. For example:

int id, age;

The size of int is 4 bytes (32 bits) in most compilers. Hence, it can take 232 distinct states: -231,-231+1, ..., -1, 0, 1, 2, ..., 231-2, 231-1

, that is, from -2147483648 to 2147483647.


float and double

float and double are used to hold real numbers.

float salary;
double price;

In C, floating-point numbers can also be represented in exponential. For example:

float normalizationFactor = 22.442e2;

What's the difference between float and double?

The size of float (single precision float data type) is 4 bytes. And the size of double (double precision float data type) is 8 bytes.


char

Keyword char is used for declaring character type variables. For example:

char test = 'h';

The size of character variable is 1 byte.


void

void is an incomplete type. It means "nothing" or "no type". You can think of void as absent.

For example, if a function is not returning anything, its return type should be void.

Note that, you cannot create variables of void type.


bool

Traditionally, there was no boolean type in C. However, C99 defines a standard boolean type under <sdbool.h> header file. A boolean type can take one of two values, either true or false. For example:


#include <stdio.h>
#include <stdbool.h>

int main() {
  bool a = true;

  return 0;
}

If you are a programming newbie, we recommend you to skip the content below for now and go on to the next chapter.


enum

You can create an enumerated type using enum keyword. An enumeration consists of integral constants. For example:

enum suit { club, diamonds, hearts, spades};

Here, a variable suit of enum type is defined. To learn more on how it works, visit: C enums


Complex types

In ISO C99, support for the complex type was standardized.

If you include complex.h header file in your program, you can use complex as a keyword to create and work with complex numbers. For example:


#include <stdio.h>      
#include <complex.h>  

int main() {
    int complex z = 2 + 1 * I;
}

To learn more, visit: How to work with complex numbers in C?


short and long

If you need to use large number, you can use type specifier long. Here's how:

long a;
long long b;
long double c; 	

Here variables a and b can store integer values. And, c can store a floating-point number.

If you are sure, only a small integer ([−32,767, +32,767] range) will be used, you can use short.

short d;

You can always check size of a variable using sizeof() operator.


#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

In C, signed and unsigned are type modifiers. You can alter the data storage of a data type by using them. For example:

unsigned int x;
int y;

Here, the variable x can hold only zero and positive values because we have used unsigned modifier.

Considering the size of int is 4 bytes, variable y can hold values from -231 to 231-1, whereas variable x can hold values from 0 to 232-1.


Derived types

Here's a list of derived types in C. These topics will be discussed in their respective chapter.