Why is the size of the data type different when the value is directly passed to the sizeof operator?

c sizeof
what is the size of double variable in bits
what is the size of short variable
what is the size of double variable in java
what is the size of int variable
sizeof(array in c)
what is the size of char variable
variable size meaning
#include <stdio.h>
int main() {
    char a = 'A';
    int b = 90000;
    float c = 6.5;
    printf("%d ",sizeof(6.5));
    printf("%d ",sizeof(90000));
    printf("%d ",sizeof('A'));
    printf("%d ",sizeof(c));
    printf("%d ",sizeof(b));
    printf("%d",sizeof(a));
    return 0;
}

The output is:

8 4 4 4 4 1

Why is the output different for the same values?

Character constants in C (opposite to C++) have the type int. So this call

printf("%d",sizeof('A'));

outputs 4. That is sizeof( 'A' ) is equal to sizeof( int ).

From the C Standard (6.4.4.4 Character constants)

10 An integer character constant has type int....

On the other hand (6.5.3.4 The sizeof and alignof operators)

4 When sizeof is applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1.

So the operand of the sizeof operator in this expression sizeof( 'A' ) has the type int while in this expression sizeof( a ) where a is declared like

char a = 'A';

the operand has the type char.

Pay attention to that calls like this

printf("%d",sizeof(6.5));

use incorrect conversion format specifier. You have to write

printf("%zu",sizeof(6.5));

Also in the above call there is used a constant of the type double while in this call

printf("%zu",sizeof(c));

the variable c has the type float.

You could get the same result for these calls if the first call used a constant of the type float like

printf("%zu",sizeof(6.5f));

4.3 — Object sizes and the sizeof operator, The amount of memory that an object uses is based on its data type. Because we typically access memory through variable names (and not directly via However, the actual size of the variables may be different on your To delete array of objects we use delete[], but we are not passing the size of array. sizeof operator is used to fetch the size a data structure will occupy in memory whereas strlen() is a function used to know thw length of a character array some details are: Usage sizeof() operator is used in different way according to the operand type. 1. When operand is a Data Type.

sizeof operator, sizeof operator. From cppreference.com value categories (lvalue, rvalue, xvalue). order of evaluation Other operators: a() , a,b , a?b:c 1) Yields the size in bytes of the object representation of type. 2) Yields the size in� The sizeof operator. In order to determine the size of data types on a particular machine, C++ provides an operator named sizeof. The sizeof operator is a unary operator that takes either a type or a variable, and returns its size in bytes. You can compile and run the following program to find out how large some of your data types are:

Why does the size of 'a' equal 4 bytes?, In Integer Data Type the values are converted to binary directly and stored In the first case the sizeof operator is being passed a variable of type char and behind this other than historic convention, it's just how C specifies the type of any � @DXM With the sizeof operand, if it's a value then it's sizeof var; if it's a pointer it's sizeof *var. This is also something that people can get wrong, and the compiler will happily take it without complaint.

Do not apply the sizeof operator to a pointer when taking the size of , However, using the sizeof operator to determine the size of arrays is error prone. The function has one parameter declared as int array[] and is passed a static array However, array has a pointer type because it is a parameter. show another NCCE, where sizeof(array) / sizeof(array[0]) is used in other dangerous ways. The size of the union data type is the same as sizeof (float), because the float type is larger than the int type. Since all of the members of a union occupy the same memory space, the union data type size doesn’t need to be large enough to hold the sum of all their sizes; it just needs to be large enough to hold the largest member.

C Programming sizeof operator, sizeof operator is used to evaluate size of data type,variable. sizeof operators looks like function still it It will print the value of variable using sizeof() operator. In this case we have directly passed an data type to an sizeof. a data type that stores data in two bytes of memory can hold 16 bits of information. This means it can store 2^16 bit patterns, which is 65,536 different combinations of zeros and ones. The short data type, stores both positive and negative numbers, so it uses its 16 bits to represent the values from -32,768 to +32,767

[PDF] COMP26120: Pointers in C (2019/20), To use pointers to pass arguments to functions by reference a variable name directly references a value, and a pointer indirectly other data type). • sizeof is a Using operator sizeof to determine standard data type sizes (Part 1 of 2)� Data Types, Arrays and Strings. Simple and Structured Data Types: A simple data type can store only one value at a time. A structured data type is one in which each data item is a collection of other data items. In a structured data type, the entire collection uses a single identifier (name).

Comments
  • 6.5 is not a float, it's a double
  • printf("%d",sizeof(6.5f)); to make it a float.
  • "why the output is differen0t here?" why it should be the same? The fact that you can assign one to another does not mean they have exactly the same type.
  • The format specifier should be, for example, printf("%zu", sizeof(6.5));