Artikel Terbaru

Type def

Introduction to C Typedef Keyword


Introduction to C typedef Keyword
C language में typedef एक keyword है। यह keyword किसी data type के existing name को नया नाम देने के लिए use किया जाता है। इस keyword द्वारा आप built in और user defined दोनों तरह के data types को नया नाम दे सकते है। 

इस keyword को मुख्यतः user defined data types जैसे की structure आदि के साथ प्रयोग किया जाता है। जब program में data types के नाम जटिल हो तो इस keyword द्वारा आप उनके सरल नाम define कर सकते है। इस keyword के प्रयोग से code short हो जाता है और program की readability बढ़ती है।

उदाहरण के लिए निचे दिए गए structure को देखिये।


struct Employee

{

    char Name[20];

    int Age;

};


ऊपर दिए structure के variables आप इस प्रकार create कर सकते है।



struct Employee e1;


ऊपर दिए गए declaration में e1 को create करने के लिए struct Employee भी declare किया गया है। आप जब भी structure का variable create करेंगे तो इसी प्रकार करेंगे।



लेकिन आप typedef keyword के प्रयोग से इस declaration का छोटा और आसान word बना सकते है। ऐसा करके आप हर बार उसी word को use कर सकते है।



Syntax of C typedef Keyword 

C language में typedef keyword का general syntax निचे दिया जा रहा है।



typedef-keyword data-type-name new-name;


जैसा की आप ऊपर दिए गए syntax में देख सकते है सबसे पहले typedef keyword declare किया जाता है। उसके बाद उस type का नाम declare किया जाता है जिसे आप change करना चाहते है। आखिर में वह नया नाम लिखा जाता है जिसे आप use करना चाहते है।



Example of C typedef Keyword 

C language में typedef keyword के उपयोग को निचे उदाहरण द्वारा समझाया जा रहा है।



#include<stdio.h>



struct Employee

{

     char Id;

     int Salary;

};



int main()

{

   typedef struct Employee emp;



   emp e1;



   e1.Id=101;

   e1.Salary=10000;



   printf("Employee Id is %d and salary is %d\n",e1.Id,e1.Salary);



   return 0;

}


ऊपर दिए गए उदाहरण में typedef keyword द्वारा struct Employee declaration को emp में convert किया गया है। यह उदाहरण निचे दिया गया output generate करता है।



Employee Id is 101 and salary is 10000

Post a Comment

0 Comments