disecrete mathematics
disecrete mathematics
BIT 1
STREAM “E”
Example.
#include <stdio.h>
int main() {
printf("BIT" // Missing closing parenthesis
return 0;
}
In this example, the syntax error occurs because there is a missing closing parenthesis ) in the
“printf” function. The program will not compile and will throw an error.
How to fix: Add the closing parenthesis.
#include <stdio.h>
int main() {
printf("BIT"); // Correct syntax
return 0;
2. Run-time Errors
A run-time error occurs when a program is syntactically correct, but an error occurs while
the program is running. These errors are not detected during the compilation but only when
the program is executed.
Examples include division by zero, invalid memory access, or accessing non-existent files.
Example:
#include <stdio.h>
int main() {
int age[5] = {15, 25, 35, 45, 55};
// Trying to access an index that is out of bounds
printf("Element at index 10: %d\n", age[10]); // Run-time error: Index out of bounds
return 0;
}
How to Fix: To avoid this, we must ensure that array indices stay within valid bounds (0
to 4 in this case):
#include <stdio.h>
int main() {
int age[5] = {15, 25, 35, 45, 55};
// access an index that is on bounds
printf("Element at index 4: %d\n", age[4]); // no error
return 0;
}
3. Semantic Errors
A semantic error, this is error which occurs when the program compiles and runs without
crashing but produces incorrect results due to logic or conceptual mistakes. These errors are
harder to detect because the code runs successfully but does not perform the intended task.
Example.
#include <stdio.h>
int main() {
int a = 5;
int b = 2;
printf("Result: %d", a / b); // Integer division
return 0;
}
The program divides a by b, but the result is 2 instead of 2.5, because both a and b are
integers, and integer division truncates the decimal part. This may not be the intended
behavior, and the result is not what was expected.
How to fix; To get a floating-point result, change the type of a or b to float.
#include <stdio.h>
int main() {
float a = 5.0;
float b = 2.0;
printf("Result: %.2f", a / b); // Correct result with floating-point division
return 0;
}
Example:
#include <stdio.h>
int main() {
int a = 10, b = 5;
printf("Addition: %d\n", a + b); // 10 + 5 = 15
printf("Subtraction: %d\n", a - b); // 10 - 5 = 5
printf("Multiplication: %d\n", a * b); // 10 * 5 = 50
printf("Division: %d\n", a / b); // 10 / 5 = 2
printf("Modulus: %d\n", a % b);
// 10 % 5 = 0
return 0;
}
Example:
#include <stdio.h>
int main() {
int a = 10, b = 5;
printf("a == b: %d\n", a == b); // 0 (false)
printf("a != b: %d\n", a != b); // 1 (true)
printf("a > b: %d\n", a > b); // 1 (true)
printf("a < b: %d\n", a < b); // 0 (false)
printf("a >= b: %d\n", a >= b); // 1 (true)
printf("a <= b: %d\n", a <= b); // 0 (false)
return 0;
}
Example:
#include <stdio.h>
int main() {
int a = 10, b = 5, c = 3;
printf("a > b && b > c: %d\n", (a > b) && (b > c)); // 1 (true)
printf("a > b || b < c: %d\n", (a > b) || (b < c)); // 1 (true)
printf("!(a > b): %d\n", !(a > b));
// 0 (false)
return 0;
}
4. Assignment Operators; used to assign values to variables. The most basic assignment
operator is the equals (=) operator, but C also provides a variety of compound assignment
operators that combine basic arithmetic or bit-wise operations with assignment. These
operators include;
Example:
#include <stdio.h>
int main() {
int a = 5;
a += 3; // a = a + 3 -> a = 8
printf("a += 3: %d\n", a);
a -= 2; // a = a - 2 -> a = 6
printf("a -= 2: %d\n", a);
a *= 4; // a = a * 4 -> a = 24
printf("a *= 4: %d\n", a);
a /= 6; // a = a / 6 -> a = 4
printf("a /= 6: %d\n", a);
a %= 3; // a = a % 3 -> a = 1
printf("a %%= 3: %d\n", a);
return 0;
}
5. Bitwise Operators; these are the operators which operate directly on binary
representations of numbers (bits). They allow manipulation of individual bits in variables,
which is often useful in lowlevel programming tasks like hardware interaction, encryption, or
optimization of computations. These include ;
i. & Bitwise AND this performs a bitwise AND
ii. | Bitwise OR this performs a bitwise OR.
iii. ^ Bitwise XOR this performs a bitwise XOR.
iv. << Left shift this shifts bits to the left
v. >> Right shift shifts bits to the right.
vi. ~ Bitwise NOT this performs a bitwise NOT (flips all bits).
Example:
#include <stdio.h>
int main() {
int a = 5, b = 3; // In binary: a = 0101, b = 0011
printf("a & b: %d\n", a & b); // Bitwise AND -> 0001 (1)
printf("a | b: %d\n", a | b); // Bitwise OR -> 0111 (7)
printf("a ^ b: %d\n", a ^ b); // Bitwise XOR -> 0110 (6)
printf("~a: %d\n", ~a);
// Bitwise NOT -> 1111...1010 (-6)
printf("a << 1: %d\n", a << 1); // Left shift -> 1010 (10)
printf("a >> 1: %d\n", a >> 1); // Right shift -> 0010 (2)
return 0;
}
6. Ternary (Conditional) Operator; shorthand for an if-else statement. It allows for
conditional expressions in a concise and readable manner. The operator evaluates a condition
and returns one of two values based on whether the condition is true or false.
Example:
#include <stdio.h>
int main() {
int a = 10, b = 5;
int max = (a > b) ? a : b; // If a > b, max = a, else max = b
printf("The maximum number is: %d\n", max);
return 0;
}
Explanation: The ternary operator checks if a > b. If true, it assigns “a” to max; otherwise, it
assigns “b”.