Thursday, May 2, 2019

DataType in C Language

Data type is a keyword used to identify type of data. Data types are used for storing the input of the program into the main memory (RAM) of the computer by allocating sufficient amount of memory space in the main memory of the computer.
In other words data types are used for representing the input of the user in the main memory (RAM) of the computer.
In general every programming language is containing three categories of data types. They are
  • Fundamental or primitive data types
  • Derived data types
  • User defined data types
datatype images

Primitive data types

These are the data types whose variable can hold maximum one value at a time, in C language it can be achieve by int, float, double, char.

Example

int a;  //  valid
a = 10,20,30;  //  invalid

Derived data types

These data type are derived from fundamental data type. Variables of derived data type allow us to store multiple values of same type in one variable but never allows to store multiple values of different types. These are the data type whose variable can hold more than one value of similar type. In C language it can be achieve by array.

Example

int  a[] = {10,20,30};  // valid
int b[] = {100, 'A', "ABC"};   //  invalid

User defined data types

User defined data types related variables allows us to store multiple values either of same type or different type or both. This is a data type whose variable can hold more than one value of dissimilar type, in C language it is achieved by structure.

Syntax

struct emp
{
int id;
char ename[10];
float sal;
};
In C language, user defined data types can be developed by using struct, union, enum etc.

Data type modifiers in C

In c language Data Type Modifiers are keywords used to change the properties of current properties of data type. Data type modifiers are classified into following types.
  • long
  • short
  • unsigned
  • signed
Modifiers are prefixed with basic data types to modify (either increase or decrease) the amount of storage space allocated to a variable.
For example, storage space for int data type is 4 byte for 32 bit processor. We can increase the range by using long int which is 8 byte. We can decrease the range by using short int which is 2 byte.

long:

This can be used to increased size of the current data type to 2 more bytes, which can be applied on int or double data types. For example int occupy 2 byte of memory if we use long with integer variable then it occupy 4 byte of memory.
datatype Modifiers images

Syntax

long a;  --> by default which represent long int.

short

In general int data type occupies different memory spaces for a different operating system; to allocate fixed memory space short keyword can be used.

Syntax

short int a; --> occupies 2 bytes of memory space in every operating system.

unsigned

This keyword can be used to make the accepting values of a data type is positive data type.

Syntax

unsigned int a =100; // right
unsigned int a=-100; // wrong

Signed

This keyword accepts both negative or positive value and this is default properties or data type modifiers for every data type.

Example

int a=10; // right
int a=-10; // right
signed int a=10; // right
signed int a=-10;  // right
Note: in real time no need to write signed keyword explicitly for any data type.

No comments:

Post a Comment

Why learning C Programming is a must?

C is a procedural programming language. It was initially developed by Dennis Ritchie between 1969 and 1973. It was mainly developed as...