C Programming Data Types

In my last tutorial on C Hello word program, we learnt how to create a simple C program and went ahead to describe C program structure. 

Today’s tutorial will focus on C programming data type.

C is regarded as a typed language. C compiler must know the type of data that want memory space in order to know the amount of space to allocate to the data.

Failure to specify type of data, your C program will fail to compile and definitely will not run.

There are some programming languages that are not strictly type. In this case, the compiler or interpreter uses type inference to understand the data type your are assigning space to.

So what is Data Type in C programming

In C programming, data type is use by C compiler to allocate data memory space.

Besides allocation of memory space, it also indicate the nature of the variable and the kind of operations the variable can perform.

We will come back to this with a simple example later.

In C programming, we have the following int, char, float and double as the fundamental or primitive data types.

Apart from primitive data type, there is derived data type which is a combination modifiers and primitive data types

There is also user define data type in C programming. 

We will go through each of them for better understanding of how they differ and when to use them.

Primitive Data Type in C

Before we look at each primitive data types, the table below shows the amount of memory space each data type can occupy.

Some examples of the primitive data type is shown below.

#include <stdio.h>
#include <stdlib.h>

int main(){

    //int data type
    int intDataType;
    intDataType = 20;

    //float data type
    float floatDataType;
    floatDataType = 22.2;

    //double data type
    double doubleDataType;
    doubleDataType = 30.40;

    //char data type
    char charDataType[50] = "char type example";

    return 0;
}

Remember that there is a minimum number of bytes a data type can take but this can vary with compilers and operate system.

Data TypeMemoryRangeFormat
char1 byte-128 to +127%hd
int2 or 4 bytes
-2,147,483,648 to 2,147,483,67%d
float4 byte1.2E-38 to 3.4E+38%f
double6 bytes%lf

Data Type Modifiers

C data type modifier is a way to changing the base data type to something different.

When a modifier proceeds a data type in C, it basically increases or decreases to amount of memory allocated to the data type.

C programming language contains the following modifiers – short, long, unsigned, signed. Not all combinations of types and modifiers are available

Below is a table with modifiers in C

Data TypeMemoryRange
unsigned char1 byte0 to 255
signed char1 byte-128 to 127
unsigned int2 or 4 bytes0 to 65,535 or 0 to 4,294,967,295
short2 bytes-32,768 to 32,767
unsigned short2 bytes0 to 65,535
long4 bytes-2,147,483,648 to 2,147,483,647
unsigned long4 bytes0 to 4,294,967,295

Derived Data type

The primitive that types can only hold a single value at a time. When you want to group or store values of the data type then we need to use derived data types.

Derived data type is like a collection of values of the same type. In C programming, this is represent with following types – Pointer types, Array types, etc.

Below is an example of derived data type code representation.

#include <stdio.h>
#include <stdlib.h>

int main(){

    // An array data type
    int carCounter[] = {2, 4, 5, 7, 23, 49};
   
    return 0;
}

User Define Data Type

User define data types are user created data type which can contain similar data types or a combination of different data types.

This usually come in handy when we want to create a data representation with different data types.

Below is an example representation of user define data types with code.

#include <stdio.h>
#include <stdlib.h>

// A struct data type
struct Person
{
    int weight;
    int height;
    char names[100];
};

int main(){
    
    //User define type person
    struct Person person;
   
    return 0;
}

Void Type

Sooner or later you will come in contact with C language void type. Void type is more like a generic or universal type.

You can use void type when you don’t know the data type you want to pass as a parameter to a function or to return a generic pointer.

The void type can be cast to any data type. Below is some of the use cases of void type.

  1. Passing void as function parameter
  2. A function that returns a void
  3. void pointer

In later tutorials, we will go into details on the above usage o

How to get size of a data type

C programming has a library function called sizeof(). This function returns the memory space any data type passed to it will occupied. 

Working with C programming language implies more often than not, you will be allocating memory spaces and freeing memory space when not in used.

With this in mind, you will found usage of sizeof() function very important as your C programming knowledge becomes better. 

Below is a simple C program that shows example usage of sizeof() function.

#include <stdio.h>
#include <stdlib.h>

int main(){

    // Example usage of sizeof function
    printf("Get memory storage size of char %d", sizeof(char));

    return 0;
}

Output: 1 (bytes)

Data Type Qualifiers

We have look at C language data types. We will diverge a bit to briefly discuss data type qualifiers in C.

Lets look at const and volatile data type qualifiers.

Const is a quantifier for a data type variable which adds the variable in a constant region of the memory in turn the value assigned to it cannot be changed.

Volatile quantifier on the other hand is rarely used in C programming but in its usage, it notifies the compiler that this variable can be modify outside the program control.

We will discuss this modifiers in later tutorials.

SUMMARY

We have examine C language data type with some example code. 

Feel free to read more on this topics because it is something that you will use in also every code you will in C.

If you have any questions or suggestions, kindly contact me or drop a message in the comment box.

We are going to C language const in our next tutorial. If your are already, click the button bellow.

Next – const data type modifier

Add a Comment