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 Show 11 previous comments 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 Shaik Masthan commented Aug 13, 2018 reply Follow flag when i=4, qt[4]='\0' it is copied to pt[4] and retrurns '\0' as return value from the expression ===> 0 in the condition of for loop ===> stop pt[] = "GATE\0ecture" ====> print GATE 0 0 replyShare gauravkc commented Aug 13, 2018 reply Follow flag Didn't get what u r saying. Assignment will always be true unless there r violations or other issues. 0 0 replyShare turendar commented Aug 13, 2018 reply Follow flag Dev c++ compiler gives the output as : AaEe 0 0 replyShare gauravkc commented Aug 13, 2018 reply Follow flag :o Bt how will the loop stop. 0 0 replyShare srestha commented Aug 13, 2018 reply Follow flag only increment statement is not given means loop always goes to infinity? 0 0 replyShare MiNiPanda commented Aug 13, 2018 reply Follow flag for(initialization;condition;increment/decrement) Here the condition is pt[i++]=qt[i++]. He didn't say that the loop will go to infinity because there isn't any increment or decrement operation. He said that the condition will not be False ever so the loop will go on forever. 1 1 replyShare srestha commented Aug 13, 2018 reply Follow flag but after sometimes both array will get NULL right? 0 0 replyShare MiNiPanda commented Aug 13, 2018 reply Follow flag srestha Can you please check the comments under this post .. there has been some discussion over it.. 0 0 replyShare Shaik Masthan commented Aug 14, 2018 reply Follow flag @turendar with in a sequence point we incremented i twice therefore it is undefined behavior in C. @gauravkc, this i want to deliver with my previous comment. 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. if you didn't get this, int a,b,c=5; a=(b=c); now check what is the value of a 0 0 replyShare gauravkc commented Aug 14, 2018 reply Follow flag a is having 5. But what I am saying is, the statement will return true or false depending upon successful or unsuccessful assignment. But assignment here cannot be unsuccessful unless there will be access violation or any other reason. Is this correct reasoning? 0 0 replyShare Shaik Masthan commented Aug 14, 2018 reply Follow flag the statement will return true or false depending upon successful or unsuccessful assignment then how you get a=5, according to your approach a=1 or a=0 right? 0 0 replyShare gauravkc commented Aug 14, 2018 reply Follow flag No as in.. 0 or non-zero. 0 0 replyShare Shaik Masthan commented Aug 14, 2018 reply Follow flag but this statement a=(b=c) always a is assigned to 5 only..... think why this happens then you understood the return value of assignment 0 0 replyShare gauravkc commented Aug 14, 2018 reply Follow flag 5 is assigned. But it is non-zero, hence considered as true. Which keeps the loop going. when the loop is running, qt[i] turns out to be 0, then it might halt there or it will never halt Is this correct? 0 0 replyShare Shaik Masthan commented Aug 14, 2018 reply Follow flag yes.... qt[i]='\0' gives return value as 0 then loop will stop 0 0 replyShare gauravkc commented Aug 14, 2018 reply Follow flag But it's non zero. It is just defined as null character. Is \0 stored as 0? 0 0 replyShare 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.