Friday, 22 March 2013

Use of some specific keyeword


Here are some important  keywords which are very useful for us.So it is very important to know about them.


  1. auto
  2. enum
  3. register
  4. typedef
  5. extern
  6. union
  7. const
  8. continue and break 
  9. static
  10. __inline
  11. inline
  12. mutable
  13. thread_local
(1)auto:-auto is a keyword used to declare an automatic storage class.It is declared as following types :-

auto  a=2;              //auto
auto  b=3.8;            //float
auto c='h';            //char

 auto  b1=3.8f;          //float 
 auto  b2=3.8F;          //float
 auto  b3=3.8l;         //double
 auto  b4=3.8L;          //long double 
 printf("a=%d\n",a);    //int 
printf("b=%f\n",b);    // float
printf("c =%c\n",c);   //char

(2)enum:-The enum data type data type gives us a chance create our own data types and define what value that data type can take.We can use enum in our progran in following manner

enum day{sun,mon};
enum day d;
d = mon;
printf("%d\n ",sun);
printf("%d\n",mon);

here if we want to initialize d with any other value rather than sun ,mon then it cause an error.If we want to print the value of sun ,mon then it gives 0 and  1 respectively because by default value in enum intilialize with zero.

(3)resister:-resister is also a storage class used to faster access of variable because the storage will be done in CPU registers .It is a hint to the compiler that the variable will be heavily used and that  recommend it will be kept in a processor register if possible.We can declare a datatype register in following manner:-

register int ri=98;

(4)typedef:-typedef keyword provide us to rename a variable datatype into a short and meaningful way.
Ex:-
                typedef unsigned long int ULI;
                 ULI a;

(5)extern:-extern is also a storage class specifier .It is used for the use a variable which is initialized in another file in same project.When we use extern modifier with any variables it is only declaration i.e. memory is not allocated for these variable.We can declare register keyword in following manner:-

extern int ei=23;

(6)union:- union is a data type in C which is similar to structure in declaration and use but different in memory allocation.  

union a
{
char name[4];
int sal;
};
union a detail;
detail.sal=1024;
printf("detail.name[0]=%d\n",detail.name[0]);
printf("detail.name[1]=%d\n",detail.name[1]);
 printf("detail.name[2]=%d\n",detail.name[2]);
printf("detail.name[3]=%d\n",detail.name[3]);
printf("detail.sal=%d\n",detail.sal);

here char name and int sal uses same memory location .Here printf function prints 0,4,0,0,1024 respectively.
because the 1024 stored in following manner for 32-bit  :-


7)const:-The const qulaifier explicitly declares an data object that cannot be changed .Its value is set at initialization.We cannot use a const variable in expression requiring a modifiable lvalue.It can be used with four types:- 
int * const ic=&v4;        //a const pointer to an integer ,the value of integer can be changed.The value of pointer cannot be changed
const int *ci=&v5;        // a pointer to a const integer the value of integer cannot be changed .The value of pointer can  be changed   .

int const *icptr=&v6;       //same as second
const int* const cicptr=&v7;       //It is a const pointer to a const integer so neither  the integer can be changed nor the point to anything else.


8)continue and break:-break keyword is used in that case when in a loop an special condition is satisfied and we want to exit from the loop.For example:-

for(int i=0;i<5;i++)
{
if(i==3)
{
break;
}
printf("%d\n",i);
}
continue keyword is used in that case if we want to take control to the beginning of the loop then it is used.
We can use continue in following manner:-

for(int j=0;j<5;j++)
{
printf("%d\n",j);
if(j==3)
{
continue;
}
printf("%d\n",j);
}

(9)Static:-static is also a storage class specifier used in C/C++.It also stores variable in memory.The default initial value is zero.The value of the variable persists between different function calls.We can use a static datatype  in following manner:-

  

#include <stdio.h>

void func() {
        static int x = 0; // x is initialized only once across three calls of func()
        printf("%d\n", x); // outputs the value of x
        x = x + 1;
}

int main(int argc, char * const argv[]) {
        func(); // prints 0
        func(); // prints 1
        func(); // prints 2
        return 0;
}



(10)__inline:-This function copy a function body whenever we call a funcion. The __inline and __forceinline keywords are available in both C and C++. For compatibility with previous versions, _inline is a synonym for __inline.We can declare a function _inline in following manner:-

__inline void congratulate(int score);


(11)inline:-It has the same woak as __inline .The main difference is that it is only available in C++.The inline keyword used as following manner:-

inline int add(int i, int j) { return i + j; }

(12)mutable:-This keyword can only be applied to non-static and non-const data members of a class. If a data member is declared mutable, then it is legal to assign a value to this data member from a const member function.

mutable member-variable-declaration;

(13)thread_local:-The variable is allocated when the thread begins and deallocated when the thread ends. Each thread has its own instance of the variable. Only variables declared thread_local have this storage duration. thread_local can only be declared for global variables, plus those declared with static or extern.It is declared as following:-


 thread_local unsigned int rage = 1


No comments:

Post a Comment