retagged by
1,083 views

2 Answers

Best answer
8 votes
8 votes

Output :  1 2

char *a

is string constant it will be stored in memory like this. 

ASCII  for  '\0' is 0

Tracing

selected by
3 votes
3 votes
while(a[++i])
    printf("%d",*++a - 'a');

Here first the while condition executes i.e a[++i] --> *(a + ++i) --> So here ++ has high precedence than binary '+'. So first i increments which then is added to address pointed by char pointer a and then finally dereferenced.

Now in the printf statement " *++a - 'a' " --> ++ has high precedence then '*' - dereference and then finally binary '-' will execute.

Value of i a[i++] --> while loop condition *++a , i value *++a - 'a' , a points to
0 a[1] = 'b' b , i = 1  1 [as 98 - 97 ] , a points 'b'
1 a[2] = 'd' c , i = 2 2 [as 99 - 97] , a points 'c'
2 a[3] = '\0'  --> breaks while loop    

Hence the code outputs 12.

Related questions

0 votes
0 votes
1 answer
1
Psnjit asked Jan 12, 2019
1,137 views
main(){unsigned int i= 255;char *p= &i;int j= *p;printf("%d\n", j);unsigned int k= *p;printf("%d", k);} Both the outputs are -1. I have even tried with - int i = 255(3rd ...
0 votes
0 votes
1 answer
2
Mr khan 3 asked Nov 3, 2018
978 views
#include<stdio.h void fun(int *p,int *q) { p=q; *p=q; } int i=0,j=1; int main() { fun(&i,&j); printf("%d%d",i,j); }What will be the output of i and j in 16-bit C Compiler...
0 votes
0 votes
0 answers
4
garvit_vijai asked Sep 2, 2018
683 views
Please explain the output for the following program: #include<stdio.h>int main() { int i = 100; int *a = &i; float *f = (float *)a; (*f)++; pri...