-3

char c=8+"ab"; //output is c=112 with warning

int c=8+"ab"; //output is c=4195952 with warning

char c=8+"a"; //output is c=112 (same as many caharacters in string constants) with warning

How is c value computed here? Please explain.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261

4 Answers4

3

"ab" is a char *. You can add an integer offset to a pointer, so 8 + "ab" is a char * pointer a few characters past the end of the string.

Then (and this will warn you) you can cast a pointer to an integer type. So int c=8+"ab" gives you the lower 32 bits of the pointer (assuming 32-bit int) and char c=8+"ab" gives you just the lowest 8 bits.

The actual value is undefined and of no use here, just don't do this.

Duncan
  • 92,073
  • 11
  • 122
  • 156
3

0+"ab" is a well defined pointer addition. Result: the char * pointer to the zero-th element 'a'.

1+"ab" is a well defined pointer addition. Result: the char * pointer to the element 1: 'b'.

2+"ab" is a well defined pointer addition. Result: the char * pointer to the element 2: '\0'.

3+"ab" is a well defined pointer addition. Result: the char * pointer 1 passed 2+"ab".

8+"ab" is not well defined pointer addition as the answer does not lie within the string literal or one past. The result is undefined behavior (UB).


How is c value computed here?

As all OP's examples use 8+"ab";. All are UB. Reported results have no specified meaning.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

Let's consider for example this declaration

char c=8+"ab";

The string literal "ab" is a character array with type char[3].

In the expression 8 + "ab" this array is implicitly converted to pointer to its first character. So the expression has the type char * and points outside the array because the array has only three elements.

As result the character is initialized by the address outside the array converted to the type char that does not make sense and can lead to undefined behavior.

As for the output you have for the character c then in fact it is due to the conversion

char c = 4195952;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Your last statement seems a bit off: the pointer is never dereferenced so whatever is stored there is never accessed. – Duncan Jan 05 '18 at 14:18
  • @GrzegorzSzpetkowski I wasn't objecting to saying it was undefined behaviour, I was objecting to Vlad's original statement that said it used the value at that location (which he has now corrected). – Duncan Jan 05 '18 at 14:26
1

You are causing undefined behavior.

As already explained by others, this is basically a pointer arithmetic, and according to the spec, C11, chapter §6.5.6

[...] If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined.

your pointer arithmetic takes the result well past the "one past the last element of the array object", so you invoke undefined behavior

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261