C Data Types

A data type in c specifies the type of value that a variable can store for example integer, character, or float. Each data type requires a specific amount of memory.

Data types in C are defined under three categories

  • Primitive or primary
  • Derived
  • User defined

Primitive Data Types –

The primary data types in C are int, char, float, and double. Let us see about them in brief.

  • int: int means ‘integer’. An int variable is used to store an integer value. The size of int is 4 bytes.
  • char: It means character and is used to store character values in C.
  • float: It is used to store decimal point values with single precision.
  • double: It is used to store decimal numbers with double precision.

Integer types –

Integers are whole numbers that can be negative, 0, or positive values, but have no decimal value. Some examples of integers could be -11, 4, and 0.

To declare a variable of type integer, we use the keyword ‘int’.

int age;

Here age is a variable of type int. This means this variable can store an integer value.

We can declare multiple variables in a single statement like this –

int age, marks;

Here we have declared two variables named ‘age’ and ‘marks’ of type int.

The size of int is 4 bytes (or 32 bits) and it can take values ranging from -2147483648 to 2147483647.

char –

To declare a variable of character type, we use the keyword ‘char’. For example

char section = ‘a’;

Here, we have declared a variable named section of type char.

The size of the character variable is 2 bytes.

float –

A float or floating point number is a number containing a decimal. To declare a float variable we use the keyword ‘float’.

float height;

Here we have used a variable named ‘height’ of type float. The size of the float is 4 bytes.

double –

It is used to store decimal numbers with double precision. To declare a variable of type double, we use the keyword ‘double’. The size of a double is 8 bytes or 64 bits.

double a;

Here we have declared a variable named ‘a’ of type double.

Derived data types –

  • Arrays
  • Pointers
  • Function

Array –

The array is one of the most commonly used data types in programming. An array in c is a collection of similar types of values.
Unlike a normal variable (that can only store one value at a time), an array can store multiple values at a time.

Given below is an example of an integer-type array that stores 5 elements at a time.

#include <stdio.h>
int main()
{
    int arr[5] = {12,4,7,5,8};
    //The array given above has five integer values.
    return 0;
}

Every element of an array has a different block number (called an index number) which is used to fetch a particular element from that array. Indexing always starts from 0 and then increases by 1 for every next element. For example in the above case –

the index of 12 is 0.

the index of 4 is 1

the index of 7 is 2

the index of 5 is 3

the index of 8 is 4

Note: The index of the first element is zero and then the index increases by 1. So, if we want to print the last value of the array, then we have got to write-

#include <stdio.h>
int main()
{
    int arr[5] = {12,4,7,5,8};
    printf("%d", arr[4]);
    //writing array name followed by square brackets containing index number fetches that element.
    return 0;
}

Pointers – A pointer is a special type of variable that can hold the address of another variable. A pointer can be declared as-

type *variableName;

Let us see some valid pointer declarations –

  • int *ptr; //Pointer to a integer.
  • float *ptr; //Pointer to a float.
  • double *fptr; //Pointer to a double.
  • char *cptr; //Pointer to a character.
#include <stdio.h>
int main ()
{
    int  x = 7; // actual variable declaration
    int  *p; // pointer variable declaration
    ip = &x;// store address of var in pointer variable
    printf("Address of x: %x\n", &x);
    // address stored in the pointer variable
    printf("Address stored in p variable: %x\n", p);
    //accessing the value of x using the pointer
    printf("Value of *p variable: %d\n", *p );
    return 0;
}

Output:

Address of var variable: bffd8b3c

Address stored in p variable: bffd8b3c

Value of *p variable: 20

Note: Accessing the value through a pointer is called dereferencing. 

Function – A function is a block of code that can be reused whenever required. For example, if you need to perform the calculation of average of two numbers multiple times in your program, you can save yourself from writing the same lines of code again by using a function. You write the code once inside a function and can use that code again and again by calling the function.

#include<stdio.h>
void calculate_average(int x, int y)
{
    var res = (x + y)/2;
    printf("The average of %x and %y is \n", res);
}the 

int main()
{
    calculate_average(4, 12);
    calculate_average(2, 10);
    return 0;
}

Output:

The average of 4 and 12 is 8

The average of 2 and 10 is 6

User-defined datatypes:

  • Union
  • Structure
  • Enum
  • typedef

A union is a special data type available in C that allows the storage of different data types in the same memory location.

#include <stdio.h>
#include <string.h>

union Student
{
    int roll;
    char name[20];
};

int main( )
{
    union Student stud;
    stud.rollno = 1001;
    strcpy(stud.str, "Aman");
    printf( "student Roll No : %d\n", stud.rollno);
    printf( "student Name : %s\n", stud.name);
    return 0;
}

When the above code is compiled and executed, it should print the following output-

Student 1 name : Aman

Student 1 Roll No. : 1001

Student 2 name : Pooja

Student 2 Roll No. : 1002

Enumeration –

Enumeration or ‘enum’ is a user-defined data type that is mainly used to assign names to integral constants. As we humans are more comfortable with names rather than numbers, enums make a program incredibly easy to read and understand.

#include <stdio.h>
enum Decision {Correct = 1, Incorrect = 0};
int main()
{
    printf("%d, %d, Correct, Incorrect);
    return 0;
}

Output:

1, 0

If we don’t assign values to the names of the enum, the compiler will automatically assign values starting from 0 by default.

For Example –

enum Decision {Correct, Incorrect}

Here the name Correct would get a value of 0 and Incorrect would get a value of 1.

Typedef –

Typedef is a keyword to assign a name to an existing datatype.

For example – If we want a variable of type long int, then we need to declare it somehow like this.
long int x;

Now if we define a typedef for long int variables, then declaring the variables of this type would be too easy.

typedef long int long_v;

In the above statement, we have given a temporary name long_v to the long int datatype.

After this statement, we can declare variables of type long int like this –

long_v x, y,z;