Monday, December 24, 2018

C Tokens, Identifiers & Keywords

Introduction to C Tokens  

C language का syntax कई दूसरी popular languages के द्वारा follow किया गया है, इसलिए C का syntax समझना आपके लिए बहुत आवश्यक है। एक C program tokens से मिलकर बना होता है। यदि आप C language के सभी tokens को use करना सिख ले तो आप C language के expert बन जायेंगे।
दाहरण के लिए की नीचे दिए गए program को देखिये। ये program tokens का सही क्रम में use है और इसके सिवा कुछ भी नहीं है।

/* tokenDemo.c */

#include <stdio.h>

int main()
{
   printf("Software programming is cool");
   printf("%d",5);
}

Tokens 6 types के होते है। इनकी list नीचे दी जा रही है।
  • Identifiers   
  • Keywords
  • Constants 
  • Variables
  • Strings
  • Operators 

    यदि इस list के अनुसार आप देखें तो ऊपर दिए गए program में int, main, printf ,+ और Software programming is cool आदि सब कुछ tokens ही है।

    इन सभी tokens के बारे में अभी ना बता कर इस tutorial में आपको सिर्फ identifiers और keywords के बारे में ही बताया जा रहा है। बाकी tokens के बारे में आपको आगे की tutorials में बताया जायेगा।   

    C Character Set 

    C language में आप एक program characters की sequence में लिखते है। जैसे की #include <stdio.h> है। यह  statement characters से ही मिलकर बना हुआ है। इस statement में i,n,c,l और h आदि characters है।

    लेकिन ऐसा नहीं है की आप एक C program में कोई भी character use कर सकते है। इसके लिए C के द्वारा character set define किया गया है। जो characters इस set में है केवल वे ही एक C program में use किये जा सकते है।  

    Alphabet

    Alphabet characters में आप A से Z capital case और a से z small case characters को use कर सकते है।         

    Digits  

    Digits में आप 0 से लेकर 9 तक की कोई भी digit use कर सकते है। 

    Special Characters 

    Special characters में आप नीचे दिए गए characters को use कर सकते है। 

    ! @ # $ % ^ & * ( ) _ - + = | \  { } [ ] < > ", ? /

    इन alphabets, digits और special characters के अलावा आप और कोई भी character C language में use नहीं कर सकते है। इसे ही C का character set कहा जाता है।    

    Identifiers 

    Identifiers वो नाम होते है जो आप variables, constants और functions आदि को देते है। इन नामों को देने के लिए भी कुछ नियम है जो आप follow करते है। यदि आप इन नियमों को follow नहीं करते है तो program में error आ जाती है।  
    1. Identifiers में @, %, - characters नहीं प्रयोग किये जा सकते है।  
    2. C case sensitive language है। इसलिए age और Age दो different identifiers कहलायेंगे।  
    3. Identifiers में आप operators नहीं use कर सकते है। 
    4. Identifier की शुरूआत आप या तो किसी character या underscore से कर सकते है। Digits से identifiers को start नहीं किया जा सकता है। 
    5. आप A से लेकर Z तक के small और capital दोनों तरह के letters या फिर underscore में से किसी के साथ भी identifiers को start कर सकते है। इसके बाद आप digits और दूसरे characters या underscore use कर सकते है। 

    Examples of Identifiers

    Identifiers के सही प्रयोग को समझाने के लिए नीचे कुछ उदाहरण दिए जा रहे है। 

    _age              // सही 

    2name           // गलत 

    Emp-name     // गलत 

    Emp_2_age    // सही    

    Keywords 

    Keywords को predefined tokens या reserved words भी कहते है। C language 32 keywords provide करती है। हर keyword का एक खास मतलब होता है। हर keyword को एक particular task perform करने के लिए define किया गया है।
    इन keywords को अभी explain नहीं किया जा रहा है। इनके बारे में आप धीरे धीरे आगे आने वाली tutorials में जानेंगे।

    auto  
    break  
    case 
    char  
    const 
    continue 
    default 
    do  
    double
    else 
    enum 
    extern 
    float 
    for 
    goto 
    if 
    int 
    long 
    register 
    return 
    short 
    signed 
    sizeof 
    static 
    struct 
    switch 
    typedef 
    union 
    unsigned 
    void 
    volatile 
    while 




    Variables  

    यदि आप किसी data के साथ operations perform करना चाहते है तो इसके लिए सबसे पहले आपको उसे computer की memory में store करना पड़ता है। Computer memory में data को store करने के लिए variables का प्रयोग किया जाता है।
    एक variable memory में किसी location का नाम होता है। यह नाम (या variable) उस memory location को computer की संपूर्ण memory में uniquely identify करने के लिए प्रयोग किया जाता है और इसी नाम के द्वारा आप उस memory location में data store करते है और उस data को पुनः प्राप्त करते है। 
    उदाहरण के लिए ये आपके computer का memory space है। आप computer की memory में कुछ data store करना चाहते है। जैसे की किसी व्यक्ति का नाम, उम्र आदि। Computer की memory में data store करवाने से पहले आप बताते है की आप किस तरह का data store करेंगे।

    यह आप data types के द्वारा define करते है। उसी के अनुसार आपको memory में space मिलता है। यानि की यदि आपने int define किया है तो 2 bytes variable को memory में compiler allot करेगा। 

    इसके बाद आपको उस memory location का नाम define करना होता है। ताकि आप जब भी चाहे उस memory location में store की गई value को इस नाम के द्वारा access कर सके। यही नाम variable कहा जाता है।

    Variables की values changeable होती है। आप एक value को हटाकर दूसरी value डाल सकते है। ऐसा आप compile time पर भी कर सकते है और dynamically (program execution के दौरान) भी कर सकते है।            

    Creating C Variables

    एक variable create करने के लिए सबसे पहले आप data type define करते है। इसके बाद आप एक unique नाम define करते है।
    /* Without value assignment */
    <data-type>  <variable-name>;                  

    /* With value assignment */
    <data-type>  <variable-name> = <variable-value>;         

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

    /* Variable declaration with value assignment */
    int Age = 25;    

    इस statement के द्वारा एक integer variable create किया गया है, जिसका नाम Age है और इस variable को 25 value assign की गई है। आइये अब समझते है की compiler इस statement को किस प्रकार execute करता है।

    जब compiler सबसे पहले int को execute करता है तो वह computer की memory में से 2 bytes की memory allot करता है। इसके बाद जब compiler Age को execute करता है तो वह उस 2 bytes की memory को Age नाम दे देता है। इसके बाद जब compiler = 25 को execute करता है तो वह 25 को इस memory location पर store कर देता है।

    C-variables-in-Hindi

    अब जब भी आप इस value को access करना चाहते है तो Age नाम के द्वारा इसे access कर सकते है।

    Scope of C Variables

    कोई variable पूरे program में किस किस जगह पर use किया जा सकता है। ये उसका scope होता है। Scope के अनुसार variables को 2 categories में divide किया गया है। 

    Local Variables 

    Local variables वो variables होते है जो program के किसी छोटे block में define किये जाते है जैसे की function, control statement block आदि। इस तरह के variables का use सिर्फ उसी block तक ही limited रहता है।
    जैसे की यदि आपने किसी function में कोई variable create किया है तो आप उस variable को उस function के बाहर use नहीं कर सकते है।  

    /* localVariableDemo.c */

    #include<stdio.h>

    void myFunction();

    int main()
    {
         int num=6;
         myFunction();
         printf("Num in main() : %d", num); 

         return 0;
    } 

    void myFunction()
    {
             /* local variable num */
             int num= 5;    
             printf("Num in myFunction : %d\n",num);
    }
      
    Num in myFuntion() : 5
    Num in main() : 6 

    Global Variables 

    Global variables वो variables होते है जिनका scope पूरे program में होता है। इन variables को आप पूरे program में किसी भी access कर सकते है। इन variables को program की शुरुआत में ही define कर दिया जाता है।  

    /* globalVariableDemo.c */

    #include<stdio.h>

    /* A global variable */
    int num=5;  

    void myFunction();

    int main()
    {
        myFunction();
        printf("Num in main() : %d",num); 
        return 0;
    }

    void myFunction()
    {
        printf("Num in myFunction : %d\n",num); 
    }

    Num in myFunction() : 5
    Num in main() : 5 
















    No comments:

    Post a Comment

    C Tokens, Identifiers & Keywords

    Introduction to C Tokens   C language का syntax कई दूसरी popular languages के द्वारा follow किया गया है, इसलिए C का syntax समझना आपके लि...