3 votes 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 turendar asked Aug 13, 2018 • edited Aug 13, 2018 by srestha turendar 1.4k views answer comment Share Follow See all 14 Comments See all 14 14 Comments reply MiNiPanda commented Aug 13, 2018 reply Follow Share 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 votes 2 votes Shubhgupta commented Aug 13, 2018 reply Follow Share @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 votes 1 votes arvin commented Aug 13, 2018 reply Follow Share @minipanda yes true whatever u said but when i am running this on online compiler it prints "AaEelecture" can u check it once? 0 votes 0 votes MiNiPanda commented Aug 13, 2018 reply Follow Share 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 votes 0 votes arvin commented Aug 13, 2018 reply Follow Share segmentation fault (core dumped) 0 votes 0 votes MiNiPanda commented Aug 13, 2018 reply Follow Share 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 votes 0 votes Shubhgupta commented Aug 13, 2018 reply Follow Share 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 votes 0 votes arvin commented Aug 13, 2018 reply Follow Share i think it needs a research to be done maybe :p lmao :) 0 votes 0 votes MiNiPanda commented Aug 13, 2018 reply Follow Share Shubhgupta Yes i think so :P arvin Yeah true :P 0 votes 0 votes srestha commented Aug 13, 2018 reply Follow Share It will give undefined behaviour isnot it? because same variable incrementing 2 times in a sequence point 0 votes 0 votes MiNiPanda commented Aug 13, 2018 reply Follow Share Yes right srestha 1 votes 1 votes srestha commented Aug 13, 2018 reply Follow Share so , no segmentation fault only undefined behavior for different compiler 1 votes 1 votes Prince Sindhiya commented Aug 14, 2018 reply Follow Share @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 votes 1 votes MiNiPanda commented Aug 14, 2018 reply Follow Share Yup 0 votes 0 votes Please log in or register to add a comment.
1 votes 1 votes 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 See all 19 19 Comments reply Show 16 previous comments Shaik Masthan commented Aug 14, 2018 reply Follow Share 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 votes 0 votes gauravkc commented Aug 14, 2018 reply Follow Share 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 votes 0 votes Shaik Masthan commented Aug 14, 2018 reply Follow Share 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 votes 0 votes Please log in or register to add a comment.