• retagged by
1,899 views
3 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 ?

2 Answers

Best answer
4 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 1 vote

$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

Position:
Show:

Related questions

1 1 vote
1 1 answer
228
228 views
khanshahima4 asked Sep 24
228 views
1. If precedence tells me which operator has priority, and associativity tells me how to handle operators at the same level, then why do I need a separate concept called ...
1 1 vote
1 answers 1 answer
378
378 views
Debargha Mitra Roy asked Dec 21, 2024
378 views
Consider the following code. What is the value printed by the below code?_______#include <stdio.h int main() { char s1[] = "Hello"; char s2[] = {'H', 'e', 'l', 'l', 'o'};...
0 0 votes
2 2 answers
880
880 views
anonymous asked May 14, 2018
880 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 0 votes
1 1 answer
896
896 views
anonymous asked May 14, 2018
896 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 ...