🌐
W3Schools
w3schools.com › c › c_data_types_sizeof.php
C sizeof operator
Note that we use the %zu format ... the sizeof operator to return a value of type size_t, which is an unsigned integer type. On some computers it might work with %d, but it is safer and more portable to use %zu, which is specifically designed for printing size_t values. Knowing the size of data types helps you understand how much memory your program ...
🌐
GeeksforGeeks
geeksforgeeks.org › c language › sizeof-operator-c
sizeof operator in C - GeeksforGeeks
sizeof() operator is a very useful tool that helps programmers understand how much memory a variable or data type occupies in the computer's memory.
Published   October 17, 2025
Discussions

c programming, How can i get the sizeof a variable through a function using a pointer as a parameter? - Stack Overflow
The compiler can't know where and to what kind of data a pointer might be pointing, so the sizeof of a pointer will always be the size of the pointer itself. Use strlen to get the length of a null-terminated string. More on stackoverflow.com
🌐 stackoverflow.com
How sizeof works in C/C++
The nittiest of all nitpicks: According to the C Specification ISO/IEC 9899:TC3 Chapter 5.2.4.2.1 (the first one I could find on google) it is 1 byte (8 bits). A (C) byte is at least 8 bits, not necessarily exactly 8 bits. Of course, if you can give me a platform that's actually in common use today that doesn't have 8-bit bytes, I'll eat... well, I'll eat my lunch, which I'm about to do anyway. But I'll do it while surprised. :-) So here is the takeaway, since the space needed for a fixed size array (like arr1) is calculated at compile time, the compiler knows about it and by proxysizeof knows about it. All that the compiler knows about arrays allocated at runtime (likearr2) is that it is a int*, which in this case is 8bytes. Incidentally, post also dances around the fact that this example definitively establishes that "arrays are just pointers" is not true, and is a somewhat common misconception about C and C++. Arrays decay to pointers if you look at them funny, but that doesn't make them the same, and sometimes the differences can rear their head. Compare const char * s = "foo"; vs const char s[] = "foo"; for example (in the context of a global/static variable). (That will show up differently in other cases too.) More on reddit.com
🌐 r/programming
30
53
June 13, 2017
c - sizeof style: sizeof(type) or sizeof variable? - Software Engineering Stack Exchange
To mitigate such problems it's ... naked sizeof. See my CLEAR macro that is used exclusively instead of naked memset. Saved my ass several times in cases of indirection typo or when a struct content picked up a vector or string... ... Macros such as that are what gave C's Macro programming a bad ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
June 11, 2013
How do I determine the size of my array in C? - Stack Overflow
It should be the case with all ... of sizeof is defined as a compile-time constant. 2013-09-22T05:39:13.037Z+00:00 ... Important: Don't stop reading here, read the next answer! This only works for arrays on the stack, e.g. if you're using malloc() or accessing a function parameter, you're out of luck. See below. 2014-01-27T14:21:12.827Z+00:00 ... For Windows API programming in C or C++, ... More on stackoverflow.com
🌐 stackoverflow.com
unary operator
sizeof is a unary operator in the C and C++ programming languages that evaluates to the storage size of an expression or a data type, measured in units sized as char. Consequently, … Wikipedia
🌐
Wikipedia
en.wikipedia.org › wiki › Sizeof
sizeof - Wikipedia
January 11, 2026 - sizeof is a unary operator in the C and C++ programming languages that evaluates to the storage size of an expression or a data type, measured in units sized as char. Consequently, the expression sizeof(char) evaluates to 1. The number of bits of type char is specified by the preprocessor macro ...
🌐
Cppreference
en.cppreference.com › w › cpp › language › sizeof.html
sizeof operator - cppreference.com
December 29, 2024 - When applied to a class type, the result is the number of bytes occupied by a complete object of that class, including any additional padding required to place such object in an array. The number of bytes occupied by a potentially-overlapping subobject may be less than the size of that object. The result of sizeof is always nonzero, even if applied to an empty class type.
Top answer
1 of 4
1

You can't just ask for sizeof(pointer) since you'll get the size of the pointer itself. Instead of that- you can change the function to get the size as parameter:

readEachChar(unsigned char * input, unsigned size)

and send the size from main:readEachChar(&text, sizeof(text));

Another solution is to run over the char until you reach the null - '\0' at the end and count the characters within it- that's what the strlen function does.

2 of 4
0

In the line

printf("The the size of the string %zu", sizeof(input));

