3 3 votes what is the output of the c program given below, #include<stdio.h> int main() { char pt[]="Gatelecture"; char qt[]="GATE"; int i=0; for( ;pt[i++]=qt[i++]; ); printf("%s",pt); } a. AeEe b. AaEe c. aAeE d. none of the above Programming in C + – turendar 4.3k views answer comment Share Follow Print See all 14 Comments 14 14 Comments reply MiNiPanda commented Aug 13, 2018 reply Follow flag Here some very less known concepts are used. I saw the output and thought this is the way it works. However I have one doubt still which I will mention at the end. Let me use subscript of i like i1 and i2 to indicate the increment after each step i.e. which 'i' is responsible for which increment. Initially, i=0 pt[i1++]=qt[i2++] // i1++ is evaluated and i becomes 1 by the time it reaches the right hand side of the assignment. So, pt[0]=qt[1] Again i becomes 2 after i2++ is evaluated. 0 1 2 3 4 5 6 7 8 9 10 11 12 A a t e l e c t u r e s \0 So, i=2 pt[i1++]=qt[i2++] // i1++ is evaluated and i becomes 3 by the time it reaches the right hand side of the assignment. p[2]=q[3] .. Again i becomes 4 after i2++ is evaluated. 0 1 2 3 4 5 6 7 8 9 10 11 12 A a E e l e c t u r e s \0 So, i=4 pt[i1++]=qt[i2++] // i1++ is evaluated and i becomes 5 by the time it reaches the right hand side of the assignment. p[4]=q[5] .. Again i becomes 6 after i2++ is evaluated. 0 1 2 3 4 5 6 7 8 9 10 11 12 A a E e \0 e c t u r e s \0 See the last assignment, p[4]=q[5] ='\0' (After qt[3] every index is '\0') The assignment operator is defined (in C) as returning the value of the variable that was assigned to - i.e. the value of the expression (a=b) is the value of a after the expression has been evaluated. Ref: https://stackoverflow.com/questions/14697643/low-level-details-of-c-c-assignment-operator-implementation-what-does-it-retu So the return type of this would be NULL as p[4] is NULL. So condition is false and loop will break. Now pt will print until there in no NULL character so AaEe is printed. My doubt: Assignment operators are evaluated in right to left direction. But is the post increment operators applied on both sides of assignment operator evaluated from left to right? (I know the associativity of post increment is from left to right and it might be a possible reason why i is incremented in that direction but i am not very sure)/ 2 2 replyShare Shubhgupta commented Aug 13, 2018 reply Follow flag @MiNiPanda, Don't you think this will show the undefined behaviour of C. Because this program is giving different results in different compilers. See the last assignment, p[4]=q[5] ='\0' (After qt[3] every index is '\0') And how can you be sure that all the elements of array after q[4] will be intialized to '\0' means it can be some garbage value also. Can you please give me some referene for this? 1 1 replyShare arvin commented Aug 13, 2018 reply Follow flag @minipanda yes true whatever u said but when i am running this on online compiler it prints "AaEelecture" can u check it once? 0 0 replyShare MiNiPanda commented Aug 13, 2018 reply Follow flag Shubhgupta Actually I printed qt[4], qt[5] and even qt[500] and all gave blanks so thought that they have null character. But initially even i also thought like you about the garbage values (even segmentation fault also can be there :/ ). Also about the undefined behaviour part you are correct. arvin Can you please print qt[5] and tell what you are getting? 0 0 replyShare arvin commented Aug 13, 2018 reply Follow flag segmentation fault (core dumped) 0 0 replyShare MiNiPanda commented Aug 13, 2018 reply Follow flag Okay :P that is why pt[4] is not getting null character and is printing the entire string until the original null at pt[12] is found. But I wonder how the code is running without error in the first place while we are trying to use qt[5]. 0 0 replyShare Shubhgupta commented Aug 13, 2018 reply Follow flag Because in my compiler in qt[5] it is showing some garbage value. So basically answer for this question should be D) None of the above? 0 0 replyShare arvin commented Aug 13, 2018 reply Follow flag i think it needs a research to be done maybe :p lmao :) 0 0 replyShare MiNiPanda commented Aug 13, 2018 reply Follow flag Shubhgupta Yes i think so :P arvin Yeah true :P 0 0 replyShare srestha commented Aug 13, 2018 reply Follow flag It will give undefined behaviour isnot it? because same variable incrementing 2 times in a sequence point 0 0 replyShare MiNiPanda commented Aug 13, 2018 reply Follow flag Yes right srestha 1 1 replyShare srestha commented Aug 13, 2018 reply Follow flag so , no segmentation fault only undefined behavior for different compiler 1 1 replyShare Prince Sindhiya commented Aug 14, 2018 reply Follow flag @Minipanda I am getting" GaTe" as output And as Arvin said he is getting "AaEelecture" on online compiler so it should be undefined behaviour As different compiler are showing different output 1 1 replyShare MiNiPanda commented Aug 14, 2018 reply Follow flag Yup 0 0 replyShare Please log in or register to add a comment.
1 1 vote Undefined behavior in C. Answer is compiler dependent. Hence, the correct answer must be d) None of the above. gauravkc answered Aug 13, 2018 • edited Aug 14, 2018 by gauravkc gauravkc comment Share Follow See all 19 Comments 19 19 Comments reply Show 16 previous comments Shaik Masthan commented Aug 14, 2018 reply Follow flag ASCII values of character returns if you are assigning them the integer right? ex:- int a='A'; ===> int a=65 (Both are equivalent if the range is <128) ASCII value of '\0' is 0 0 0 replyShare gauravkc commented Aug 14, 2018 reply Follow flag Okk.. I don't know why but my laptop is also showing it will go into infinite loop. I'm using gcc 4.9.2 :/ The answer is compiler dependent I guess. Thanks for having a detailed conversation :) 0 0 replyShare Shaik Masthan commented Aug 14, 2018 reply Follow flag note that, i shows undefined behavior.... therefore i may not assign the value as 4 in your compiler... at 4th index only we have null ===> if you didn't get that index to i, you are in infinite loop if your compiler shows infinite loop, then print i value in the loop....you should didn't encounter i=4 0 0 replyShare Please log in or register to add a comment.