Polls

How Is My Site?

View Results

Loading ... Loading ...
June 10th, 2008 Code-theory 2 Comments

In c, there are some keywords the usage of which is not very clearly defined (in textbooks). But they are an important part of the language. Here are some I could find ::

Volatile::

The keyword volatile is used to specify that the value of a variable can be changed explicitly by a program even in the same expression where no external or explicit change is made in the value of the variable by the program. This, explicit specification of volatility is important because c automatically assumes a variable on the right side of an expression to have a constant value, and refers to that value if future for the same statement.
Thus

d=a+b+c+a*a+d;

will have same values of ‘a’ throughout by default. This is an implicit optimization of all modern compilers. This can also overcome problems of changing compilers, which differ in evaluation rules for expressions.
This is very helpful for ports, which have values that can be changed by external conditions only.

Static ::

1. Static local variables ::

  1. #include<stdio.h>
  2.  
  3. int ret()
  4. {
  5. static int aa;
  6. return aa++;
  7. }
  8. main()
  9. {
  10. int a;
  11. for(i=0;i<10;i++)
  12. {
  13. printf("%d ",ret());
  14. }
  15. getch();
  16. }

In this code, the initial value of ‘aa’ is retained each call. Thus we get the numbers from 0 to 9 as output, even though the declaration of ‘aa’ takes place along with the function.

2.Static global variables ::

A static global variable limits access for one file only.

  1. #include<stdio.h>
  2. #include<conio.h>
  3. static int num;
  4. int ret()
  5. {
  6. num++;
  7. return num;
  8. }
  9. main()
  10. {
  11. int i;
  12. for(i=0;i<10;i++)
  13. {
  14. printf("%d ",ret());
  15. }
  16. getch();
  17. return 0;
  18. }

The rest of the usage is same more or less within the rules of scope and visibility. New conventions promote the usage of namespaces and deprecate the use of static variables.

2 Responses to “Keywords less used”

  • Keywords less used - CodeCall Programming Forum 11June2008

    [...] which have values that can be changed by external conditions only. Read the full post here :: Keywords less used | TECHARRAZ __________________ God is real… unless declared an integermy blog :: [...]

  • 2 Static » Blog Archive » Volatile:: 21June2008

    [...] Volatile:: Thus we get the numbers from 0 to 9 as output, even though the declaration of ‘aa’ takes place along with the function. 2.Static global variables ::. A static global variable limits access for one file only. #include … [...]

Leave a Reply

 

RSS