Data Types in C with examples

Data Types in C with examples



This tutorial gives you detail information about different data types in c with examples. 

Along with a detailed answer of data types, this will also answer the following:

What are datatypes in C?
What are the four basic data types in C?
What are the different data types?
What are the 4 data types?

Let's start learning about data types in C. Basically data types are those which are used for the variables in C programs. It defines which type of data the variables are going to be stored.

In the declaration of every variable, it is mandatory to specify its data type. The declaration of variable includes data type and identifier.

Syntax:
datatypes    identifiers_name;
for example:


int number1;
In the above example datatype is an integer and the variable name is number1 which is an identifier.
Datatype is just a representation of data clearly, how much memory is required to allocate and what type of data is allowed to store. This is the definition of datatypes.
Suppose I am declaring a variable num and it is of integer type so here num gets memory allocation at some location and that location is called the memory address of num.
Generally integer occupies 2 bytes of memory and once the variable is ready, it knows what type of data is allowed inside it.
int num = 15;
Suppose here I am storing 15 in num. so num is an integer type and can store only integer value and the size is 2 bytes so 2 bytes will be reserved in the memory for num and has some address which is called memory address of num.
We cannot store the decimal value inside the int data type. Int data type is only made to allow storing only integer data.
Datatype represents two things about a variable. First one what type of data is allowed to store and secondly how much memory is required to store the data.

We make use of identifiers for declaring variables. for example

int newInteger;
char newCharacter;
float newFloat;

Here, newInteger, newCharacter and newFloat are the variables that are made to store something but exactly what the data those variables are going to store is defined by the data types.

In this scenario, char is a data type in C which tells variables to store character value only. In the same way, int and float tell variables to store integer value and floating (decimal) value.

Classification of Datatypes in C


Data types may be classified into four types. The following are the classification of datatypes in C language. The first one is primitive data types (Basic or fundamental), the second one is user-defined datatypes, third one is derived data types and the last one is Empty set.
  1. Fundamental or Basic data types
  2. User-defined data types
  3. Derived data types
  4. Empty set

Basic or fundamental data types

  1. Char
  2. Int
  3. Float
  4. Double

There are four basic data types in c. Basic data types are also known as primary data types. The size and range of the four basic data types of c are given below:

Data types: char
Size in bytes: 1
Range: -128 to 127

Data types: int
Size in bytes: 2
Range: -32768 to 32767

Data types: float
Size in bytes: 4
Range: 1.2E-38 to 3.4E+38

Data types: double
Size in bytes: 8
Range: 2.3E-308 to 1.7E+308

where E is the notation for powers of 10.  However, the size occupied by data types differ from machine to machine.

By using the keyword long and short, the programmer can make the data types to use double the memory or half the memory occupied bt the data types in c.

With the keyword short, the memory consumption occupied becomes half and with the keyword long, the memory consumption becomes double and likewise, the range of the data type also gets affected.

With the keyword signed and unsigned, the programmer can also make the variable of data type hold positive as well as negative value or only positive value.

With the keyword signed, the variable can hold both positive as well as negative value, and with the keyword unsigned the variable of the data type can hold only positive value. All the declarations are signed in nature by default.

Example: Basic datatypes in c

#include<stdio.h>
void main()
 {
  int sum;           // int data type declaration.
  char name;         // char data type declaration.
  float decimal;     // float data type declaration.
  double nks;        // double data type declaration.
 }

Derived data types

Derived data types are those data types that are derived or made from the basic data types. The data types such as array, pointers, functions, structure, union comes under this category.

Example: Derived datatypes in c

#include<stdio.h>
void main()
 {

  int a[10];      // array of integers.

 }

You will understand derived data types in detail in the upcoming tutorial.

User-defined data types

As the name suggests that these are the data types that are made by the user for the ease of using data types. It is the important data type in c. Type definition data types (typedef) is used to redefine the name of already declared data types.

It simply gives the new name of predefined datatypes but not able to redefine its range or memory consumption.

Example: User-defined datatypes in c

#include<stdio.h>
typedef int YourName;           // typedef data type.
void main()
 {

  YourName n, k, s;

 }


There are three user-defined data types. They are:
  1. Structure 
  2. Union
  3. Enum
You will learn more about these special data types in the upcoming tutorial. Here, you will be given a normal idea of these data types.

Structure

A structure is a competitive data types because variables of a structure can hold many values of different data types. struct keyword is used in a program to declare define structure data type.

Example: Structure in c

#include<stdio.h>
struct X
 {
  int a;
  char b;
 };

void main()
 {
  struct X a1;
 }


Here, a1 will hold both integer as well as char data. If you don't grasp the concept of structure here then no need to worry. You will understand it in detail in the next tutorials.

Union

Union is much more similar to structure except the memory it has allocated is different from the memory allocation of structure. union keyword is used to declare and define the union data types in a program. 

Example: Union in c

#include<stdio.h>
union Y
 {
  int m;
  char k;
 };

void main()
 {
  union Y a1;  // union data type.
 }


enum

enum is a special type of data type in c language. enum stands for enumeration. enum allows the user not only to declare their own data type but also helps in defining the values, its variables are going to hold during the execution of the programs.

enum keyword is used to define the enumeration data type and to declare the enumeration data type.

Example: enum in c

#include<stdio.h>
enum ShowDay { MonDay, TuesDay, WednesDay, ThursDay, FriDay, SaturDay, SunDay };
void main()
 {
  enum ShowDay a
  a = MonDay              // a = 0
  a = TuesDay             // a = 1
  a = WednesDay           // a = 2
  a = ThursDay            // a = 3
  a = FriDay              // a = 4
  a = SaturDay            // a = 5
  a = SunDay              // a = 6
 }

Void data type or Empty set

Void denotes things that do not have the value of any type. void data types in c shows empty type which has no value. It represents no data.



<< Previous                                                                           Next >>
Previous
Next Post »

Comment Below: ConversionConversion EmoticonEmoticon