1

I have two codes and two question.

Question about first code: Has an integer promotion been made? I mean, y-->Promotion to int -->conversion to Double ? or y--> conversion to Double? which one is true?

Question about second code: Has an integer promotion been made? I mean, x and y-->Promotion to int?

//First code
#include <stdio.h>
int main() {
    double x=17.123;
    short y=13;
    x+y;
    return 0;
}
//Second Code
#include <stdio.h>
int main() {
    short x=17;
    short y=13;
    x+y;
    return 0;
}
14
  • Read up on this: port70.net/~nsz/c/c11/n1570.html#6.3.1.8 Commented Aug 9, 2024 at 17:20
  • hi thank's a alot. I will read! can you answer my question please? kind regards. Commented Aug 9, 2024 at 17:22
  • Promotions are never done when both operands are the same type. So why would the second code need any promotion? Commented Aug 9, 2024 at 17:25
  • 3
    @Barmar: In one of your previous comments, you wrote: "Promotions are never done when both operands are the same type." -- I believe that this statement is incorrect. In the link to the ISO C11 standard posted by someone else, the standard states: "Otherwise, the integer promotions are performed on both operands." In this sentence, "otherwise" means that if neither of the operands is a floating-point type. Commented Aug 9, 2024 at 17:35
  • 1
    @AndreasWenzel I misread the first bullet after that as saying no promotions are done. Commented Aug 9, 2024 at 18:28

1 Answer 1

1

Question about first code: Has an integer promotion been made? I mean, y-->Promotion to int -->conversion to Double ? or y--> conversion to Double? which one is true?

No, there is no integer promotion. For binary +, C 2018 6.5.6 4 says “If both operands have arithmetic type, the usual arithmetic conversions are performed on them.” The usual arithmetic conversions are specified in 6.3.1.8 1. For adding a double and a short, we only need to consider the first two rules:

First, if the corresponding real type of either operand is long double, the other operand is converted, without change of type domain, to a type whose corresponding real type is long double.

Otherwise, if the corresponding real type of either operand is double, the other operand is converted, without change of type domain, to a type whose corresponding real type is double.

So the short is converted to double. The remaining rules in the list begin with “Otherwise” and hence do not apply.

(The type domain refers to real or complex and does not concern us here.)

Question about second code: Has an integer promotion been made? I mean, x and y-->Promotion to int?

Yes, there is an integer promotion. For adding a short and a short, we consider further rules:

Otherwise, if the corresponding real type of either operand is float, the other operand is converted, without change of type domain, to a type whose corresponding real type is float.

Otherwise, the integer promotions are performed on both operands. Then the following rules are applied to the promoted operands:…

Thus, the integer promotions are performed on both operands. This converts them both to int, per 6.3.1.1 2:

If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.