Data Types in C Language


In C, variables must be declare before it can be use in the program. For this purpose data types are used. Data types are the keywords use to define a variable of a certain type. Data type tells the compiler that what type of value the variable will store and what will be it’s size.

There are two types of data types in C :

1. Fundamental Data Types

  • Integer
  • Float
  • Character

2. Derived Data Types

  • Arrays
  • Structures
  • Pointers
  • Enumeration
  • Union

Fundamental Data Types are built in data types who’s definition are already known by the compiler.

1. Integers – Integer is a number in which there is no decimal point like 5 (a complete number). The keyword int is use to declare a variable of int type. The range of a integer depends upon the compiler. For a 16-bit compiler like Turbo C or Turbo C++ the range is -32768 to 32767. For a 32-bit compiler like Visual Studio or gcc the range would be -2147483648 to +2147483647. Here the 16-bit microprocessor means that when it compiles a C program it generates machine language code that is targeted towards working on a 16-bit machine microprocessor like Intel 8086/8088. And a 32-bit compiler for a 32-bit microprocessor like Intel Pentium. The size of int is also depend on the compiler for a 16-bit compiler, the size is 2 bytes and for 32-bit, the size is 4 bytes.

Syntax : int var_name;

2. Floats – Floats are the number in which there is a decimal point like 5.5 The keyword float is use to declare a variable of float type. A float occupies four bytes in memory and can range from -3.4e38 to +3.4e38. If this is insufficient, then C offers a double data type that occupies 8 bytes in memory and has a range from -1.7e308 to +1.7e308. A variable of type double can be declared as double a;.

Syntax : float var_name;

3. Character – Character is any alphabet like a,b,c. The keyword char is use to declare a variable of char type. The size of char is 1 byte. The range of char is -128 to 127. There also exits unsigned char that has a range from 0 to 255. The maximum length of a character can be only 1 character.

Syntax : char var_name;

►►To get the exact size of a type or a variable on a particular platform, you can use the sizeof operator. The expression sizeof(type) yields the storage size of the object or type in bytes. Following is an example to get the size of int type on any machine.

#include <stdio.h>
#include <limits.h>

int main()
{
   printf("Storage size for int : %d \n", sizeof(int));
   
   return 0;
}

Derived data types are the data type that a user can create. It’s definition is written by the user.

1. Arrays – An array is a collection of similar elements. These similar elements could be all ints, or all floats, or all chars. The declaration of array takes the following form:

Syntax : int arr[20];

where int specifies the type of elements that array will store. arr is the name array and [20] specifies the size of array, it also called index or dimension of array. And brackets [] tells the compiler that we are dealing with array.

2. Structures – A structure contains a number of data types grouped together. These data types may or may not be of the same type. The following is the example to use the structure. The keyword struct is use to define a structure.

Syntax :

struct book

{

      char name[20];

      float price;

      int pages;

}var_name;

3. Pointers – Pointers are the variables that contains addresses as there value. Pointers are use to point any other variable or to access a location. Pointers have many features to offer as to return more than one value from a function, or to access arrays. The declaration takes the following form.

Syntax : int *ptr;

* tells the compiler that we are going to declare a pointer that will point a int variable.

Advertisements

One thought on “Data Types in C Language”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s