Data Types in C Programming

 Data Types in C Programming


What is Data Type?

The data type specifies the type of value that variable can hold or store. What type of value variable is going to store that we have to define before input any value is called data types. e.g.  int, float, char.

Following are the data types in C programming

1. Basic data type

2. Derived data type

3. Enumeration data type

4. Void data type


1. Basic data types in C:

The basic data types are based on integer and floating types values.  Both the signed and unsigned values are allowed in basic data types.


  • Integer(int) datatype is used to store integer data values. The storage capacity is 2 bytes. %d is the format specifier used to read the integer values. 

e.g. int a=100;


  • Short int data type memory capacity is 2 bytes. %u format specifier is used to read short integer data.

  e.g.  short int a=100;


  • Long int data type storage capacity is 4 bytes. %ld format specifier is used to read long integer values.

 For example long int a=100;


  • Float data type is used to store values which contain decimal points. To store fractional values, a float data type is used. Storage capacity of the float data type is 4 bytes. %f format specifier is used to read floating values. 

 e.g.  float a=10.60;


  • Double data type stores the values up to 8 bytes. %if is used to read double data type values. 

e.g.  double a=100000.123;


  • Long double data type helps to store values up to 10 bytes. %Lf format specifier is used to read long double data values.

e.g. double a=100000.123;


  • Char data type is used to store or assign character values in a variable. Storage capacity of character data type is 1 byte. %c format specifier is used to read character data values.

e.g. char name=’A’;

char  color=’r’;


  • String in  C and C plus plus is used with arrays. The storage capacity of string data type can be defined by the programmer. String is enclosed within double quotation marks(“ “ ). %s format specifier is used to read the string values.

char name[20];

char address[50];


2. Derived Data Type in C:

Array, pointers, structure and union are derived data types. 

  • Array: 

Array is a collection of identical data items. The similar types of data values can be stored using arrays.  For example the array can have integer values,  float, char, double values. 

For example 

Int i[];

float  marks[4];

double interest[5];

Here the square bracket is used to define the size of the variable. Size is nothing but storage capacity of that variable. 


  • Pointer:

Pointer is a variable that stores the address of another variable. Pointer variables can store addresses of any type of data type. Pointer is used for dynamic memory allocation in C language. 

For example,

Int *ptr;

Int a=5;

ptr=&a;

Ptr is a pointer variable which holds the address of a variable.


  • Structure: 

Structure is user defined or derived data type which contain heterogenous data items. Structure variables can store different types of data items. For example int, float, char etc. 

For example,

Struct student[20]

{

Char name[20];

Int rollno;

}


  • Union:

Union is a special type of data type in C language. Union can store different data types in the same memory allocation.

For example

union student

{

Char name[20];

Int rollno;

}


3. Enumeration in C:

Enumeration data types help to enhance the readability of code. We can use enums to define enumerated data types. 

For example

enum weekdays;

enum  weekend;


4. Void:

The absence of any other data type in c and C ++ is void data type. In a program when you declare any function with void then this function doesn't return anything. 

For example

1.

void main()

{

}


2. void getdata();

3. void display();

In above examples the main function is declared with void it means it does not return anything to main.

In another example getdata() function is declared with void so it doesn't return anything to function.

But if you declare function like

int getdata();

int display();

In this case the programmer has to return something to function. Return keyword is used to return values to function.






टिप्पणी पोस्ट करा

0 टिप्पण्या