C Programming • C Basics, Operators, Loops
C Programming / C Operators and Precedence — Exercises

C Operators and Precedence — Exercises

Practical 2 C Basics, Operators, Loops

Write a C program to demonstrate operator precedence using an arithmetic expression.

Practical / Solution

C Operators and Precedence — Exercise 1

Problem

Write a C program to demonstrate operator precedence using an arithmetic expression.

Program

#include <stdio.h> int main() { int a = 5, b = 3, c = 2; int result; // multiplication is evaluated before addition result = a + b * c; printf("Result = %d\n", result); return 0; }

Explanation

Multiplication has higher precedence than addition, so b * c is evaluated before a +.

Expected Output

Result = 11