the sizeof operator will give you the size of the pointer input. It will not give you the size of the object that it is pointing to (which may be the size of the array or the length of the string).

In the function main, the definition

unsigned char text[] = "thisisalongkindofstring";

will make text an array of 24 characters: These 24 characters consist of the 23 actual characters and an extra character for the null terminating character. In C, a string is, by definition, a sequence of characters that is terminated by a null character, i.e. a character with the character code 0.

Therefore, in order to determine the length of the string, you must count every character of the string, until you encounter the terminating null character. You can either do this yourself, or you can use the function strlen which is provided by the C standard library.

Also, it is normal to use the data type char for individual characters of a string, not unsigned char. All string handling functions of the C standard library expect parameters of type char *, not unsigned char *, so, depending on your compiler, mixing these data types could give you warnings or even errors.

Another issue is that this line is wrong:

readEachChar(&text);

The function readEachChar seems to expect the function argument to be a pointer to the first character of the string, not a pointer to an entire array. Therefore, you should write &text[0] instead of &text. You can also simply write text, as this expression will automatically decay to &text[0].

After applying all of the fixes mentioned above, your code should look like this:

#include <stdio.h>
#include <string.h>

void readEachChar( char * input )
{
    size_t len = strlen( input );

    printf( "The size of the string: %zu\n", len );

    for ( size_t i = 0; i < len; i++ )
    {
        printf( "[%x]", input[i] );
    }

    printf("\n");  
}

int main()
{
    char text[] = "thisisalongkindofstring";

    readEachChar( text );

    return 0;
}

This program has the following output:

The size of the string: 23
[74][68][69][73][69][73][61][6c][6f][6e][67][6b][69][6e][64][6f][66][73][74][72][69][6e][67]
🌐
Reddit
reddit.com › r/programming › how sizeof works in c/c++
r/programming on Reddit: How sizeof works in C/C++
June 13, 2017 - So sizeof 42 is valid, and yields the size in bytes of type int. (You can write that as sizeof (42) if you prefer, but the parentheses are part of the operand expression, not part of the syntax of sizeof.) It's very common for the operand expression to be the name of an object, but it can be just about anything.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_sizeof_operator.htm
C - The sizeof Operator
The sizeof operator is a compile−time unary operator. It is used to compute the size of its operand, which may be a data type or a variable. It returns the size in number of bytes.
🌐
Codecademy
codecademy.com › docs › operators › sizeof
C | Operators | sizeof | Codecademy
June 13, 2025 - sizeof() is a compile-time operator returning bytes, while size() is a runtime container method returning element count. When applied to a pointer, sizeof() returns the pointer size, not the allocated memory size.
Top answer
1 of 16
1754

Executive summary:

int a[17];
size_t n = sizeof(a)/sizeof(a[0]);

Full answer:

To determine the size of your array in bytes, you can use the sizeof operator:

int a[17];
size_t n = sizeof(a);

On my computer, ints are 4 bytes long, so n is 68.

To determine the number of elements in the array, we can divide the total size of the array by the size of the array element. You could do this with the type, like this:

int a[17];
size_t n = sizeof(a) / sizeof(int);

and get the proper answer (68 / 4 = 17), but if the type of a changed you would have a nasty bug if you forgot to change the sizeof(int) as well.

So the preferred divisor is sizeof(a[0]) or the equivalent sizeof(*a), the size of the first element of the array.

int a[17];
size_t n = sizeof(a) / sizeof(a[0]);

Another advantage is that you can now easily parameterize the array name in a macro and get:

#define NELEMS(x)  (sizeof(x) / sizeof((x)[0]))

int a[17];
size_t n = NELEMS(a);
2 of 16
1120

The sizeof way is the right way iff you are dealing with arrays not received as parameters. An array sent as a parameter to a function is treated as a pointer, so sizeof will return the pointer's size, instead of the array's.

Thus, inside functions this method does not work. Instead, always pass an additional parameter size_t size indicating the number of elements in the array.

Test:

#include <stdio.h>
#include <stdlib.h>

void printSizeOf(int intArray[]);
void printLength(int intArray[]);

int main(int argc, char* argv[])
{
    int array[] = { 0, 1, 2, 3, 4, 5, 6 };

    printf("sizeof of array: %d\n", (int) sizeof(array));
    printSizeOf(array);

    printf("Length of array: %d\n", (int)( sizeof(array) / sizeof(array[0]) ));
    printLength(array);
}

