Why sizeof int is wrong, while sizeof(int) is right?
The following could be ambiguous:
sizeof int * + 1
Is that (sizeof (int*)) + 1
, or (sizeof(int)) * (+1)
?
Obviously the C language could have introduced a rule to resolve the ambiguity, but I can imagine why it didn't bother. With the language as it stands, a type specifier never appears "naked" in an expression, and so there is no need for rules to resolve whether that second *
is part of the type or an arithmetic operator.
The existing grammar does already resolve the potential ambiguity of sizeof (int *) + 1
. It is (sizeof(int*))+1
, not sizeof((int*)(+1))
.
C++ has a somewhat similar issue to resolve with function-style cast syntax. You can write int(0)
and you can write typedef int *intptr; intptr(0);
, but you can't write int*(0)
. In that case, the resolution is that the "naked" type must be a simple type name, it can't just be any old type id that might have spaces in it, or trailing punctuation. Maybe sizeof
could have been defined with the same restriction, I'm not certain.
why sizeof type compared with integer returns false
The sizeof
operator yields not an int
but size_t
which is an unsigned integer type. When you compare a signed integer like -1 to an unsigned integer you will end up comparing the wrong values.
Do the following changes and the code will work as expected.
#include<stdio.h>
#include <stdbool.h>
int main()
{
int x = (int)sizeof(int) > -1;
bool z = sizeof(int);
printf("x is %d \t z is %d \n",x,z);
if((int)sizeof(int) > -1)
{
printf("true\n");
}
else
printf("false\n");
}
Output:
x is 1 z is 1
true
sizeof operator giving wrong results
The syntax int maxChainLen(val p[], int n)
is equivalent to: int maxChainLen(val* p,int n)
- the function actually accepts a pointer to val
.
When you call this function, the array you pass decays to a pointer.
You can also see this answer for exact standard-backed wording.
Why this code is printing False, though size of int is greater than -1?
Well sizeof
returns size_t
which is unsigned and when it is compared with int
the int is promoted to unsigned and the bit representation all 1's now considered as unsigned, which is bigger than -1
and also that of sizeof int
. That's why this result.
Correct format specifier for size_t
is %zu
.
Why is sizeof(int) less than -1?
sizeof
generates a size_t
which is always positive. You are comparing it with -1
which is probably promoted in size_t
which gave you a HUGE number, most likely greater than the size of an int.
To make sure of it, try this:
printf("%zu\n", sizeof(int));
printf("%zu\n", (size_t)(-1));
[EDIT]: Following comments (some have been removed), I precise indeed that sizeof
is an operator, not a function.
Is the (int) in sizeof (int) a typecast operator or some special case parameter? [C]
()
is used with sizeof
operator if the operand is a data type.
C11: 6.5.3.4 (p2):
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.
sizeof operator error on data types
According to the C standard (cited from ISO/IEC 9899:TC3 section 6.5.3.4):
The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type.
So using a type name without parenthesis isn't legal.
Also, sizeof
returns an implementation defined value of the type size_t
and you probably should not cast it.
Related Topics
Is Make_Shared Really More Efficient Than New
Why Was Pair Range Access Removed from C++11
Why Do We Have Reinterpret_Cast in C++ When Two Chained Static_Cast Can Do Its Job
Safer But Easy-To-Use and Flexible C++ Alternative to Sscanf()
Std::Back_Inserter for a Std::Set
How to Get Current CPU and Ram Usage in C++
Is a Linked-List Implementation Without Using Pointers Possible or Not
Opencv Surf Function Is Not Implemented
How to Force a Function Not to Be Inlined
Use-Cases of Pure Virtual Functions with Body
Template Parameter Packs Access Nth Type and Nth Element
Template Function Specialization for Integer Types
How to Use Libraries Compiled with Mingw in Msvc
Int *Array = New Int[N]; What Is This Function Actually Doing
Should Objects Delete Themselves in C++
Why Is Operator!= Removed in C++20 for Many Standard Library Types