Please enable Javascript to correctly display the contents on Dot Net Tricks!

Data Types in C language

  Author : Shailendra Chauhan
Posted On : 19 Dec 2012
Total Views : 18,904   
Updated On : 12 Dec 2016
 

Data Types

The data type in C defines the amount of storage allocated to variables ,the values that they can accept ,and the operation that can be performed on those variables.

C is rich in data types. The verity of data type allow the programmer to select appropriate data type to satisfy the need of application as well as the needs of different machine.

There are three classes of Data-Type

  • Primary Data Type

  • Derived Data Type

  • User Defined Data Type

Primary Data Types(Fundamental Data Types)

All C compiler support five type of fundamental data type

  1. Integer int 2,768 to 32,768

  2. Character char -128 to 127

  3. Floating Point float 3.4e-38 to 3.4e+38

  4. Double Precision Floating Point double 1.7e-308 to 1.7e+308

  5. Void Data Type void(used for function when no value is to be return)

Integer Type
Signed
Unsigned
int
unsigned int
short int
unsigned short int
long int
unsigned long int
Character Type
Signed
Unsigned
signed char
unsigned char
Float Type
float
double
long double
Void Type
void
It doesn't return any value

Size and Range of Data-Types on a 16-bit machine

Types
Size
Range
char (or signed char)
8
-128 to 127
unsigned char
8
0 to 255
int (or signed int)
16
-32768 to 32767
unsigned int
16
0 to 65536
short int (or signed short int)
8
-128 to 127
unsigned short int
8
0 to 255
long int (or signed long int)
32
-2,147,483,648 to2,147,483,647
unsigned long int
32
0 to 4,294,967,295
float
32
3.4E - 38 to 3.4E + 38
double
64
1.7E - 308 to 1.7E + 308
long
80
3.4E _ 4932 to 1.1E + 4932

Note

  1. Use sizeof() to know the size of int , char, float etc.

Example program to determine the max and min range of a particular data type.

#include<conio.h>
#include<stdio.h>
void main() 
{ 
 unsigned int i, j; 
 i =1;

o) {    j = i;    i ++; } printf("Maximum value of unsigned int is = %u", j); printf("Minimum value of unsigned int is = %u", i); }

Note

  1. Do not use increment(++) or decrement(--) operator with floating points variable.

A simple program using different data types

#include<conio.h>
#include<stdio.h>
void main()
{
/*………Declaration part…………*/
char c;
int x, y;
float f1, f2;
Double d1, d2;
unsigned p;
/*…………Assigning while declaring………*/
int a = 4321;
long int l = 5432167;
/*……………Assignment Part ………………*/
c = 'A';
x = 867;
f1 = 4.3214;
d1 = 8.5467342;
f2 = 20.000;
d2 = 3.0;
/*………Displaying Values……………………*/
printf("c = %c \n", c);
printf("x = %d and y = %d \n", x, y);
printf("l = %ld \n", l);
printf("d1 = %07lf \n", d1);
printf("p = %u \n", p);
}

User define Data type

By using a feature known as "type definition" that allows user to define an identifier that would represent a data type using an existing data type.

Note

  1. General form:     typedef  type  identifier;

  2. or (to better understand)

  3. typedef existing_data_type new_user_define_data_type;

Example Using user defined data type

 typedef int number;
typedef long big_ number;
typedef float decimal;
typedef double big_decimal;
/********* now we can use above user defined types to declare variables********/
number visitors = 25;
big_number population = 12500000;
decimal radius = 3.5;
big_decimal pie= 3.1415926535

Advantage

  1. main advantage of typedef is that we can create meaningful data type names for increasing the readability of the program.

Derived Data Type

Those data types which are derived from fundamental data types are called derived data types.

There are basically three derived data types .

  1. Array: A finit collection of data of same types or homogenous data type.

  2. String: An array of character type.

  3. Structure: A collection of related variables of the same or different data types.

note: Details of Array, String and Structure is available separately in this site.

Examples of derived data types.

 /*...........Array...........*/
int roll_ no[40]; //Array to contain roll number of 40 students
/*...........String...........*/
char name[20] = "Deepak Kumar"; //string of MAX 20 chars
/*........... Structure...........*/
struct employee
{
  char name[20];
  int age;
  float salary;
};    // Collection of different data types
struct employee emp = {"Shakshi", 28, 450000.00};
What do you think?

By this article I have tried to explain different types of data used and their ranges in C language. I hope this article will be beneficial for you. Any comment and criticize will be welcome.



ABOUT AUTHOR

Shailendra Chauhan
Author, Architect, Corporate Trainer and Microsoft MVP

He is the author of some of most popular e-books which encompass technical Interview on Node.js Interview Questions and Answers, ASP.NET MVC Interview Questions and Answers, AngularJS Interview Questions and Answers and LINQ Interview Questions and AnswersKnow more...

Free Interview Books
 
COMMENTS (0)
6 JUN
Hadoop Development cum Administration (Online)

7:00 AM-9:00AM IST

Know More
30 MAY
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

Know More
30 MAY
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

28 MAY
ASP.NET Core with Angular4 Development (Classroom)

10:30 AM-11:30 AM Weekend

27 MAY
ASP.NET MVC with Angular4 Development (Classroom)

4:00 PM-5:30 PM Weekend

20 MAY
Angular2 and Angular4 Development (Online)

08:00 AM-10:00 AM IST(+5.30 GMT) Weekend

20 MAY
NodeJS with Angular4 Development (Classroom)

5:00 PM-6:30 PM Weekend

8 MAY
ASP.NET MVC with Angular4 Development (Online)

07:00 AM-09:00 AM IST(+5:30 GMT) M/W/F

2 MAY
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST( TUS, THR)

29 APR
ASP.NET MVC with Angular2 Development (Classroom)

9:30 AM-11:00 AM

22 APR
MEAN Stack 2 Development (Classroom)

11:00 AM-12:30 PM Weekend

10 APR
Master Class ASP.NET MVC 5 with Angular2 Plus Angular4 Development (Online)

09:00 PM - 11:00 PM IST (M/W/F)

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+