Consider the following C code snippet. What will it print?
#include <stdio.h> int main() { int a = 5, b = 3; int result = a * b + a / b - b; printf("%d\n", result); return 0; }
Remember operator precedence: multiplication and division happen before addition and subtraction.
The expression is evaluated as (5 * 3) + (5 / 3) - 3.
5 * 3 = 15
5 / 3 = 1 (integer division)
So, 15 + 1 - 3 = 13.
Which of the following best explains why operators are needed in programming languages like C?
Think about how we do math or compare values in code.
Operators provide a simple way to perform arithmetic, comparisons, and other operations on data, making programming efficient and readable.
Analyze the following C code and determine its output.
#include <stdio.h> int main() { int x = 4, y = 0; if (x && y || !y) { printf("True\n"); } else { printf("False\n"); } return 0; }
Remember the precedence: ! (NOT) has higher precedence than && (AND), which has higher precedence than || (OR).
Expression: (x && y) || (!y)
x && y is 4 && 0 β 0 (false)
!y is !0 β 1 (true)
So, 0 || 1 β true, so "True" is printed.
Examine the code below. What error will it cause?
#include <stdio.h> int main() { int a = 10; int b = 5; int c = a +* b; printf("%d\n", c); return 0; }
Look carefully at the operator between a and b.
The operator '+*' is not valid in C. It causes a syntax error during compilation.
Consider this C code snippet initializing an array with an operator expression. How many elements does the array contain?
int arr[] = {1 + 2, 3 * 2, 4 / 2, 5 - 3};
Count the number of comma-separated values inside the braces.
The array is initialized with 4 values: 3, 6, 2, and 2. So, it contains 4 elements.