C supports some special operator, sizeof operator, pointer operators (& and *) and member selection operator (. and ->). The comma and sizeof operators are discussed in this section while the pointer operators are discussed in chapter.
Comma operator
The comma operator can be used to link related expressions together. A comma – linked list of expressions are evaluated left to right and the value of right most expression is the value of combined expression. For example, the statement
Value = (x = 10, y=5, x+y);
first assigns the value 10 to x, then assigns 5 to y, and finally assigns 15 (i.e. 10+5) to value. Since comma operator has the lowest precedence of all operators, the parentheses are necessary.
Program - Illustrate the use of comma operators for swapping the contents of two variables
#include <stdio.h>
void main ()
{
int a, b, temp;
b = (a = 10, a +15);
printf (“Before swapping a = %d and b = %d\n”, a, b);
temp = a, a = b, b = temp;
printf (“after swapping a = %d and b = %d\n”, a, b);
}
Output
Before swapping a = 10 and b = 25
After swapping a = 25 and b = 10
Sizeof Operator
The sizeof is a compile time operator and, when used with an operand, it returns the number of bytes the operand occupies. The operand may be a variable, a constant, or a datatype. For example
m = sizeof (sum)
n = size of (long int):
k = sizeof (235L);
The sizeof operator is normally used to determine the lengths of array and structures when their sizes are not known to the programmer. It is also used to allocate memory space dynamically to variables during execution of a program.
Mixed mode operation and automatic (implicit) conversion
When operands of different data types are used in an arithmetic expression, one of the operand data type will be converted to the type of other operand. This conversion is taking place automatically during program execution and is referred to automatic type conversion or implicit conversion.
Cast or explicit conversion
Cast operation is used to overcome automatic conversion. The variable declared in specific data type can be converted into the required type as shown below.
About the Author
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc