sizeof(10) ?

1) sizeof(10) gives us 10=2 bytes.this means that 10 is stored as an int data type.what does this actually mean?
2) is this the situation(int var=10) where data constant is stored in memory ?
1) Not necessarily. Depends on the implementation. In many common implementations sizeof(int) returns 4 bytes. Your compiler is recognizing that it does not take 4 bytes to represent 10. i.e. 10 can be represented as a short (assuming here a short is 16 bits).

2) Not sure I understand what you're asking. Declaring an int and assigning it a value of 10 will cause the compiler to allocate 4 bytes of storage for var (again assuming an int is 32 bits in your implementation) and initialize that storage with the value 10.
what will sizeof(data constant) really give us?
> what will sizeof(data constant) really give us?

Where T is a type, sizeof(T) yields the number of bytes required to hold the object representation of an object of type T

Where e is an expression, sizeof(e) is equivalent to sizeof( decltype(e) ); that is sizeof(X) where X is the declared (static) type of the expression e.

sizeof(), in either case, is evaluated at compile time.

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
     int i = 0 ;

     int j = ++i ; // i is incremented
     std::cout << i << '\n' ; // prints 1

     j == sizeof(++i) ; // type of ++i is int; sizeof(++i) is sizeof(int)
     // the number of bytes that is required to hold the object representation
     // of an int is evaluated at compile time. i is not incremented at runtime.

     std::cout << i << '\n' ; // prints 1
}
Last edited on
@AbstractionAnon
10 is of type int so an int must be 2 bytes on his implementation.
It's all about the type of a value. Here's another example.
1
2
3
4
5
6
7
8
9
#include <iostream>

int main()
{
        std::cout << sizeof(0) << std::endl;    // sizeof int
        std::cout << sizeof(0L) << std::endl;   // sizeof long

        return 0;
}
Topic archived. No new replies allowed.