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; } |
chevron_right
filter_none
(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; } |
chevron_right
filter_none
This modified program prints “abc”
Quiz of this Question
Recommended Posts:
- Operators in C | Set 2 (Relational and Logical Operators)
- C | Operators | Question 27
- C | Operators | Question 26
- C | Operators | Question 18
- C | Operators | Question 17
- C | Operators | Question 15
- C | Operators | Question 13
- C | Operators | Question 21
- C | Operators | Question 22
- C | Operators | Question 27
- C | Operators | Question 26
- C | Operators | Question 11
- C | Operators | Question 24
- C | Operators | Question 23
- C | Operators | Question 12



