413 views
11 11 votes

What is the output of the following code?

#include <stdio.h>

int main() {
    int a = 5, b = 10;
    printf("%d", a > b || b > 0);
    return 0;
}

2 Answers

1 1 vote

Here, $\texttt{a = 5}$ and $\texttt{b = 10}$.

The expression inside $\texttt{printf()}$ is $\texttt{a > b || b > 0}$.

Now, $\texttt{a > b}$ means $\texttt{5 > 10}$, which is false, so its value is $0$.

Also, $\texttt{b > 0}$ means $\texttt{10 > 0}$, which is true, so its value is $1$.

Therefore, the expression becomes $0 \ || \ 1$.

In C, logical OR $\texttt{||}$ gives $1$ if at least one condition is true.

So, $0 \ || \ 1 = 1$.

Therefore, the output is $\texttt{1}$.

Answer:
Position:
Show:

Related questions

7 7 votes
2 2 answers
409
409 views
GO Classes asked Jun 2
409 views
What is the output of the following code?#include <stdio.h int main() { int a = 5, b = 10; printf("%d", a < b && b 0); return 0; }
7 7 votes
2 2 answers
348
348 views
GO Classes asked Jun 2
348 views
Which of the following is not a keyword in C?$\texttt{int}$ $\texttt{return}$ $\texttt{main}$ $\texttt{while}$
9 9 votes
1 1 answer
432
432 views
GO Classes asked Jun 2
432 views
Assume that $\texttt{char}$ is $8$ bits and signed integers are represented using $2$'s complement. Consider the following C code:#include <stdio.h int main() { signed ch...
6 6 votes
4 4 answers
403
403 views
GO Classes asked Jun 2
403 views
Which of the following is a valid identifier in C?$\texttt{2sum}$ $\texttt{total-marks}$ $\texttt{_value}$ $\texttt{float}$