Thursday, June 25, 2020

C Language (Data Types)

Data types

 

C programming में variable और function को इस्तेमाल करने से पहले उसे declare करना होता है|Data type, variable और function के type और nature को specify करता है की ये किस प्रकार के data को store कर सकते हैं और कितना मेमोरी occupies करेंगे मतलब की data type किसी भी variable के type को specify करता है की यह variable memory location में कितना space occupy करेगा और यह किस प्रकार के data को store करेगा|

 

Types of Data Type

 

C programming में Data Type निम्नलिखित प्रकार के होते हैं जो की निचे दिए गए हैं लेकिन generally data type दो प्रकार के होते हैं Basic data type और derived data type.

1.     Basic data type

2.     Derived data type

3.     Enumerated Type

4.     Void Data Type

 

Basic Data Type

यह data type primary data type होता हैं जो computer memory में actual data को represent करता हैं मतलब की कौन सा variable किस size का होगा, कितना मेमोरी occupy करेगा, ये specify करते हैं| Basic data type के अंतर्गत integer, floating-point और char type आते हैं|चलिए सभी के बारे में एक एक करके explain करते हैं|

 

Integer (int)

Integer data type whole number को store करने के लिए इस्तेमाल किया जाता है यह zero, negative और positive values को store कर सकता है जैसे की 0, 3, 6, -7, -9, -120. यह data type decimal number (दशमलव संख्या) को store नहीं करता है जैसे की 1.2, -2.3

 Integer variable को declare करने के लिए int keyword का उपयोग किया जाता है| For example: int a;

यहाँ पर a एक variable name है जो की integer type का variable है| आप C programming में multiple variable को declare कर सकते हैं| Multiple variable को separate करने के लिए comma का इस्तेमाल किया जाता है| जैसे:

int a, b, c;

Example:  

#include

int main() {

   printf("Size of int type in bytes : %d \n", sizeof(int));

return 0;

}

जब आप इस program को compile और execute करायेंगे तो यह program आपको आपके operating system के architecture के अनुसार output देगा| मेरा operating system 64-bit architecture का है इसलिए यह 4 bytes answer provide कर रहा है|

Size of int type in bytes : 4

निचे दिए गए table में integer और character type के size और range के बारे में बताया गया है|

Type

Storage Size

Range

char

1 byte

-128 to 127 or 0 to 255

unsigned char

1 byte

0 to 255

signed char

1 byte

-128 to 127

int

2 bytes or 4 bytes

-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647

unsigned int

2 bytes or 4 bytes

0 to 65,535 or 0 to 4,294,967,295

short

2 bytes

-32,768 to 32,767

unsigned short

2 bytes

0 to 65,535

long

4 bytes

-2,147,483,648 to 2,147,483,647

unsigned long

4 bytes

0 to 4,294,967,295

Floating-point types

Floating-point types का उपयोग real number को store करने के लिए किया जाता है जैसे की 5.02, 5.364, -5.012 इस type के अंतर्गत float, double और long double data type आते हैं| Float data type का size 4 bytes होता है, double data type का size 8 bytes होता है जबकि long double data type का size 10 bytes होता है| तीनो types अलग अलग प्रकार के precision को store करके रखते हैं| Precision का मतलब दशमलव के बाद अंक|

निचे दिया गया table floating-point number के type, range, storage size और precision को का details provide करता है|

Type

Storage Size

Range

Precision

float

4 bytes

1.2E-38 to 3.4E+38

6 decimal places

double

8 bytes

2.3E-308 to 1.7E+308

15 decimal places

long double

10 bytes

3.4E-4932 to 1.1E+4932

19 decimal places

 

char

char एक single character को store करने के लिए इस्तेमाल किया जाता है| Single character को single quote के अन्दर लिखा जाता है| character type के variable को define करने के लिए char keyword का उपयोग किया जाता है| Character variable का size 1 byte होता है|जिसका range -128 से 127 या 0 से 255 तक होता है|

declaration of character type variable:

char first =’a’;

Derived data type in C

