Data Types in C Language

Preview

Data types in C Language

Data type is the way to specify the type of data and range of values that can be stored in a variable. C supports three types of data types:-

  • Primary Data Types
  • Derived Data Types
  • User Defined Data Types

Primary Data Types

There are four primary data types provided by C as follows:



 Integer Data Type

Different Integer data types are:

Name

 

Size

 

Range

 

short 1 Byte

-27 to 27 –1

(-128 to 127)

signed short 1 Byte

-27 to 27 –1

(-128 to 127)

unsigned short 1 Byte

0 to 28-1

(0 to 255)

int 2 Bytes

-215 to 215 –1

(-32768 to 32767)

signed int 2 Bytes

-215 to 215 –1

(-32768 to 32767)

unsigned int 2 Bytes

0 to 216-1

(0 to 65535)

long 4 Bytes

-231 to 231 –1

(-2,147,483,648 to 2,147,483,647)

signed long 4 Bytes

-231 to 231 –1

(-2,147,483,648 to 2,147,483,647)

unsigned long 4 Bytes

0 to 231 –1

(0 to 4,294,967,295)



Floating point Data Type

Different types of floating point data type are:

 

Data Type

Size

 

Range

 

float 4 Bytes ±3.4X10-38 to ±3.4X 10+38
double 8 Bytes ±1.7X10-308 to ±1.7X 10+308
Long double 10 Bytes ±3.4X10-4932 to ±3.4X 10+4932



 Character Data Type

Character data type can be categorized into following types:

Name

 

Size

 

Range

 

char 1 Byte

-27 to 27 –1

(-128 to 127)

signed char 1 Byte

-27 to 27 –1

(-128 to 127)

unsigned char 1 Byte

0 to 28 –1

(0 to 255)

 

void

void means null or empty. 



Derived Data Types

There are following types of derived data types provided by C Language.

Array

It is collection of elements having same size, type and values which can be stored in it. 

Function

It is named block of statements which can be called anywhere in the same program or another program.

Pointer

It is basically used to store the address of another variable. They  basically provide better and faster execution .

const

const  is a keyword to declare a named constant in C language.

Example: const int a=10;


User Defined Data Types

There are following types of user defined data types provided by C

Structure

It is collection of different types of values. A Structure can contain more than one variable of different data types under one name.

Union

It is also a collection of different  types of values but they are used if we need to use any one of the union members at a time. It always starts with keyword union.

typedef

it is used to give a new name to the existing data type.

Example:

typedef  int distance.

In this example, data type int has been named as distance. We can use the word distance as a data type to declare variables of int type as:

distance one,two;

enum

enum means enumerated data type . It is used to define a list of named constants which contain consecutive values starting from 0 (zero).

Example:

enum WEEK{SUN, MON, TUE, WED,THU,FRI, SAT};

In this example, SUN has been assigned value 0, so FEB will automatically contain 1, and so on. 

Program to demonstrate the use of enum

#include<stdio.h>
int main()
{
enum WEEK{SUN, MON, TUE, WED,THU,FRI, SAT};
WEEK W;
W=TUE;
printf(”W=%d”,W);
printf(”\nSAT=%d”,SAT);
return(0);
}



volatile keyword

This  keyword is used to tell the compiler that the value of the variable may change at any time without any action taken by the code.

volatile int a=10;

Comments

These are messages defined by the programmer inside a program. They are just for the reference . They are not executed by compiler. Comments start with /* (Slash asterisk) and end with */ (Asterisk Slash).

/*
Developer : Lovejot*/
int main() /*Beginning of Program */
{
printf(”Comments are here”);
return(0);
} /*End of Program */

 

 

Best Books of C



Lesson tags: comment in c, const in c, Data types of c, enum in c, enumeriated data type in c.symbolic constant in c, floating point data types in c, in c, integer data types, volatile in c, what is lvalue
Back to: C Programming Language