C - Data Types

C Programming

Share

In this tutorial we will learn about data types in C programming language.

Variables

A variable is a named location to hold a data value.

The value stored in a variable can change and is opposite to a constant whose value never changes.

Example of variable: name, isGameOver, next_page, _nextgame, stage123 etc.

Rules for variable names

Following are the rules for naming a variable.

  • First character must be a letter or underscore
  • Can use letters (A-Z and a-z), digits (0-9) and underscore _
  • Must not be a keyword
  • Must not contain white space

Variable names are case-sensitive meaning hello and Hello are treated as two separate variables.

Data Types in C

Data Types are storage representation and tells the compiler what type of data is stored in the variable.

C divides its data types into 3 categories.

  • Primary
  • Derived
  • User defined

Primary Data Types

C provides 5 primary or fundamental data types namely character char, integer int, floating point float, double precision floating point double and void.

So, if we want to create a variable that will hold a character we use the char data type.

char ch = 'a';

Similarly, if we want to store integer value we use the int data type.

int gameScore = 10;

Range of primary data types

In the following table we have the range of the values of the primary or fundamental data type.

DATA TYPE KEYWORD EQUIVALENT SIZE (bits) RANGE
character char 8 -128 to 127
integer int 16 -32768 to 32767
floating point (6 decimal place) float 32 3.4e-38 to 3.4e+38
double-precision floating point (12 decimal place) double 64 1.7e-308 to 1.7e+308

Extended data type

We can also use the short, long, signed and unsigned keywords to extend the primary data types.

A short data type has a smaller range compared to the long data type.

A signed data type can hold both positive and negative values. But an unsigned data type can only hold positive values.

The following table list different combination of the primary data type along with their value range.

DATA TYPE KEYWORD EQUIVALENT SIZE (bits) RANGE
character char 8 -128 to 127
signed character signed char 8 -128 to 127
unsigned character unsigned char 8 0 to 255
short integer short int or short 8 -128 to 127
signed short integer signed short int 8 -128 to 127
unsigned short integer unsigned short int 8 0 to 255
integer int 16 -32768 to 32767
signed integer signed int 16 -32768 to 32767
unsigned integer unsigned int 16 0 to 65535
long integer long int or long 32 -2147483648 to 2147483647
signed long integer signed long int 32 -2147483648 to 2147483647
unsigned long integer unsigned long int 32 0 to 4294967295
floating point (6 decimal place) float 32 3.4e-38 to 3.4e+38
double-precision floating point (12 decimal place) double 64 1.7e-308 to 1.7e+308
extended double-precision floating point long double 80 3.4e-4932 to 1.1e+4932

Use the data type that correctly matches the data range you are trying to store.

Void

The void data type takes no value and is generally used with functions to denote that the function is not going to return any value.

Declaring variables

When we declare a variable the compiler gets to know two things.

  • Name of the variable
  • Type of data that variable will hold

Example: Create a variable that will hold level of a game.

int level;

Example: Declaring multiple variables

int level;
int score;
int life;

We can merge the three variables having the same data type int in one single line by separating them with comma.

int level, score, life;

Assign value to a variable

We use the = assignment operator to assign value to a variable.

Example: Create a variable to store score of a match and assign 0 to it.

//declaring variable
int level;

//assigning value
level = 0;

We can merge the declaration and assignment step into one.

//declare and assign
int level = 0;

Derived Data Types

These are the data types derived from the primary data type.

Example: Array, Function, Pointer

We will learn about the derived data types in their respective tutorial.

User Defined Data Type

C allows programmers to create user defined types using the typedef and enum.

typedef

We use the typedef keyword to give new identifier name to a exisiting data type.

The general format to use typedef is given below.

typedef type identifier

Where type is any existing data type and identifier is the new name given to it.

Example:

typedef int num;

In the above line we are giving integer int data type a new name num.

In the following example we have used typedef to give int a new name num. And then created a new variable and assigned integer value 10 to it.

#include <stdio.h>
int main(void)
{
  typedef int num;
  num score = 10;
  printf("Score: %d", score);
  return 0;
}

The above code will print the following output.

Score: 10

enum

enum is enumerated data type that we can use to create user defined data type.

Using enum we can create a data type and also define the value that can be assigned to the variable of that type.

In the following example we are creating an enumerated data type color and it can take only 4 values RED, GREEN, BLUE and WHITE. Next we are creating a variable carColor of type color and assigning the value GREEN to it.

enum color {
  RED,
  GREEN,
  BLUE,
  WHITE
};

color carColor = GREEN;

We can combine the enum definition and variable creation as follows

enum color {
  RED,
  GREEN,
  BLUE,
  WHITE
} carColor = GREEN;

By default, the compiler assign integer value to the enum constants starting from 0.

So, in the above color enum the RED constant gets the integer value 0, GREEN is assigned 1 and so on.

The following code will print "Car Color: 1" as output since the enum constant GREEN gets the integer value 1.

#include <stdio.h>
int main(void)
{
  enum color {
    RED,
    GREEN,
    BLUE,
    WHITE
  } carColor = GREEN;
  printf("Car Color: %d\n", carColor);
  return 0;
}

We can assign our own integer value to the enum constants.

When we set value to a enum constant then the successive constants get value that increases by 1.

For example, if we want to set GREEN to 2 then we can write the following.

enum color {
  RED,
  GREEN = 2,
  BLUE,
  WHITE
} carColor = GREEN;

So, RED will be 0 (default as enum constant value starts from 0).

GREEN will be 2 (we have set that value).

BLUE will be 3 and WHITE will be 4.

Share