Data Types in C

May 15, 2021 0 Comments

Data Types in C

Data Types in C, A data type is generally specified when declaring variables, arrays, functions, etc.

In ANSI C, the data types are divided into three categories. They are:

1. Basic data types

2. User-defined data types

3. Derived data types

Basic Data Types:

There are few basic data types in C:

Data Type           size         Format Specifier    Meaning     
char                1 byte        %c                 Character
int                 2 or 4 bytes  %d                 Integer
float               4 bytes       %f                 Single-precision floating point
double              8 bytes       %lf                double-precision floating point
short int           2 bytes       %hd                Short Integer
long int            4 or 8 bytes  %ld                Long Integer
long double         16 bytes      %Lf                Long floating point
unsigned char       1 byte        %c                 unsigned Character
unsigned int        2 or 4 bytes  %u                 unsigned Integer
unsigned short int  2 bytes       %hu                unsigned Short Integer
unsigned long int   8 bytes       %lu                unsigned Long Integer
        

Int: The int data type uses to hold zero, positive, and negative numbers.

int a = 0, b = -5, c = 10;

Char: The char data type uses to declaring Character type variables.

char x = ‘A’;

Float and Double: The float and double uses to hold the real numbers.

float a = 10.00;

double b = 10.2345;

Void: The void data type specifies that no value is available. It uses three kinds of situations.

  1. The function with no return value has the return type is void.
void add(int a, int b);

2. The function arguments as void. A function with no passing arguments.

int func(void);

3. Pointer to void: The function return type void * represents the address of an object, but not its type.

void *malloc(size_t size);

The qualifier signed or unsigned may be applying to char or any integer. The signed data type uses for storing both positive and negative values whereas the unsigned data type uses to store only positive values.

Example:

#include<stdio.h>
int main() {
        int i;
        char c;
        double d;
        float f;
        long long int lli;
        long double ld;
        short int si;
        long int li;

        printf(" %lu ",sizeof(i));
        printf(" %lu ",sizeof(c));
        printf(" %lu ",sizeof(d));
        printf(" %lu ",sizeof(f));
        printf(" %lu ",sizeof(lli));
        printf(" %lu ",sizeof(ld));
        printf(" %lu ",sizeof(si));
        printf(" %lu\n",sizeof(li));
        return 0;
}

User-Defined Data Types:

The User-defined data types can Create two ways

  1. typedef keyword
  2. enum keyword

Using typedef keyword we can create our own data types. You can use typedef declarations to construct shorter or more meaningful names for types already defined by C.

typedef int int_32_t;
typedef char int_8_t;

int_32_t a;
int_8_t c;

enum used to assign names to integral constants, the names make a program easy to read and maintain. By default the values of enum names as start with zero.

enum days {
     mon,
     tue,
     wed,
     thu,
     fri,
     sat,
     sun,
};
enum days day;

Derived Data Types:

These Data types are deriving from fundamental or basic data types. for example, Structures, Union, Pointer, Arrays, Functions.

Operators in C

Share This: