Shivani
Shivani

Reputation: 85

Increment operation behaviour on signed char in C

For signed char in C, the range is from -128 to 127. How can a+1 (as in the code), produce 128 and not -128? The subsequent increment is normal (-127 for both a and i).

#include<stdio.h>
int main()
{
    char i=127,a=127;
    printf("%d\n",++i);//output is -128
    printf("%d\n",a+1);//output is 128
    //subsequent increment after this is -127 for a and i.
    return 0;
}

Upvotes: 2

Views: 199

Answers (1)

AnT stands with Russia
AnT stands with Russia

Reputation: 320631

When used with arithmetic operators, all small integer types (bool, char, short) are subjected to integer promotions before any arithmetic is perfomed on them. In the a + 1 expression your a is promoted to int meaning that this expression is actually interpreted as (int) a + 1. The range of char plays no role here. All calculations are performed in the domain of int and the result has int type. 127 + 1 is 128, which is what you see printed.

Your ++i is defined as i = i + 1. For the same reasons the right-hand side is calculated as (int) i + 1, meaning that in this case the addition is performed in the domain of int as well. Later the result is converted back to char and stored back into i. The addition itself does not overflow and produces 128, but the subsequent conversion back to char "overflows", which produces implementation-defined behavior. In your case that implementation-defined behavior produced the value of -128 as char result.

Upvotes: 8

Related Questions