Pages

Const Qualifier and Typedef

The const Qualifier
const एक Keyword है इसको const qualifier भी कहते है। जब किसी Variable को const keyword के साथ declare करते है (Variable से पहले const keyword लगाते है) - इसका मतलब यह की variable की value program के Runtime बदल नही सकते है। अगर इस Value को बदलना चाहेगे तो Compile time Error आयेगा।

The const is a keyword. Also called const qualifier. When a variable’s declaration is qualified (i.e. preceded) with const keyword -  it means that the variable will remain stable to a particular value, given in the declaration. And the value of the variable cannot be changed by the program in runtime. Also, any attempt to change the value of the variable will result into a compile time error.
To understand the concept first consider a normal declaration –

int x = 0;


Here x is set to the value zero. If you want to modify(change) the value of x some where in your program then you can easily do this by using a statement like x = 100 or x = 500. Also you can take the value for x from the keyboard using scanf as –  scanf(“%d”, &x);
Now, qualify (i.e. precede the same declaration by const keyword. As shown here –
                            const int x = 0;
After the const keyword is applied, the variable x is now stable at value zero. Any further attempt to change the value of x will result into a compile time error – cannot modify a const object. That is, you, now, can use x, but can not change the value of x by statements like x = 100 or x = 500 or by any other means such as using increment/decrement operators. Also, you cannot use scanf( ) to take the value for x from keyboard. So, writing scanf(“%d”, &x); is illegal now, because const has been applied to the x. This all means that the variable will be – or behave as – a constant after the const qualifier is applied. Generally, const qualifier is applied to those variables, which you want to use, but don’t want to change throughout the program.      When you use const qualifier, the variable must be given a value. You cannot use the declaration without a value. For example, a statement like –
const int x;
is illegal. Because the variable is not set to a value. It must be like this –
const int x = 0;            (stable at zero)
or
const int x = 100; (stable at 100)
The const keyword can be applied to any type – character, integer, float or double.   By convention, a const qualified variable is declared in UPPER CASE i.e. in capital. So that it can be easily noticed in program and among other variables. For Example –
const double PI = 3.142F;
const int TOTAL = 100;
Here is an example to demonstrate the use of const.


/* Calculating Area and Perimeter of Circle */

#include<stdio.h>
#include<conio.h>

int main(void)
{
   const double PI = 3.142;
   double radius,area,peri;
   clrscr();
   printf("Enter the radius:");
   scanf("%lf",&radius);
   area = PI*radius*radius;
   peri = 2*PI*radius;
   printf("Area of circle = %lf\n",area);
   printf("Perimeter of circle = %lf",peri);
   getch();
   return 0;
}

Output:
Enter the radius:7.0
Area of circle = 153.958000
Perimeter of circle = 43.988000


Type Definition 
C allows you to create an alias (alternative name) for a type. This process is known as Type Definition. The keyword typedef is used when you create an alias for a type. After the alias has been created, you can use the original keyword as well as the alias. The general for of creating the alias is –
typedef type alias;
Here, type is the data type and alias is the alternative name. For example, the following statement –
typedef int integer;                       
will create an alias integer, for the keyword int. Now, you can use both - the original keyword int and the alias integer - in the same sense. For example, now the declarations –
int x;
        and
integer y; 
both are valid. The first creates an integer variable x and the second creates an integer variable y. x is created using the original keyword int and y is created using the alias integer.


Lvalue and Rvalue (Pronounced as “el value” and “are value”)
The term Lvalue stands for Location value and Rvalue for Read value. An lvalue is generally a variable to which a value can be assigned. And rvalue is a constant, variable or expression which can be assigned i.e. given to an lvalue i.e. a variable. As you know you can use only a single variable on the left hand side of an assignment statement. As in the following code –
int x, y, z;
x = 10;
y = 20;
z = 30;
Consider these three assignment statements. x = 10, y = 20, z = 30. Respectively, these three copies the value 10, 20 and 30 to x, y and z. In all assignment statements given here, the LHS of = have a variable. Therefore, x, y and z are lvalues in these assignment statements. Because they can be at the left of =. And the values 10, 20, 30 which can be assigned to these lvalues are called rvalues. Because they are used at the right of =. Also, an rvalue can be a variable from which can be copied. As in x = y; x is lvalue and y is rvalue. An rvalue can be a constant. As in x = 10; 10 is rvalue. An rvalue can be an expression. As in x = y + z; y + z gives the result and then it is copied in x. So y + z is an rvalue. Those functions that return a value can be used as rvalues. For example –
r = sqrt(3.0);
is perfectly valid because the function sqrt( ) returns a value. However, the following statement –
s = clrscr( );
is invalid because clrscr( ) does not return any value. So rvalue will not be available. And compiler will flag the error -   rvalue required.
If you write x = y then it will copy the value of y into x. If you write this in reverse i.e. y = x then it will copy the value of x into y. Because assignment operator = always copies the result of RHS into the variable at LHS. However, the statement x = 10 cannot be written as 10 = x. Because, the value of x cannot be copy to 10. Therefore 10 is not an lvalue. A constant cannot be an lvalue. Similarly, x = y + z cannot be written as y + z = x. Because the value of x cannot be copied over y + z. So, an expression like y + z cannot be an lvalue. When an lvalue is not available compiler flags the error -  lvalue required.





NEXT : Conditional statements
PREVIOUS : Conditional Operator

1 comment: