C | Operators | Question 27
#include <stdio.h> #include <stdlib.h> int top=0; int fun1() { char a[]= { 'a' , 'b' , 'c' , '(' , 'd' }; return a[top++]; } int main() { char b[10]; char ch2; int i = 0; while (ch2 = fun1() != '(' ) { b[i++] = ch2; } printf ( "%s" ,b); return 0; } |
(A) abc(
(B) abc
(C) 3 special characters with ASCII value 1
(D) Empty String
Answer: (C)
Explanation: Precedence of ‘!=’ is higher than ‘=’. So the expression “ch2 = fun1() != ‘(‘” is treated as “ch2 = (fun1() != ‘(‘ )”. So result of “fun1() != ‘(‘ ” is assigned to ch2. The result is 1 for first three characters. Smile character has ASCII value 1. Since the condition is true for first three characters, you get three smilies.
If we, put a bracket in while statement, we get “abc”.
#include <stdio.h> #include <stdlib.h> int top=0; int fun1() { char a[]= { 'a' , 'b' , 'c' , '(' , 'd' }; return a[top++]; } int main() { char b[20]; char ch2; int i=0; while ((ch2 = fun1()) != '(' ) { b[i++] = ch2; } b[i] = '\0' ; printf ( "%s" ,b); return 0; } |
This modified program prints “abc”
Quiz of this Question
Please Login to comment...