Skip to content
StudyFame

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 sizeof does and why it is an operator.
  • Using sizeof with 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 x or sizeof(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 =. So x = 1, 2; stores 1, but x = (1, 2); stores 2.
  • Most common use: two variables in a for loop, 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

  1. sizeof(int): an int uses 4 bytes.
  2. sizeof price: a double uses 8 bytes. No brackets needed for a variable.
  3. sizeof(marks): 5 ints × 4 bytes = 20.
  4. sizeof(marks) / sizeof(marks[0]): 20 ÷ 4 = 5 elements.
  5. sizeof(name): "Riya" has 4 letters plus the hidden '\0' = 5 bytes.
  6. sizeof(x++): only the size of x++ 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, sizeof gives 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

  • sizeof is an operator, not a function.
  • It returns size in bytes as size_t; print with %zu.
  • The expression inside sizeof is 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

MistakeWhat happensFix
Printing sizeof with %dWarning; wrong on some systemsUse %zu
Using the array-length trick inside a function parameterGives pointer size ÷ element sizePass the length as a separate argument
Expecting sizeof(i++) to increase ii is unchangedIncrement separately
x = 1, 2; expecting 2x = 1Use brackets: x = (1, 2);
sizeof int without bracketsSyntax errorsizeof(int)

sizeof and Comma Operator Practice Questions

  1. Is sizeof a function or an operator?
  2. Which format specifier prints the result of sizeof?
  3. How do you find the number of elements in an array?
  4. What is the value of x after x = (5, 10, 15);?
  5. What is sizeof("Hi")?
Show answers
  1. An operator (a unary compile-time operator).
  2. %zu
  3. sizeof(arr) / sizeof(arr[0])
  4. 15 (the last value).
  5. 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)

  1. sizeof returns size in:

    A) Bits   B) Bytes   C) Kilobytes   D) Words

  2. Value of sizeof(char):

    A) 0   B) 1   C) 2   D) 4

  3. If int a[10];, then sizeof(a) / sizeof(a[0]) is:

    A) 4   B) 10   C) 40   D) 1

  4. Value of (3, 7, 1):

    A) 3   B) 7   C) 1   D) 11

  5. Which operator has the lowest precedence in C?

    A) =   B) ||   C) Comma ,   D) ? :

Show answers
  1. B: bytes.
  2. B: 1 (always).
  3. B: 10.
  4. C: 1 (the last value).
  5. C: the comma operator.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

sizeofUnary compile-time operator, size in bytes
Result type / formatsize_t / %zu
sizeof(char)Always 1
Array lengthsizeof(arr) / sizeof(arr[0])
sizeof(x++)x is not incremented
Comma operatorLeft to right, gives the last value
Lowest precedenceComma 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.