3-byte int and 5-byte long?

I think 3.9.1/2 (C++98) sums this up nicely (immediately followed by analogous information for the unsigned types): There are four signed integer types: “signed char”, “short int”, “int”, and “long int.” In this list, each type provides at least as much storage as those preceding it in the list. Plain ints have the natural size … Read more

How to get the string size in bytes?

You can use strlen. Size is determined by the terminating null-character, so passed string should be valid. If you want to get size of memory buffer, that contains your string, and you have pointer to it: If it is dynamic array(created with malloc), it is impossible to get it size, since compiler doesn’t know what … Read more

Why is (sizeof(int) > -1) false? [duplicate]

sizeof(int) has type size_t, which is an unsigned integer type. -1 has type int, which is a signed integer type. When comparing a signed integer with an unsigned integer, first the signed integer is converted to unsigned, then the comparison is performed with two unsigned integers. sizeof(int) > (unsigned int)-1 is false, because (unsigned int)-1 … Read more

How sizeof(array) works at runtime?

sizeof is always computed at compile time in C89. Since C99 and variable length arrays, it is computed at run time when a variable length array is part of the expression in the sizeof operand. Same for the evaluation of the sizeof operand: it is not evaluated in C89 but in C99 if the operand … Read more

Can sizeof return 0 (zero)

In C++ an empty class or struct has a sizeof at least 1 by definition. From the C++ standard, 9/3 “Classes”: “Complete objects and member subobjects of class type shall have nonzero size.” In C an empty struct is not permitted, except by extension (or a flaw in the compiler). This is a consequence of … Read more

How can I find the number of elements in an array?

If you have your array in scope you can use sizeof to determine its size in bytes and use the division to calculate the number of elements: #define NUM_OF_ELEMS 10 int arr[NUM_OF_ELEMS]; size_t NumberOfElements = sizeof(arr)/sizeof(arr[0]); If you receive an array as a function argument or allocate an array in heap you can not determine … Read more

What does sizeof without () do? [duplicate]

So if we look at the grammar for sizeof in the C99 draft standard section 6.5.3 Unary operators paragraph 1 it is as follows: sizeof unary-expression sizeof ( type-name ) There is a difference you can not use a type-name without parenthesizes and section 6.5.3.4 The sizeof operator paragraph 2 says(emphasis mine): The sizeof operator … Read more