Awesome q2a theme
+1 vote
16 views

https://www.onlinegdb.com/edit/r1CBB9EHO

#include<stdio.h>
int main()
{
    short int i = 10;
    char c = 'a';
    float  p = 3.0;
    short int s = i+c;

    printf("%d\n", sizeof(short int)); //size of short int is 2
    printf("%d\n", sizeof(char)); // size of char is 1
    printf("%d\n", sizeof(c+i)); /*why is size of (char+short int) is 4 and not 2.
    It should be 2 because char should be promoted to short int,
    and then the result of (short int + short int) should be short int whose size should be 2. */

    printf("%d", sizeof(s)); // though here it is 2 which is correct.


    return 0;


}


 

in Programming by (15 points) | 16 views
0
Why not do sizeof(c+c) ?
0
Thanks for the link mate.

1 Answer

+1 vote
Best answer
All operands of type char and short are converted to int before any operation.

That’s why sizeof(c+i) returns 4.

In the assignment operator the value of the RHS operand is converted to the type of the LHS operand.

That’s why sizeof(s) returns 2.
by (71 points)
selected by
+2
Thanks mate.

All data types having less rank than int are converted to int for operations.
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
Welcome to GATE CSE Doubts, where you can ask questions and receive answers from other members of the community.
9,200 questions
3,182 answers
14,686 comments
96,168 users