C compilers default behavior with constants

Say I do the following:

char x;

x = x + 077;

In this situation does the constant 077 have a default type of int even though the expression is assigning to a char? From reading K&R I have inferred that the + operator has higher precedence than = and the 077 (octal) has a default type of int, thus the value of x is promoted to an int. Then the addition operation is performed and then the answer of converted back to a char and assigned to x. Is this correct?

Does the same behavior happen for the following two expressions?:

x += 077
x += 1

Also, what happens if the following is used in an expression:

(char) 14

Is 14 first an int by default which is then reduced to a char, or is it a char to begin with?

char x;

x = 14;

Also, in this case is 14 first an int which is then reduced to a char or is it a char to begin with?


14, 1 and 077 are integer literals, and those are always int or some larger type, never a char. That type doesn't depend on the context. (See C99 § for the details on the actual type.)

The compound assignment a += b behave exactly as the corresponding a = a + b expression, except that a is only evaluated once:

(C99 § A compound assignment of the form E1 op = E2 differs from the simple assignment expression E1 = E1 op (E2) only in that the lvalue E1 is evaluated only once.

Similar questions

Data Variables/Constants and Type int Constants (C Primer Plus)
world. I'm a novice in CS field and learning C language with a book called 'C Primer Plus'. I have a question regarding C data types (Chapter 3 of the book). In the book, it is written that [...] Some types of data are preset before a program is used and keep their values unchanged throughout the life of the programs. These are constants. Other typ...
Examples of behavior differences of C Compilers
I was looking for some area of C,where we get different behaviors by different compilers. Looking to following code,We get output 7 in gcc4.4.5, and 10 in tcc4.5. int i=0; i=i++ + ++i + i++ + ++i; printf("%d",i); Can we identify other area of usage where we get different behaviors, which should be avoided for a good programming practice. [Note: Abo...
A weird behavior of C compilers GCC and Turbo
I've gone through other similar questions, but trying to understand the situation I'm facing. So, here's my two line C code. And here are the outputs I get from GCC and Turbo C Compiler. Output: Output: I tried all sorts of experiments with pre-increment operator individually, and both compilers work similar but when I use above printf statement, o...
C syntax: Is the following 'extern volatile const' behavior consistent among C compilers?
I have done the following C experiment: I have three files (a.c, a.h, b.c): a.c: a.h: b.c: What I want to obtain with this is to be able to modify a variable selectively. So basically, v will be seen as int for a.c and as const int for the rest of the code (e.g. b.c). With GCC 4.8.1 this links and behaves as expected. I want to know if I can rely o...
Windows Shared memory and different Compilers weird behavior
I had the two different process interacting with each other using the shared memory and i observe that it doesn't work as expected for some reason, the thing is, the buffer for some reason fills up with additional zero's when i put data into it. Here is what i do: Process1 vc++ (msvc2012 active solution platform Win32): Here is process two, written...
strtok() behavior is different across compilers
I wrote a program which parses a string according to this format: On my computer, this program works flawlessly. However, once I have uploaded the code and compiled it on the school's computer, strtok() has a different behavior. For instance, with this string: p2test/f4.txt:1:, on my computer the first token would be p2test/f4.txt. However, on the ...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.