ProgrammingVery hardGATE CS

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    char s[] = "abcd";
    char *p = s;
    (*p)++;
    *p++ += 2;
    ++*p;
    p[1]--;
    printf("%s", s);
    return 0;
}
  1. A. dcbd
  2. B. dbcd
  3. C. dcbc
  4. D. cdbd

Correct answer: A. dcbd

(*p)++ makes s[0] = 'b'. *p++ += 2 adds 2 to s[0] (the old pointer target), giving 'd', and then p points to s[1]. ++*p turns s[1] into 'c', and p[1]-- turns s[2] into 'b': "dcbd".

More C Programming questions

Exams that ask this topic