328 views
7 7 votes

What is the output of the following code?

#include <stdio.h>

int main() {
    int a = 5, b = 2;
    float x = a / b + 0.5;
    printf("%.1f", x);
    return 0;
}

2 Answers

1 1 vote

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

In the expression $\texttt{a / b + 0.5}$, first $\texttt{a / b}$ is evaluated.

Since both $\texttt{a}$ and $\texttt{b}$ are integers, integer division is performed.

So, $\texttt{5 / 2 = 2}$.

Now the expression becomes $\texttt{2 + 0.5}$, which gives $\texttt{2.5}$.

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

Answer:
Position:
Show:

Related questions

7 7 votes
2 2 answers
292
292 views
GO Classes asked Jun 2
292 views
What is the output of the following code?#include <stdio.h int main() { float x = 5 / 2; printf("%.1f", x); printf(" "); float y = 5.0 / 2; printf("%.1f", y); return 0; }...
5 5 votes
2 2 answers
389
389 views
GO Classes asked Jun 2
389 views
What is the output of the following code?#include <stdio.h int main() { int i, sum = 0; for (i = 1; i <= 6; i++) { if (i % 2 == 0) continue; sum = sum + i; if (sum 6) br...
13 13 votes
3 3 answers
446
446 views
GO Classes asked Jun 2
446 views
What is the output of the following code?#include <stdio.h int main() { int a = 0, b = 5, c = 10; int ans = a++ && ++b || c ; printf("%d %d %d %d", ans, a, b, c); return ...
7 7 votes
3 3 answers
509
509 views
GO Classes asked Jun 2
509 views
What is the output of the following code?#include <stdio.h int main() { int x = 5; printf("%d", x++); printf("%d", ++x); return 0; }$57$ $67$ Compilation error Undefined ...