void printSizeOf(int intArray[])
{
    printf("sizeof of parameter: %d\n", (int) sizeof(intArray));
}

void printLength(int intArray[])
{
    printf("Length of parameter: %d\n", (int)( sizeof(intArray) / sizeof(intArray[0]) ));
}

Output (in a 64-bit Linux OS):

sizeof of array: 28
sizeof of parameter: 8
Length of array: 7
Length of parameter: 2

Output (in a 32-bit windows OS):

sizeof of array: 28
sizeof of parameter: 4
Length of array: 7
Length of parameter: 1
🌐
Educative
educative.io › answers › what-is-the-sizeof-function-in-c
What is the sizeof() function in C?
The sizeof()function in C is a built-in function that is used to calculate the size (in bytes)that a data type occupies in ​the computer’s memory.
🌐
Scaler
scaler.com › home › topics › sizeof() in c
sizeof() in C - Scaler Topics
January 12, 2024 - In the above example, we utilize the sizeof() operator, which is applied to an int data typecast. We use the malloc() method to allocate memory dynamically and return the pointer referring to that memory. The memory space is then multiplied by 10, where the memory space originally represented the number of bytes held by the int data type. Now the catch over here is that the output might alter depending on the computer, for example, a 32-bit operating system will produce different results, whereas a 64-bit operating system would produce different results for the same data types.
🌐
Cprogramming
cboard.cprogramming.com › cplusplus-programming › 177977-sizeof.html
sizeof()
August 20, 2019 - "Finding the smallest program that demonstrates the error" is a powerful debugging tool. Look up a C++ Reference and learn How To Ask Questions The Smart Way ... I tried a few examples last night and realised that sizeof() behaves differently compared to what I was expecting.
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-language › sizeof-operator-c
sizeof Operator (C) | Microsoft Learn
August 3, 2021 - The result is an unsigned integral constant. The standard header STDDEF.H defines this type as size_t. When you apply the sizeof operator to an array identifier, the result is the size of the entire array rather than the size of the pointer represented by the array identifier. When you apply the sizeof operator to a structure or union type name, or to an identifier of structure or union type, the result is the number of bytes in the structure or union, including internal and trailing padding.
🌐
Quora
quora.com › What-is-the-sizeof-operator-in-C-and-what-does-it-do
What is the sizeof() operator in C and what does it do? - Quora
Answer (1 of 3): First, as the question notes, sizeof is an operator (specifically a unary compile-time operator), not a function and it does not require parenthesis surrounding the object that is its single argument. The only correct reason to use parenthesis around the argument to sizeof is if ...
🌐
Unstop
unstop.com › home › blog › the sizeof() operator in c with detailed code examples
The Sizeof() Operator In C With Detailed Code Examples
March 19, 2024 - The sizeof() operator in C is a compile-time unary operator that determines the size of an operand (data type, variable, expression, or pointer). This operator has multiple applications and advantages.
🌐
Programiz
programiz.com › c-programming › examples › sizeof-operator-example
C Program to Find the Size of int, float, double and char
#include<stdio.h> int main() { int intType; float floatType; double doubleType; char charType; // sizeof evaluates the size of a variable printf("Size of int: %zu bytes\n", sizeof(intType)); printf("Size of float: %zu bytes\n", sizeof(floatType)); printf("Size of double: %zu bytes\n", sizeof(doubleType)); printf("Size of char: %zu byte\n", sizeof(charType)); return 0; } ... In this program, 4 variables intType, floatType, doubleType and charType are declared.
Top answer
1 of 4
12

1. My previous knowledge suggested that it is a compile-time operator, which I never questioned, because I never abused sizeof too much…

C 2018 6.5.3.4 2 specifies the behavior of sizeof and says:

… If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

In your example with sizeof(int[size]), the type of int[size] is a variable length array type, so the operand is evaluated1, effectively computing the size during program execution.

In your example with sizeof(*p), the type of *p is not a variable length array type, so the operand is not evaluated. The fact that p may point to an object of automatic storage duration that is created during program execution is irrelevant; the type of *p is known during compilation, so *p is not evaluated, and the result of sizeof is an integer constant.

2. Does sizeof return a value of type int, is it a size_t (ULL on my platform), or is it implementation-defined.

C 2018 6.5.3.4 5 says “The value of the result of both operators [sizeof and _Alignof] is implementation-defined, and its type (an unsigned integer type) is size_t, defined in <stddef.h> (and other headers).”

