Tuesday, 5 March 2013

The size of Operator



The size of Operator

The operator size of gives the size of the data type or variable in terms of bytes occupied in the memory. The operand may be a variable, a constant or a data type qualifier.

Example

m = sizeof (sum);
n = sizeof (long int);
k = sizeof (235L);


The size of operator is normally used to determine the lengths of arrays and structures when their sizes are not known to the programmer. It is also used to allocate memory space dynamically to variables during the execution of the program.

Example program that employs different kinds of operators. The results of their evaluation are also shown in comparision .

.main() //start of program
{
int a, b, c, d; //declaration of variables
a = 15; b = 10; c = ++a-b; //assign values to variables
printf (“a = %d, b = %d, c = %d\n”, a,b,c); //print the values
d=b++ + a;
printf (“a = %d, b = %d, d = %d\n, a,b,d);
printf (“a / b = %d\n, a / b);
printf (“a %% b = %d\n, a % b);
printf (“a *= b = %d\n, a *= b);
printf (“%d\n, (c > d) ? 1 : 0 );
printf (“%d\n, (c < d) ? 1 : 0 );
}


Notice the way the increment operator ++ works when used in an expression. In the statement c = ++a – b; new value a = 16 is used thus giving value 6 to C. That is a is incremented by 1 before using in expression.
However in the statement d = b++ + a; The old value b = 10 is used in the expression. Here b is incremented after it is used in the expression.

We can print the character % by placing it immediately after another % character in the control string. This is illustrated by the statement.

printf(“a %% b = %d\n”, a%b);

This program also illustrates that the expression

c > d ? 1 : 0

Assumes the value 0 when c is less than d and 1 when c is greater than d.

No comments:

Post a Comment