Data Types in C

In this lesson, you will learn about data types in C, how they are declared, and the primary data types that every beginner C programmer should know.

C Data Types

In programming, data types are used to specify how the variable or functions are declared. The variable’s data type determines what the variable is about( Number, Character, Decimal) and the size allocated for that variable.

In C, we can classify the datatypes into four types:

Data TypesUsage
Common Data TypesInteger (int) floating-point (float), character ( char), string, and void are commonly used datatype in C.
Derived Data TypesDerived Data Types are more complex structures of the data types above, such as arrays, structs, pointer, and union.
Enumeration Data TypesFor Enumerators, which will be discussed in detail later in this course.
Void Data Typesfor void variables declaration

Common Data Types in C

Below are some of the commonly used datatype in C and their characteristics.

  • int: used to store an integer value.
  • float: to store decimal numbers with single precision.
  • double: to store decimal numbers with double precision.
  • char: to store a single character.
  • string: is used to store a string.

Data Types Characteristics

TypeSize (bytes)Value range
int2 or 4 bytes-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
char1 byte-128 to 127 or 0 to 255
float4 bytes1.2E-38 to 3.4E+38
double8 bytes2.3E-308 to 1.7E+308
short int2 bytes-32,768 to 32,767
unsigned int2 or 4 bytes0 to 65,535 or 0 to 4,294,967,295
long int4 or 8 bytes-9223372036854775808 to 9223372036854775807
long long intat least 8 bytes-(2^63) to (2^63)-1
unsigned long intat least 4 bytes0 to 18446744073709551615
unsigned long long intat least 8 bytes
signed char1 byte-128 to 127
unsigned char1 byte0 to 255
long double10 byte3.4E-4932 to 1.1E+4932

Declaring Data type in C

int

int, or integer, is a number that can be zero or value of a positive or negative number. A variable of int data type cannot contain a decimal value.

To declare an int variable, use the int keyword, followed by the variable name

int number;

You can also declare multiple variables of the same data type for better code organization. Example:

int number1, number2;

float

float is used to store decimal numbers with single precision.

float salary;

double

double is another Data Type that is used to store a decimal number. However double can hold a double-precision float data type.

double price;

char

char is used to declare a variable of type character.

char flag= 'y';

short

short is used to store small integers. Please refer to the table above for short Data Type size.

short x;

long

unlike short, a long keyword is used to store large numbers.

long y;

void

void is used when a Type is not available. For example, a function that does not return a specific Data Type uses the void keyword.

void DoSomething()
{
  printf("I'm a void!");
}

bool

bool is used for variables that have values of true or false. Unlike C++, the bool Data Type in C requires the library stdbool.h to be included.

"stdbool.h" 
bool isActive = false;

Enumerated

Enumerated is a user-defined datatype in C. Also know as enum, it’s a set of constants that makes programmers’ life easier, by defining enumerated Data type to be used later in the program. An example of Enumerated Data Type is the Days of the Week enum:

enum Day                  
   {
      Sunday,
      Monday,
      Tuesday,
      Wednesday,
      Thursday,
      Friday,
      Saturday
   };

In this case, the Day Data Type can be used anywhere in the program as needed.

Another example of Enum is the months of a year:

enum Month {
  ;
  Jan,
  Feb,
  Mar,
  Apr,
  May,
  Jun,
  Jul,
  Aug,
  Sep,
  Oct,
  Nov,
  Dec
};

signed and unsigned Variables

signed and unsigned are C modifiers. there are used to change the storage of a datatype from going to positive to negative numbers.

For Example, unsigned int will only hold zero and positive numbers.

int y; // Range from -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int x; // Range from 0 to 65,535 or 0 to 4,294,967,295

Constants in C

a constant, as the name indicates, is used to hold constant value, such as the PI, or the number of months in a year.

For example:

const double PI = 3.14;
const numberOfMonth = 12;

a constant value cannot be changed later in the program. For example, the code below will throw an error

const numberOfMonth = 13;
Back to: Learn C Programming > Overview of C

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.