3. This article states that "The operand to sizeof cannot be a type-cast", which is incorrect. Type-casting has the same precedence as the sizeof operator, meaning in a situation where both are used, they are simply evaluated right to left. sizeof(int) * p probably does not work, because if the operand is a type in braces, this is handled first, but sizeof((int)*p) works just fine.

The article means the operand cannot directly be a cast-expression (C 2018 6.5.4) in the form ( type-name ) cast-expression, due to how the formal grammar of C is structured. Formally, an expression operand to sizeof is a unary-expression (6.5.3) in the grammar, and a unary-expression can, through a chain of grammar productions, be a cast-expression inside parentheses.

Footnote

1 We often think of a type-name (a specification of a type, such as int [size]) as more of a passive declaration than an executable statement or expression, but C 2018 6.8 4 tells us “There is also an implicit full expression in which the non-constant size expressions for a variably modified type are evaluated…”

2 of 4
8

The semantics of sizeof() per the (draft) C11 standard:

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

Note "If the type of the operand is a variable length array type, the operand is evaluated". The means that the size of a VLA is computed at run time.

"otherwise, the operand is not evaluated and the result is an integer constant" means the result is evaluated at compile time.

The return type is size_t. Full stop:

The value of the result of both operators (sizeof() and _Alignof()) is implementation-defined, and its type (an unsigned integer type) is size_t, defined in <stddef.h> (and other headers).

Note that the type is size_t. Don't use unsigned long nor unsigned long long nor anything else. Always use size_t.

Top answer
1 of 1
2307
"Explanation: sizeof Operator in C Definition: The sizeof operator in C is a compile-time unary operator that is used to determine the size, in bytes, of a data type or variable. It is an essential tool for dynamic memory allocation, buffer sizing, and understanding the memory layout of data structures in C programming. Usage: The sizeof operator can be applied to any data type, including primitive types (such as int, float, char), composite types (such as arrays, structs, unions), and even pointers. It returns the size in bytes as an unsigned integer of type size_t. Syntax: sizeof(expression) sizeof(type) Example: #include int main() { int a; double b; char c; printf(Size of int: %zu bytesn, sizeof(a)); printf(Size of double: %zu bytesn, sizeof(b)); printf(Size of char: %zu bytesn, sizeof(c)); return 0; } In this example, the sizeof operator is used to determine the size of different data types. The output will be the number of bytes allocated to each data type on the specific platform where the code is executed. Advantages: Provides a way to write portable code, as the size of data types can vary between different systems and compilers. Helps in dynamic memory allocation by informing how much memory to allocate for different data structures. Aids in buffer management by ensuring that enough memory is allocated to store data. Common Use Cases: Array Size Calculation: When declaring an array, the sizeof operator can be used to determine the total size of the array in bytes and the size of each element, helping to determine the number of elements in the array. Dynamic Memory Allocation: When using functions like malloc() for dynamic memory allocation, sizeof ensures that the correct amount of memory is allocated. Structure Padding: Understanding the size of structures, including any padding added by the compiler, can be critical for optimizing memory usage and ensuring proper alignment. Correct Option Analysis: The correct option is: Option 3: To determine the size of a data type or variable in bytes. This option accurately describes the primary purpose of the sizeof operator in C. It is used to ascertain the size, in bytes, of any given data type or variable, which is essential for various programming tasks such as dynamic memory allocation and buffer management. Additional Information To further understand the analysis, let’s evaluate the other options: Option 1: To allocate memory at compile time This option is incorrect because the sizeof operator does not allocate memory. Instead, it merely returns the size of a type or variable. Memory allocation at compile time is typically handled by the compiler when it translates the source code into machine code, not by the sizeof operator. Option 2: To allocate memory dynamically This option is also incorrect. While the sizeof operator is often used in conjunction with dynamic memory allocation functions like malloc(), it itself does not perform memory allocation. It provides the size information needed to allocate the appropriate amount of memory. Option 4: To determine the size of a data type or variable in bits This option is misleading. The sizeof operator returns the size in bytes, not bits. Although bytes can be converted to bits (1 byte = 8 bits), the direct output of sizeof is in bytes. Conclusion: Understanding the sizeof operator is fundamental for efficient and effective C programming. It provides critical information about the memory requirements of data types and variables, which is crucial for tasks such as dynamic memory allocation and buffer management. The correct use of sizeof contributes to writing portable and reliable code by accounting for the varying sizes of data types across different systems and compilers."