Type Here to Get Search Results !

Web Stories

Data Types in C

Dheeraj Patidar

In this tutorial, you will learn about basic data types such as int, float, char etc. in C programming.

Do you know "What are data types in C"?

Let's understand deeply, what data type are actually. There are lot of defination about data types in market but we have to understand from basic level to advance level about C Data types.

Data Types in C

What is Data type in C

The Data types are entity which are used specify which type of data should store in any variable, and much memory will be required to store the data.

Each of the variable in C program is associated with any data type to specify which type of data should store like integer, floating number, character, string, etc.

Data type is nothing but it is a representation of a data. In the declaration of any variable it is mandatory to specify its data type.

Syntax of Data type:- 

data_type variable_name;

Ex.=> int a;

Data type represents two things:-

  • how much memory is required to Complete allocate for variable.
  • second is what type of data is allowed to store in a particular variable.

For example=> In "int a" variable integer data type is allowed and it required 2 or 4 bytes for memory allocation.


Classification of Data Types in C

Dheeraj Patidar


1) Primitive Data Types

Primitive data types are fundamental of any of the programming language. The concept of Derived data types is arrived from primitive data types. 

There are eight primitive data types: char, int, float, double.

1.1) char

Char Data Type is used to store single character in variable. char type of variable acquires 1 byte of memory in memory.

1.2) int

int Data Type is used to store integer value from range  -2,147,483,648 to 2,147,483,647. int acquire 2 or 4 byte of memory.

Types of int Data Type

1.2.1) short int : 2 bytes memory requireds

  • signedshort : -32768 to 32767
  • unsigned short : 0 to 65535

1.2.2) int : 2 or 4 bytes memory required

  • signed int : -2,147,483,648 to 2,147,483,647
  • unsigned int : 0 to 4,294,967,295

1.2.3) long int : 4 bytes memory required

  • signed long int : -9223372036854775808 to 9223372036854775807
  • unsignedlong int : 0 to 18446744073709551615


1.3) float

float Data Type is used to store decimal number with single precision. 

More variation of float:

1.3.1) float : 4 bytes memory required  [1.2E-38 to 3.4E+38]

1.3.2) double: 8 bytes memory required [2.3E-308 to 1.7E+308]. used to store decimal Number with double precision.

1.3.3) long double: 10 bytes memory required [2.3E-308 to 1.7E+308]


1.4) void

The void type specifies that no value is available. It is used in three kinds of situations:

1. Function returns as void

There are various functions in C which do not return any value or you can say they return void. A function with no return value has the return type as void. 
For example, void exit (int status);

2. Function arguments as void

There are various functions in C which do not accept any parameter. A function with no parameter can accept a void. 
For example, int rand(void);

3. Pointers to void

A pointer of type void * represents the address of an object, but not its type. For example, a memory allocation function void *malloc( size_t size); returns a pointer to void which can be casted to any data type.


2) Derived Data Type

2.1) Array

An array is a variable that can store multiple values of the same data type. Array is Comes under derived data type in c.

For example, if you want to store 100 integers, you can create an array for it.

With the help of Array we can store n number of elements in a single variable of same data type.

Syntax:- dataType identity[size of array];
Declaration:- int marks[5];
Initialisation:- int marks[5]={12,23,34,45};


Types of Array

2.1.1) One Dimensional Array: 

The type of array which is having onlyone subscript is called 1-D array. 1-D Array is used to storedata in linear form.

Syntax:- dataType identity[size of array];

Declaration of 1d array: int arr[4];

Initialisation of 1d array: arr[4]={23,45,67,12};

we are initialise 1-D array just like variable. But the main differenceis that we are using curly braces { } for initialising all the elements of array.

2.1.2) Two Dimensional Array:

The type of array which is having 2 subscript is called 2-D array. Two dimensional array is also called matrix or table.

Syntax:

dataType identity[Number of Rows][Number of Columns];

There are two methods of declaring & initialising 2D array:

Method 1:

Declaration=> int arr[2][3];

Initialisation=> arr[2][3]={12,13,14,15,16,17};

In this method we are initialising all the elements of two rows in single line.

Method 2: 

declaration=> int arr[2][3];

initialisation=> arr[2][3]={
{12,13,14};
{15,16,17};
}

In this method we are initialising element of 1st and 2nd row separately using two inner curly braces. One curly brace for 1st row and 2nd curly brace for 2nd row. Both curly braces enclosed with another outer curly braces.

2.1.3) Multi-Dimensional Array

The type of array which is having more than 2 subscript is called M-D array. In C programming, you can create an array of arrays is known as multidimensional arrays.

Syntax: dataType identity[N of 2d array][N of Rows][N of Columns];

Declaration: int arr[2][3][4];

Initialisation: int test[2][3][4] = {
{{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}},
{{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}};

If any array having 3 subscript then the total elements of array is [sub1]*[sub2]*[sub3].

For example: int arr[2][3][4];

This M-d array can holds 2*3*4=24 elements.


2.2) String

string is a one dimensional character array terminated by a null ('\0'). When the compiler encounters a sequence of characters enclosed in the single quotation marks, it appends a null character \0 at the end by default. Null Character (\0) Means terminating String and the ASCII value of Null (\0) character is 0.

Syntax:- char identity[size];
Declaration:- char name[20];

