c sizeof operator, comma operator

C sizeof operator

It is a unary operator and returns the size of the operand in terms of bytes. It returns the size of any kind of operands like constant, variable, or any other data type. The size that sizeof operator returns are totally machine-dependent. For example, if the code is running on a machine of 32 bit then the size of the operand will be different than that of a 64-bit machine.

Example: Understanding sizeof operator
#include<stdio.h>
main()
{
int m = 2;
char n = 'A''
double p = 4.56;
printf("m has size : %d\n", sizeof(m));
printf("n has size : %d\n", sizeof(n));
printf("p has size : %d\n", sizeof(p));
}
Output:
m has size: 4
n has size: 1
p has size: 8

Note: Generally the c sizeof operator is used to writing a portable program, which means if a program is written on one machine that it can be executed in any other machine. If this is not done then the code will not run properly if we write and execute the code in different machines.

Comma Operator

It is also used as a separator as well as writing a compact code. The execution of the expression (Program Statement) in which the comma operator is used is done from left to right. The value and type of the rightmost expression is the final value of the program statement (expression write with the comma operator).

Example: Let u = 2, v = 5, u + v;
Explanation: Initially 2 will be assigned to the variable ‘u’ you. Then 5 will be assigned to the variable ‘v’. Finally, the value of the expression you u + v will be evaluated which becomes the resultant value of the entire program statement (expression).

Example: Let a = (u = 2, v = 5, u + v);
Explanation: Here the execution of the expression written within the parenthesis will be the same as the above example. Then the value of the resultant expression (u + v) will be assigned to the variable ‘a’.

Note 1: Since the comma operator’s preference is lower than the assignment operator’s preference. Hence the parentheses have been used in this example for the proper execution of the program statement.
Note 2: A compact code is written in the given example. If the comma operator was not used then the given program statement could be written in the following way.

u = 2;
v = 5;
a = u + v;

Thus, we had to write a minimum of 3 program statements to write the given program statement.

Leave a Comment

Your email address will not be published. Required fields are marked *