Check The Programming Section
The sizeof is a compile-time unary operator that returns the size of a variable or parenthesized type-specifier that it proceeds in bytes. For example, the size of int is 4 bytes and doubles are 8 bytes. Consider the example below:
int k;
printf("%d ",sizeof k))
printf("%d", sizeof (double));
It will display 4 8 as the size of int and double data type.
Note: To compute the size of a data type, the type name needs to be enclosed in parentheses. For example, sizeof(int). This is not necessary for a variable name like sizeof k. But enclosing the variable name by parentheses is no harm, like sizeof(k).
C/C++ defines a special type called size_t using typedef and corresponds to an unsigned integer. The return type of the sizeof operator is size_t. For any other purpose we can consider the type of size_t is the same as unsigned integer value. sizeof can also return the size of an expression after evaluation. Consider the example below:
printf(“%d”, sizeof(4 + 5.5));
In this case sizeof returns 8 as size of value 9.5 (after 4 + 5.5) as double. To calculate the size of a constant it needs to be enclosed inside the parentheses as sizeof(5).
sizeof did not evaluate its operand which prefixed and suffixed with increment or decrement operator. It simply returns the size of the operand. Consider the example given below:
int main(){
int a = 8;
printf(“%d %d”, sizeof(a++), sizeof(++a));
printf(“\n%d”, a);
}
The postfix and prefix operation inside sizeof will not be evaluated. The sizeof operator inside the first printf() statement simply returns the sizeof a. This is the reason the value of the a is remain unchanged.
Comma operators can put multiple expressions together. Consider the example listed below:
int k = (4, 5);
The left side of the comma operator is always evaluated to void. It means, the left side of the comma operator will be evaluated and discarded. The outcome of the comma operator is the outcome of the right expression of the comma operator. In the above example, at first 4 get executed and discarded. Thereafter, the right side expression 5 gets evaluated and assigned to k. So 5 is assigned as the value of k.
The parentheses are necessary when we used comma operator as the right side of the assignment operator. Because comma operator is lower in precedence of the assignment operator. Consider the example,
int a = 3;
a = (a = a+2, a+4);
At first a = a+2 the left part of the assignment operator gets evaluated and value of gets updated to 5 after adding with 2. Thereafter, value of a is added with 4 and returned as the outcome of the comma operator and assigned to a.
In C/C++, . (dot) and -> (arrow) operators are designed for specific purpose to access the members of structure, union in C and to access the menbers of class in C++. Here, we will not discuss much about them, these two operators will be discussed during the discussion of structure and union in C and class in C++.
Parentheses () are operators used to increase the precedence of the operations inside them. Consider the example given below:
int k = 4 * 5 + (12 - 3);
In the above expression the subtract operation 12-3 has been put inside the parentheses and which sets this operation as higher in precedence and will be evaluated first.
Squarebrackest [ ] are used to perform the array indexing. Given an array and pass an expression inside [ ] returns an index into that array. Array indexing is discussed in detailed here.