Monday 29 January 2018

C Tokens and Keywords

 C Tokens and Keywords 

               Tokens are the smallest building blocks of any programming language. Variables, Data Types, Constants, Operators, Function names are examples of tokens. A combination of these tokens and whitespaces form a C statement and a combination of statements form a C program. Every statement ends with a semicolon ( ; ) and follows a particular syntax.


Variables and Data Types

Consider the following C statement :

 

Above statement can be divided into tokens ( float, val, =, 7.93 )

float is a keyword which refers to floating point Data type.

         Keywords are predefined identifiers ( names of variables, functions, structures etc. ) that are reserved and have special meaning for a language. They cannot be used as variable, class or function names.
         Variables are reserved memory locations to store values.
         Data Types specify what kind of values a variable can store ( In the above statement, val can store only float type values since its data type is float ).

Based on the Data Type of a variable, Operating System allocates required memory for that variable.

C Data Types can be categorized into 3 classes :-
Built-in Data Type [ int, char, float, double ]
Enumerated Data Type [ enum ]
Void Data Type [ void ]
Derived Data Type [ pointer, array, structure, union ]

Following table lists down the C built-in data types and other attributes.

 

void represents the absence of a data type. Uses of void are as follows :-
1) To specifies return type of a function when it is not returning any value.
2) To indicate empty argument list to a function.
3) To declare generic pointers.
* These points will become clear as we move along.

Data Type Modifiers

        Modifiers can be used to alter the meaning of some built-in data types in C.

Following table explains the use of Type modifiers :- 



sizeof
      
         The memory allocated for a particular data type is platform dependent. To find the exact size of a variable on a platform, we can use sizeof operator.

Following program shows the use of sizeof operator : 
----------------------------------------------------------------------------------------------------------------------------




----------------------------------------------------------------------------------------------------------------------------
Output:

Size of integer = 4
Size of character = 1
Size of float = 4
Size of double = 8
Size of short int = 2
Size of long int = 8

----------------------------------------------------------------------------------------------------------------------------

typedef
      
           typedef is either used to create a new name for an existing data type or create a new data type.

Consider the following declaration :-



Now, float x; can be written as decimal x;

Enumerated Data Type

  
       The keyword enum is used to create enumerated data type which is a way of referring to numbers using names.
Consider the following example :- 

 

We have defined a data type days which can take values in the set { Mon, Tue, Wed }.
Mon, Tue and Wed are enumerators in this case.
Next, we declare a variable d of type days and initialized it with value Wed.
By default, value of the first name is 0, second name is 1 & so on.
Thus, in the above example, Mon has value 0, Tue has value 1 & Wed has value 2. We can override the default values by explicitly assigning values to the enumerators.
For e.g, the following declaration assigns value 7 to Tue :



        Now, Wed will have value 8 because enumerators have values larger by 1 than their predecessor.
Let's look at a simple program which illustrates the use of enum :

-----------------------------------------------------------------------------------------------------------------------------


-----------------------------------------------------------------------------------------------------------------------------
Output:
Value of day : 0
New value of day : 2

----------------------------------------------------------------------------------------------------------------------------

Symbolic Constants
    
          Symbolic Constants refer to fixed values that cannot be altered during the execution of the program.

          If a variable is declared of type const, the value of that variable cannot be modified once defined.

Consider the following declaration :-



         val is a constant and cannot be assigned any other value in the program.  A statement like val = 9; is illegal.We can also declare symbolic constant using #define preprocessor directive.

Following statement declares a constant VAL and assigns a value 13 to it :



' volatile ' Type Qualifier

             We use volatile qualifier when a variable can be modified outside the control of the program. For e.g, a variable which is updated by a system clock. The qualifier volatile prevents compiler from doing any kind of optimizations to the code referring to the variable and ensures that every access to the variable is a memory access. In usual case, variables are stored in cache or CPU register for faster access and the updated variable is later stored in memory. In case of volatile , all updations must be done in the memory itself.

An example of volatile variable is shown below :

 

Reference Variables

          A reference variable provides an alias for a previously defined variable.

Consider the following example :



val is a previously defined variable & num is the reference to the variable val.
Both the variables ( val & num ) refer to the same data object in memory i.e both val and num contains value 17.
The statement
          num = 19;
will change the value of both the variables to 19.
A reference variable must be initialized at the time of declaration and once it is initailized to a data object, it cannot be changed to refer to another object.

Variable Scope
      
            Variables in a C program are categorized as local or global variables. Local variables are declared inside a function and they can be accessed within that function only. They go out of scope ( destroyed ) as soon as the function execution is over. Global variables are declared outside any function. They can be accessed from any location in the program i.e from function and they remain alive as long as the program execution continues. Following program illustrates the use of local and global variables :

-----------------------------------------------------------------------------------------------------------------------------


-----------------------------------------------------------------------------------------------------------------------------
Output:

Value of local variable ( in main ) y = 20
Value of global variable x = 10
Value of local variable ( in func ) z = 30
Value of global variable x = 10

----------------------------------------------------------------------------------------------------------------------------

            We can observe that the global variable x is accessed from the main function as well as the user defined function.

There is one more category of variables in C called environment variables .

No comments:

Post a Comment