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

3

15
#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?

Lakes answered 10/12, 2019 at 18:49 Comment(4)
6.5 is not a float, it's a doubleTamas
printf("%d",sizeof(6.5f)); to make it a float.Giuseppinagiustina
"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.Foreigner
The format specifier should be, for example, printf("%zu", sizeof(6.5));Eubank
L
8

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));
Lammas answered 10/12, 2019 at 18:51 Comment(0)
C
18

Constants, like variables, have a type of their own:

  • 6.5 : A floating point constant of type double
  • 90000 : An integer constant of type int (if int is 32 bits) or long (if int is 16 bits)
  • 'A' : A character constant of type int in C and char in C++

The sizes that are printed are the sizes of the above types.

Also, the result of the sizeof operator has type size_t. So when printing the proper format specifier to use is %zu, not %d.

Circumference answered 10/12, 2019 at 18:52 Comment(0)
L
8

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));
Lammas answered 10/12, 2019 at 18:51 Comment(0)
A
1

Because the values don't matter to sizeof. It's the size of the types.

  • character constants are ints, not chars.

  • floating-point constants are by default doubles unless you suffix them with f or l.

Actino answered 11/12, 2019 at 12:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.