+ 1

Doubt in C

I don't know How does output comes? https://code.sololearn.com/c3RpBdMiCNXp/?ref=app

27th Jan 2021, 9:33 AM
Yogeshwaran P
Yogeshwaran P - avatar
13 Answers
+ 8
Adding on to Ipang 's answer You can't type cast *short* to type *char* neither you can convert *char* to type *short* So the only possible solution the compiler could think of was to promote both of them to the nearest data type which was *int* in this case. (Sizeof which is 4)
27th Jan 2021, 9:59 AM
Arsenic
Arsenic - avatar
+ 7
Speculatively speaking, expression `c + i` is implicitly casted to `int`, thus `sizeof` operator returns 4, which is used as last argument to be printed by `printf`.
27th Jan 2021, 9:47 AM
Ipang
+ 7
Ipang I suspect it must be a challanges question
27th Jan 2021, 10:06 AM
Arsenic
Arsenic - avatar
+ 6
Yogeshwaran Honestly I can't tell you why, I just (as I wrote, speculatively) figured addition of a `char` and a `short` require a promotion to a larger data type (assuming `int`). My speculation resulted from an attempt to cast `c + i` to various integral types e.g. `short`, and `long long` which gave me an impression that otherwise explicitly specified, `int` is the promote type used. printf("%lu %lu %lu", sizeof(i), sizeof(c), sizeof((short)c + i)); printf("%lu %lu %lu", sizeof(i), sizeof(c), sizeof((long long)c + i)); I didn't understand what you mean by "predict the result manually" BTW ... (Edit) Arsenic say correct 👍
27th Jan 2021, 10:05 AM
Ipang
+ 5
Arsenic Idk bro, maybe it is ... Anyways great answer about casting possibilities 👍
27th Jan 2021, 10:09 AM
Ipang
+ 5
Martin Taylor yes, that makes more sense.
27th Jan 2021, 4:00 PM
Arsenic
Arsenic - avatar
+ 3
Ipang why sizeof(c+i) implicitly converted it into int? What is a reason behind it And how can I predict the result manually,in such a above case?with other two data types instead of char and int
27th Jan 2021, 9:54 AM
Yogeshwaran P
Yogeshwaran P - avatar
+ 3
Thank you Ipang and Arsenic for detailed explanation.☺️ Ipang,I said "predict the result manually" which means to predict the output of this code, before compiling it on compiler..... Btw thank you for yours great answers....☺️
27th Jan 2021, 12:22 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 3
Yogeshwaran Honestly I don't know how to predict return value of `sizeof` operator, I guess it differs by compiler implementation and/or system platform (32/64 bit) somehow.
27th Jan 2021, 1:21 PM
Ipang
+ 2
Ipang it's ok no problem....☺️👍
27th Jan 2021, 1:34 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 1
Thank you so much Martin Taylor ☺️
27th Jan 2021, 4:07 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 1
why most compilers will error or at least give a warning about an undetermined result or non type parameter. Agreed, it will give a different result on each type depending on the platform's bus width, but spit out a warning in the case in the example, about using a number when it's looking for a type. Because, regardless of if it returns a char, short, int, long or anything else, the parameter isn't a 'type', which 'sizeof' is expecting. That is set in the prototype/definition of 'sizeof'. In the case of the example, it will just treat the parameter as a number, not a type and return and int (whatever size int is for the compiler/system it's on). Which is why (sizeof(c) + sizeof(i)) is the way to get the sum of both.
28th Jan 2021, 4:33 AM
A S Raghuvanshi
A S Raghuvanshi - avatar