Type Casting, Operators & Error Types
Type casting is the process of converting one data type to another. In C, this can happen automatically (implicit) or manually (explicit).
Let's solve your examples step by step:
#include <stdio.h>
int main() {
float x = 5/2; // Integer division happens FIRST!
printf("%.5f", x);
return 0;
}
5/2 is integer division = 2 (not 2.5)
#include <stdio.h>
int main() {
float x = 7 / 4; // Integer division
printf("%.2f", x);
return 0;
}
Explanation: 7 ÷ 4 in integer division = 1 (remainder ignored)
#include <stdio.h>
int main() {
float x = 7 / 4.0; // Mixed division: int / float
printf("%.2f", x);
return 0;
}
Manual conversion using the cast operator: (datatype)variable
#include <stdio.h>
int main() {
int a = 5, b = 2;
printf("%.1f", (float)(a / b)); // Cast AFTER division
return 0;
}
#include <stdio.h>
int main() {
int a = 5, b = 2;
printf("%.1f", (float)a / b); // Cast BEFORE division
return 0;
}
| Expression | Order of Operations | Result | Explanation |
|---|---|---|---|
float x = 5/2; |
5/2 → assign to x | 2.0 | Integer division first |
float x = 5/2.0; |
5→5.0, then 5.0/2.0 | 2.5 | Float division |
(float)(5/2) |
5/2 → cast result | 2.0 | Cast after division |
(float)5/2 |
cast 5 → divide by 2 | 2.5 | Cast before division |
int x = 5;
int y = ++x; // x is incremented FIRST, then assigned
// x = 6, y = 6
int x = 5;
int y = x++; // x is assigned FIRST, then incremented
// x = 6, y = 5
#include <stdio.h>
int main() {
int a = 5, b, c;
b = ++a; // Pre-increment: a becomes 6, b gets 6
c = a++; // Post-increment: c gets 6, a becomes 7
printf("a = %d, b = %d, c = %d\n", a, b, c);
return 0;
}
div>
int x = 5; // Binary: 101
int result = x << 2; // Shift left by 2 positions
// Result: 10100 (binary) = 20 (decimal)
// 5 × 2² = 5 × 4 = 20
div>
int x = 20; // Binary: 10100
int result = x >> 2; // Shift right by 2 positions
// Result: 101 (binary) = 5 (decimal)
// 20 ÷ 2² = 20 ÷ 4 = 5
#include <stdio.h>
int main() {
int x = 5;
int result1 = x++ + ++x; // Complex expression
// Step by step:
// x = 5 initially
// x++: uses 5, then x becomes 6
// ++x: x becomes 7, uses 7
// result1 = 5 + 7 = 12
// Final x = 7
printf("result1 = %d, x = %d\n", result1, x);
return 0;
}
div>
// Missing semicolon
int x = 5 // ERROR: Missing semicolon
printf("Hello");
// Undeclared variable
int x = 5;
y = 10; // ERROR: 'y' not declared
// Wrong function name
#include <stdio.h>
int main() {
Print("Hello"); // ERROR: should be 'printf'
return 0;
}
// Mismatched braces
int main() {
if (x > 5) {
printf("Greater");
// ERROR: Missing closing brace
return 0;
}
// Division by zero
#include <stdio.h>
int main() {
int a = 10, b = 0;
int result = a / b; // RUNTIME ERROR: Division by zero
printf("%d\n", result);
return 0;
}
// Array index out of bounds
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
printf("%d\n", arr[10]); // RUNTIME ERROR: Invalid index
return 0;
}
// Infinite recursion
int factorial(int n) {
return n * factorial(n); // ERROR: No base case
}
// Wrong loop condition
#include <stdio.h>
int main() {
int sum = 0;
// Intent: sum numbers from 1 to 10
for(int i = 1; i < 10; i++) { // BUG: Should be i <= 10
sum += i;
}
printf("Sum: %d\n", sum); // Prints 45 instead of 55
return 0;
}
// Wrong formula
#include <stdio.h>
int main() {
int celsius = 25;
// Intent: Convert Celsius to Fahrenheit
// Correct formula: F = (C × 9/5) + 32
int fahrenheit = celsius * 9/5 + 32; // BUG: Integer division
printf("%d°C = %d°F\n", celsius, fahrenheit);
return 0;
}
| Error Type | Detection Time | Program Behavior | Example | Difficulty to Fix |
|---|---|---|---|---|
| Compile-Time | During compilation | Won't compile | Missing semicolon | Easy |
| Runtime | During execution | Crashes/abnormal termination | Division by zero | Medium |
| Logical | During testing/use | Wrong output | Incorrect formula | Hard |
// Problem 1: Predict the output
int a = 7, b = 3;
printf("%.2f\n", (float)a / b);
printf("%.2f\n", a / (float)b);
printf("%.2f\n", (float)(a / b));
// Problem 2: Fix this code to get accurate division
int marks = 85, total = 100;
float percentage = marks / total * 100; // Currently gives 0.00
// How would you fix this?
// Problem 3: Trace the execution
int x = 3, y = 4, z;
z = ++x + y++;
printf("x=%d, y=%d, z=%d\n", x, y, z);
// Problem 4: Bitwise operations
int num = 12; // Binary: 1100
printf("Left shift by 2: %d\n", num << 2);
printf("Right shift by 1: %d\n", num >> 1);