-5
#include <stdio.h>

main() {
    printf("ABCDE" + sizeof("ABC");/*please explain the output*/
}

Output of the program is E compiled with gcc, please explain

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Vivek Singh
  • 114
  • 1
  • 8

3 Answers3

3

Two quotes from the C Standard will make the result clear.

The first one is about string literals (6.4.5 String literals)

6 In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals.78) The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence....

And the second one is about implicit conversions (6.3.2.1 Lvalues, arrays, and function designators)

3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

So according to the first quote the string literal "ABC" is stored as a character array of the type char[4] (due to the appended zero) with the static storage duration.

According to the second quote in the expression

sizeof("ABC")

the array is not converted to a pointer. So in fact this expression is equivalent to the expression

sizeof( char[4] )

and yields the result equal to 4.

So the expression

"ABCDE" + sizeof("ABC")

can be rewritten like

"ABCDE" + 4

In this expression according to the second quote the string literal "ABCDE" that represents itself a character array is converted to the pointer to its first element. Thus there is used the so-called pointer arithmetic.

You can imagine the result of the evaluation of the expression in the function call with the following code snippet

char string_literal[6] = "ABCDE";
char *p = &string_literal[0];
printf( p + 4 );

the expression p + 4 points to the character 'E' and equivalent to &p[4] or &string_literal[4]

Pay attention to that according to the same C standard the function main without parameters shall be declared like

int main( void )

even though a return statement is absent in the function body.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thank you so much. I am new to stack overflow and have created an account just a day back. I have got this question in HCL interview, so I thought of asking this question. Many thank to you. – Vivek Singh Jul 31 '17 at 12:37
  • HCL is the company working in the INDIA, here is the link of the company https://en.wikipedia.org/wiki/HCL_Technologies – Vivek Singh Aug 01 '17 at 19:08
0

It's because of the terminating '\0' byte, 3 + 1 bytes so "ABCDE"[4] which is 'E'.

Also, the correct signature for main() is either

  1. int main(void) or,
  2. int main(int argc, char *argv[])
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
0

The output of your program is not E, it does not compile as posted because there is a missing ) in printf("ABCDE" + sizeof("ABC");

The size of a string constant is the number of bytes inluding the null terminator, hence sizeof("ABC") evaluates to 4.

"ABCDE" + 4 is the address if the E in the string and since the string dos not have any other character after this E, the output is the sane as that of printf("E");.

Note that the prototype for main without arguments is int main(void) and for good style, you should return 0 and output a newline.

Here is a corrected version of your code:

#include <stdio.h>

int main(void) {
    printf("ABCDE\n" + sizeof("ABC"));
    return 0;
}

Output is

E
chqrlie
  • 131,814
  • 10
  • 121
  • 189