Derived data type का इस्तेमाल primary data type की मदद के द्वारा किया जाता है इसलिए इसे derived data type कहा जाता है| सभी derived data type के बारे में हम अलग अलग tutorial में सीखेंगे|

Array type

Pointer Type

Structure type

Union type

 

Void Data

void data type किसी भी प्रकार का value return नहीं करता है मतलब की यह no value available को specify करता है|जब हमें function से किसी भी प्रकार का value return नहीं करवाना होता है तो हम उस function का type void specify कर सकते हैं|

एक बात का हमेशा ध्यान रखें की आप void type का variable declare नहीं कर सकते|

 

C - Data Types

C Programming में दो प्रकार के Data Types होते है |

1.      Basic Data Types

2.      Derived Data Types

 

Basic Data Types

Derived Data Types

1.      int (Integer)

2.      char (Character)

3.      float

4.      double

5.      void

1.      Arrays

2.      Pointers

3.      Structure

4.      Unions

5.      Enums (Enumerations)



Basic Data Types

1. int (Integer)

·         Integer Data Type में variable को declare करने के 'int' keyword का इस्तेमाल करते है |

·         Integer Data Type सभी numeric values को store कर सकता है |

·         integer को output में print करना हो तो '%d' format specifier से output में print करते है |

·         Integer Data Type 2, 4 और 8 bytes के हो सकते है |

·         अगर Computer का Processor 16-bit हो तो int का size 2 Bytes होता है |

·         अगर Computer का Processor 32-bit हो तो int का size 4 Bytes होता है |

·         अगर Computer का Processor 64-bit हो तो int का size 8 Bytes होता है |



Integer Data Types, Storage Size and Range

Data Types

Storage Size

Range

int (16-bit)

2 Bytes

-32,768 to 32,767

int (32-bit)

4 Bytes

-2,147,483,648 to 2,147,483,647

int (64-bit)

8 Bytes

-9,223,372,036,854,775,807 to 9,223,372,036,854,775,807

unsigned int (16-bit)

2 Bytes

0 to 65,535

short int

2 Bytes

-32,768 to 32,767

unsigned short int

2 Bytes

0 to 65,535

signed short int

2 Bytes

–32,768 to 32,767

long int

4 Bytes

–2,147,483,647 to 2,147,483,647

unsigned long int

4 Bytes

0 to 4,294,967,295

signed long int

4 Bytes

–2,147,483,648 to 2,147,483,647



Size of इस keyword से int Data Types के size का पता करे |

Source Code :

#include <stdio.h>
 
int main() {
printf("int(16-bit) storage size : %d \n", sizeof(int));
printf("int(32-bit) storage size : %d \n", sizeof(int));
printf("int(64-bit) storage size : %d \n", sizeof(int));
printf("unsigned int storage size : %d \n", sizeof(unsigned int));
printf("short int storage size : %d \n", sizeof(short int));
printf("unsigned short int storage size : %d \n", sizeof(unsigned short int));
printf("signed short int storage size : %d \n", sizeof(signed short int));
printf("long int storage size : %d \n", sizeof(long int));
printf("unsigned long int storage size : %d \n", sizeof(unsigned long int));
printf("signed long int storage size : %d \n", sizeof(signed long int));
return 0;
}

Output

int(16-bit) storage size : 2
int(32-bit) storage size : 4
int(64-bit) storage size : 8
unsigned int storage size : 4
short int storage size : 2
unsigned short int storage size : 2
signed short int storage size : 2
long int storage size : 4
unsigned long int storage size : 4
signed long int storage size : 4


2. char (Character)

·         Character Data Type में variable को declare करने के 'char' keyword का इस्तेमाल करते है |

·         Character को output में print करना हो तो '%c' format specifier से output में print करते है |

·         सिर्फ एक ही character को declare कर सकते है | for eg. 'H'

अगर multiple character मतलब पूरे एक string को print करना हो तो '%s' का इस्तेमाल करते है |

·         Character Data Type 1 byte का होता है | |


