What will be the output of the following C code? void main() { char *p =…

2010

What will be the output of the following C code?

void main()
{
    char *p = "ayqm";
    char c;
    c = ++*p;
    printf("%c", c);
}

Answer: C. bConceptIn C the indirection operator * and the pre-increment operator ++ sit at the same precedence level and associate from right to left. An expression…

  1. A.

    a

  2. B.

    c

  3. C.

    b

  4. D.

    q

Attempted by 87 students.

Show answer & explanation

Correct answer: C

Concept

In C the indirection operator * and the pre-increment operator ++ sit at the same precedence level and associate from right to left. An expression written as ++*p therefore groups as ++(*p): indirection is applied first, so the increment acts on the object the pointer designates, not on the pointer itself. Pre-increment raises that object by one, writes the raised value back into it, and the expression as a whole evaluates to the new value. Raising a char by one means moving one step up in the execution character set; the C standard guarantees consecutive codes only for the decimal digits, so this item, like the examination that set it, assumes the ASCII-compatible sets used in practice, in which the lowercase letters are consecutive as well.

Application

  1. The declaration char *p = "ayqm"; gives p the address of the opening character of that string literal, so *p designates the character a.

  2. In the statement c = ++*p; the right-to-left grouping makes the expression c = ++(*p);, so *p is resolved to the stored character before the increment is applied.

  3. Pre-increment raises that stored character by one code point: a, whose ASCII code is 97, becomes b, whose ASCII code is 98, and this new character is written back into the string.

  4. Pre-increment evaluates to the raised value, so the assignment gives c the character b.

  5. printf("%c", c); prints a single character, and the program’s output is b.

Cross-check

Each near-miss reading of the expression lands somewhere else, which is exactly what the alternatives probe:

  • Writing *p++ groups as *(p++): the pointer is advanced and the expression yields the character it addressed before the move, namely a, leaving p aimed at y.

  • Writing *++p advances the pointer first and yields y, the character at index 1 of "ayqm".

  • Reaching c would need the stored character raised twice, but the program applies the increment only once.

  • Reaching q would need *(p + 2), that is index 2 of the literal, but the program never moves the pointer at all.

Standards note

One standards caveat belongs with this classic item. Because "ayqm" is a string literal, writing to the character it holds is undefined behaviour in standard C, and many compilers place literals in read-only memory, where the program would abort rather than print. The examination poses the item under the textbook execution model in which the pointed-to character is updated in place and the program prints; read that way, the trace above is exact and the printed character is b. Write through an array of your own instead, as in char s[] = "ayqm"; char *p = s;, and the same trace becomes well defined in standard C too.

Explore the full course: Tpsc Assistant Technical Officer

Loading lesson…