retagged by
943 views
3 votes
3 votes
#include<stdio.h>
main()
{
    char *s="Manikant";
    int i=0;
    printf("%s",s+s[2]-s[4]);  // Line-1
    printf("\n%d",printf("GATE-2017"));  //Line-2
    printf("\n%c%d",s[i++],i);    //Line-3
}

What is output of Line-1 , Line-2 and Line-3 ?
retagged by

2 Answers

Best answer
4 votes
4 votes
#include<stdio.h>
int main() {
    char *s="Manikant";
    int i=0;
    printf("%s\n",s+s[2]-s[4]);  // Line-1
    printf("%d\n",printf("GATE-2017"));  //Line-2
    printf("%c%d\n",s[i++],i);    //Line-3
}

O/P

ikant        
GATE-20179
M1 // undefined [sequence point error]

s+s[2]-s[4] evalutes to s+3

printf("GATE-2017") , here 9 characters are printed by inside printf(). So inside printf() returns 9. => first printf prints 9 after GATE-2017

S[i++] expression evalutes to S[0] because of post increment.

Line3:

If an object is written to within a full expression, any and all accesses to it within the same expression must be directly involved in the computation of the value to be written.

printf("%c%d\n",s[i++],i); Here, we are accesing $i$ in the second argument of printf() but, it has nothing to do with the storing of $i$,which is a side effect of s[i++]. => causes undefined behaviour. Link

selected by
1 votes
1 votes

$s\left [ 2 \right ]=n,\, s[4]=k$

Let ASCII value of $s\left [ 4 \right ]=k=X$$\Rightarrow s[2]=k+3$

 printf("%s",s+s[2]-s[4]); 
  printf("%s",s+3);

it will print from s[3]to end where it finds NULL,hence output$\Rightarrow ikant$

 printf("%d\n",printf("GATE-2017"));$\Rightarrow$printf returns number of characters it printed!

Internal Printf will run 1 time and will print"GATE2017" and then 9 (number of characters it printed returned by printf)

hence output -:GATE20179

 printf("%c%d\n",s[i++],i);

It will print s[0]=m and then i=1 hence output will be 

m1

Related questions

0 votes
0 votes
2 answers
1
anonymous asked May 14, 2018
511 views
What changes must be done for printing value 5. #include <stdio.h int main() { int var; /*Suppose address of var is 2000 */ void *ptr = &var; *ptr = 5; printf("var=%d and...
0 votes
0 votes
1 answer
2
anonymous asked May 14, 2018
580 views
Please explain solution in brief . #include <stdio.h void f(char ); int main() { char *argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" }; f(argv); return 0; } void f(char ...
2 votes
2 votes
0 answers
3
adwaitLP asked Oct 10, 2016
1,199 views
Very basic C language doubt regarding \5 #include <stdio.h int main() { printf("\5"); return 0; }why the output of this code is ♣??
0 votes
0 votes
1 answer
4