for eg.

  #include <stdio.h>
 
  int main() {
  char str1='H'; // declare varible in single quotes
  char str2="H"; // declare varible in double quotes
  char str3[10]='Hello'; // Get Warning Error
  char str4[10]="Hello";
  printf("Single quoted Print Character: %c \n", str1);
  printf("Double quoted Print Character: %c \n", str2);
  printf("Single quoted Print String : %s \n", str3); //Get Warning Error
  printf("Double quoted Print String : %s \n", str4);
 
  return 0;
  }
 

Note : ऊपरवाला Program Warning Error देगा |Program में // Get Warning Error की lines हटाकर नीचेवाला Output दिया है | User single quotes और double quotes के बिच का फर्क जानने के लिए ऊपरवाला Program दिया है |

Output

Single quoted Print Character: H
Double quoted Print Character: $
Double quoted Print String : Hello



Character Data Types, Storage Size and Range

Data Types

Storage Size

Range

char

1 Byte

-128 to 127

unsigned char (16-bit)

1 Byte

0 to 255

signed char

1 Byte

--128 to 127



sizeof इस keyword से char Data Types के size का पता करे |

Source Code :

#include <stdio.h>
 
int main() {
printf("char storage size : %d \n", sizeof(char));
printf("unsigned char storage size : %d \n", sizeof(unsigned char));
printf("signed char storage size : %d \n", sizeof(signed char));
 
return 0;
}

Output

char storage size : 1
unsigned char storage size : 1
signed char storage size : 1


3. float (Floating-point)

·         Floating-point Data Type में variable को declare करने के 'float' keyword का इस्तेमाल करते है |

·         float को output में print करना हो तो '%f' format specifier से output में print करते है |

·         Floating-pont Data Type 4 bytes का होता है | |


for eg.

  #include <stdio.h>
 
  int main() {
  float a=5, b=3.145; // Type Casting convert integer value to print in float value
  printf("Value of Float variable : %f \n", a);
  printf("Value of b : %f \n", b);
  return 0;
  }
 

Output

Value of Float variable : 5.000000
Value of b : 3.145000



Floating-point Data Type, Storage Size and Range

Data Types

Storage Size

Range

Digits of Precision

float

4 Bytes

1.2E-38 to 3.4E+38

6



sizeof इस keyword से char Data Types के size का पता करे |

Source Code :

#include <stdio.h>
 
int main() {
printf("Floating-point Storage size : %d \n", sizeof(float));
return 0;
}
 

Output

Floating-point Storage size : 4


4. double

·         Double Data Type में variable को declare करने के 'double' keyword का इस्तेमाल करते है |

·         integer को output में print करना हो तो '%f', '%e', '%E', '%lf' format specifiers से output में print करते है |

·         Double Data Type 8 bytes का होता है | |

·         Double और Floating-point Data Type में कोई फर्क नहीं है |


for eg.

#include <stdio.h>
 
int main() {
double a=5;
printf("Value of Double variable : %lf \n", a);
printf("Value of Double variable : %e \n", a);
printf("Value of Double variable : %E \n", a);
return 0;
}
 

Output

Value of Double variable : 5.000000
Value of Double variable : 5.000000e+000
Value of Double variable : 5.000000E+000



Double Data Types, Storage Size and Range

Data Types

Storage Size

Range

Digits of Precision

double

8 Bytes

2.3E-308 to 1.7E+308

15

long double

10 Bytes

3.4E-4932 to 1.1E+4932

19



sizeof इस keyword से char Data Types के size का पता करे |

Source Code :

#include <stdio.h>
 
int main() {
printf("Double Storage size : %d \n", sizeof(double));
return 0;
}
 

Output

Double Storage size : 8


5. void

·         void मतलब null value |

·         void में कोई value नहीं होती |

·         ये data type function और function के parameter / argument के लिए इस्तेमाल करते है |

·         ये कोई value return नहीं करता |


for eg.

#include <stdio.h>
 
void hello(void); // function with no return value and parameter
main()
{
hello();
}
void hello(void)
{
printf("Hello World");
}
 

Output

Hello World