sizeof and Comma Operators in C
C has two special operators that look unusual. sizeof looks like a function but is an operator: it tells you how many bytes something uses. The comma operator lets you write several expressions where only one is expected.
In this lesson you will learn both, including the very useful trick of finding the number of elements in an array with sizeof.
What You Will Learn
- What
sizeofdoes and why it is an operator. - Using
sizeofwith types, variables and arrays. - Finding the number of elements in an array.
- What the comma operator does.
- The difference between the comma operator and a comma separator.
sizeof and Comma Operators in C Explained
Exam definition: The sizeof operator is a unary operator that returns the size of a data type or variable in bytes. The comma operator evaluates expressions from left to right and gives the value of the last expression.
The sizeof operator
sizeof(int): size of a type (brackets are required for a type).sizeof xorsizeof(x): size of a variable (brackets optional).- The result type is
size_t, an unsigned integer. Print it with%zu. - It is usually calculated at compile time, and the expression inside is not evaluated:
sizeof(x++)does not increase x.
Number of elements in an array
sizeof(arr) / sizeof(arr[0]) = total bytes of the array ÷ bytes of one element = number of elements.
The comma operator
(a, b, c)evaluates a, then b, then c, and the result is c.- It has the lowest precedence of all operators, even lower than
=. Sox = 1, 2;stores 1, butx = (1, 2);stores 2. - Most common use: two variables in a
forloop, e.g.for (i = 0, j = 10; i < j; i++, j--). - Commas in a declaration (
int a, b;) or a function call (printf("%d", x)) are separators, not the comma operator.
Memory trick: sizeof = a measuring tape: it measures, it doesn't run anything. Comma operator = a queue: everyone is served in order, but only the last person's answer is kept.
Syntax of sizeof and the Comma Operator
sizeof(type) /* sizeof(double) */
sizeof expression /* sizeof x */
result = (expr1, expr2, expr3); /* result = expr3 */
Example 1: sizeof with Types, Variables and Arrays
#include <stdio.h>
int main(void)
{
int marks[5] = {90, 85, 70, 95, 60};
double price = 99.5;
char name[] = "Riya";
printf("sizeof(int) = %zu\n", sizeof(int));
printf("sizeof price = %zu\n", sizeof price);
printf("sizeof(marks) = %zu\n", sizeof(marks));
printf("elements = %zu\n", sizeof(marks) / sizeof(marks[0]));
printf("sizeof(name) = %zu\n", sizeof(name));
int x = 5;
size_t s = sizeof(x++); /* x++ is NOT executed */
printf("s = %zu, x = %d\n", s, x);
return 0;
}
Output
sizeof(int) = 4 sizeof price = 8 sizeof(marks) = 20 elements = 5 sizeof(name) = 5 s = 4, x = 5
Code Explanation
sizeof(int): an int uses 4 bytes.sizeof price: adoubleuses 8 bytes. No brackets needed for a variable.sizeof(marks): 5 ints × 4 bytes = 20.sizeof(marks) / sizeof(marks[0]): 20 ÷ 4 = 5 elements.sizeof(name): "Riya" has 4 letters plus the hidden'\0'= 5 bytes.sizeof(x++): only the size ofx++is found (4); the increment never runs, so x is still 5.
How These Operators Work
- The compiler already knows the size of every type and variable, so it replaces
sizeof(...)with a number while compiling. Nothing is measured while the program runs (except for variable-length arrays). - The comma operator runs each expression in order, throws away all results except the last, and gives that last value.
- The array-length trick only works where the array was declared. Inside a function that receives the array as a parameter,
sizeofgives the size of a pointer instead.
Example 2: The Comma Operator
#include <stdio.h>
int main(void)
{
int a, b, x, y;
x = (a = 2, b = 3, a + b); /* runs all three, keeps a + b */
printf("x = %d\n", x);
y = 10, 20; /* = runs first: y = 10 */
printf("y = %d\n", y);
y = (10, 20); /* brackets: last value 20 */
printf("y = %d\n", y);
return 0;
}
Output
x = 5 y = 10 y = 20
In y = 10, 20; the assignment has higher precedence than the comma, so it is read as (y = 10), 20;. Compilers often warn that the 20 has no effect.
Important Points to Remember
sizeofis an operator, not a function.- It returns size in bytes as
size_t; print with%zu. - The expression inside
sizeofis not evaluated. - Array length =
sizeof(arr) / sizeof(arr[0]). - The comma operator gives the last value and has the lowest precedence.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
Printing sizeof with %d | Warning; wrong on some systems | Use %zu |
| Using the array-length trick inside a function parameter | Gives pointer size ÷ element size | Pass the length as a separate argument |
Expecting sizeof(i++) to increase i | i is unchanged | Increment separately |
x = 1, 2; expecting 2 | x = 1 | Use brackets: x = (1, 2); |
sizeof int without brackets | Syntax error | sizeof(int) |
sizeof and Comma Operator Practice Questions
- Is
sizeofa function or an operator? - Which format specifier prints the result of
sizeof? - How do you find the number of elements in an array?
- What is the value of x after
x = (5, 10, 15);? - What is
sizeof("Hi")?
Show answers
- An operator (a unary compile-time operator).
%zusizeof(arr) / sizeof(arr[0])- 15 (the last value).
- 3 (H, i and the hidden
'\0').
Coding Exercise
Create an array of 7 temperatures. Use sizeof to print the total bytes, the size of one element, and the number of days stored.
Show solution
#include <stdio.h>
int main(void)
{
float temp[7] = {31.5f, 32.0f, 30.8f, 29.9f, 33.1f, 34.0f, 32.6f};
printf("Total bytes: %zu\n", sizeof(temp));
printf("One element: %zu\n", sizeof(temp[0]));
printf("Days stored: %zu\n", sizeof(temp) / sizeof(temp[0]));
return 0;
}
Total bytes: 28 One element: 4 Days stored: 7
Quick Quiz: sizeof and Comma Operators (MCQ)
-
sizeofreturns size in:A) Bits B) Bytes C) Kilobytes D) Words
-
Value of
sizeof(char):A) 0 B) 1 C) 2 D) 4
-
If
int a[10];, thensizeof(a) / sizeof(a[0])is:A) 4 B) 10 C) 40 D) 1
-
Value of
(3, 7, 1):A) 3 B) 7 C) 1 D) 11
-
Which operator has the lowest precedence in C?
A)
=B)||C) Comma,D)? :
Show answers
- B: bytes.
- B: 1 (always).
- B: 10.
- C: 1 (the last value).
- C: the comma operator.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| sizeof | Unary compile-time operator, size in bytes |
|---|---|
| Result type / format | size_t / %zu |
| sizeof(char) | Always 1 |
| Array length | sizeof(arr) / sizeof(arr[0]) |
| sizeof(x++) | x is not incremented |
| Comma operator | Left to right, gives the last value |
| Lowest precedence | Comma operator |
Frequently asked questions
- Is sizeof a function?
- No. It is a compile-time unary operator; brackets are only needed for type names.
- How do you find the length of an array in C?
sizeof(arr) / sizeof(arr[0]), in the function where the array is declared.- What does the comma operator return?
- The value of the last expression.
- Is the comma in int a, b; the comma operator?
- No. It is a separator.
Remember
- sizeof = measuring tape (doesn't run the code inside).
- Comma = queue, last answer is kept.
Next Topic
Next lesson: Operator Precedence in C. You will learn which operator C calculates first when there are many in one expression.