Does it fit in a byte?
Combine two comparisons with a logical operator. An easy embedded C problem (Variables and operators), run and graded in the browser.
easy · Variables and operators · logical-operators · ranges
A byte holds a whole number from 0 to 255. Before storing a value in one, it is worth checking that it fits.
Return 1 if v is between 0 and 255 inclusive, and 0 otherwise. && means "and", so a && b is 1 only when both sides are true.
Starter code
int fits_in_byte(int v) {
return 0;
}Example tests
A value that fits
printf("%d\n", fits_in_byte(100));Expected output:
1A value that is too large
printf("%d\n", fits_in_byte(300));Expected output:
0Learn it first
- Variables and operators · Storing values, doing arithmetic, comparing, and the two surprises of integer maths.
More Variables and operators problems
- Average of three · Add three values and divide.
- Last digit · Use the remainder operator.
- Celsius to Fahrenheit · Fix a formula that divides too early.
- Time in seconds · Combine several values into one total.
- Even or odd · Produce a yes-or-no answer as 1 or 0.
- Operator precedence · Fix an expression that is evaluated in the wrong order.
- Percentage · Keep precision by ordering the operations correctly.