You can initialize strings in number of ways:-
  • char c[] = {'a', 'b', 'c', 'd', '\0'};
  • char c[5] = {'a', 'b', 'c', 'd', '\0'};
  • char c[] = "abcd"; // Value after d is '\0' by default
  • char c[50] = "abcd"; // Value after d is '\0' by default


2.3) Functions

Function is a block of instructions having identity which takes input it as an argument, process that input and produce an output. A function declaration tells the compiler about a function's name, return type, and parameters.

Syntax:
return_type function_identity( parameter list ) {
body of the function
}

Parts of function:-

1) Return type: A function may return a value. Return type means which type of data function will return it must be specified. Some functions perform the desired operations without returning a value. In this case, the return_type is void.

2) Identity: This is the actual name of the function by the help of which function Called from main function.

3) Parameters: Parameters are placeholder for arguments passing from main function. Parameters are optional that is,a function may contain no parameters according to the Condition.

4) Function Body: In the body a function perform its specific task.


2.4) Pointers

pointer is variables that stores the address of another variable. Generally pointers are used to to access the information of a memory location.

Syntax:
data type* variable;
Or
data type *variable;

Declaration: 
int *a;


Type of Pointer :-

1. Typed Pointer: always points to specific type of data. Like integer pointer, double pointer, struct emp pointer, string pointer, array pointer.

2. Untyped Pointer: Can points to any type of data. Also called generic pointer. Only void pointer comes under generic pointer.

Generally in pointer concept we need to take the help of two Operators to perform any task:

1. Address of operator (&): used to returns the address of a perticular variable.
Ex=> 
&i=> 87994,
&j=>87998

2. Pointer Operator (*): used to return the value of address or memory location.
Ex.=>
*(&i) = 72
*(&j) = 87994 //it holds the address of pointer.

Pointer always holds 2 byte memory because it only store address of a variable not value. Address of variables is always unsigned integer type. That's why we are using "%u" format specifier for printing address of avariable.

Pointer size always very on compiler type or computer architecture. If it is 16 bit compiler then the size of int and integer pointer is 2 byte. And if it is is 32 bit compiler then the size of of int and integer pointer is 4 byte.


3) User Defined Data Type

3.1) struct

structure is a user defined data type. Using structure we can Store n number of elements of different data type in a single variable.

For example in Array Concept we can store n number of element but of same data type. Due to this disadvantage of array the concept of structure is introduced.

Syntax:
struct structureName
{
dataType member1;
dataType member2;
……...
……...
};

Declaration:
struct emp
{
char ename[20]; //use of string for storing name
int ecode;
float esalary;
};

we can initialised any structure just like an array. but the main difference b/w initialisation of structure and array is that there is no need to specify the size of structure variable because we are using the member of a structure for storing elements.

 And in the concept of array it is mandatory to specify the size of array because all the elements are of same data type.

For Example:
struct emp e1={"vishal",6253, 30000};
int arr[10]={12,34,56,22};

There are 2 ways of initialising structure:-

1) Direct Initialisation.
2) Input values from End-user using prinf function.


3.2) union

union is a user defined data type like structure. But union members share same memory location for storing and processing the data.

Example=>

Dheeraj Patidar

In above example we found that in structure a and b both members get different memory location but in union a and b share same memory location.

Syntax:
union unionName
{
dataType member1;
dataType member2;
……...
……...
}


Declaration:
union abc
{
int a;
char b;
};


3.3) typedef

  • In C programming, typedef is a keyword which is used to give a name to any dataType in a program. In simple word we can say that typedef keyword is used to create duplicate name or Alias name of any data type.
  • typedef is a user defined data type.
  • With the help of typedef keyword we can give dublicate name to any primitive (int, float,char), derived (array, string) ,
  • and as well as user defined datatype (structure, union).
  • We can use typedef either globally and locally.
  • Syntax: typedef dataType name;
  • Declaration: typedef int myint;

3.4) enum

enum stands for enumeration, is a type of data type that consists of integral constants. To define enums, the enum keyword is used.

Syntax of enum:
enum flag {const1, const2, ..., constN};

By default, const1 is 0, const2 is 1 and so on. You can change default values of enum elements during declaration (if necessary).

// Changing default values of enum constants
enum suit {
    club = 0,
    diamonds = 10,
    hearts = 20,
    spades = 3,
};


Enumerated Type Declaration:
When you define an enum type, the blueprint for the variable is created. Here's how you can create variables of enum types.

enum boolean {false, true};
enum boolean check; // declaring an enum variable

Here, a variable check of the type enum boolean is created.

You can also declare enum variables like this:

enum boolean {false, true} check;


Example of Enumeration in c:

Let's create a simple program of enum.

 

#include <stdio.h> 

enum weekdays{Sunday=1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}; 

int main() 

enum weekdays w; // variable declaration of weekdays type 

w=Monday; // assigning value of Monday to w. 

printf("The value of w is %d",w); 

   return 0

}



Conclusion

This is all about Data Types in  C programming. Here you have only get the brief summary or definations of all C Data Types.

I will publish deep content about each of the data type in C with there examples. You will get individual post on each Data Type in easy & understandable manner.

So keep in touch with me. Follow me on insta, and Facebook page.

Thank for Visit... 

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.