C | Operators | Question 27

filter_none

edit
close

play_arrow

link
brightness_4
code

#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


(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”.

filter_none

edit
close

play_arrow

link
brightness_4
code

#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


This modified program prints “abc”

Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.