[go: up one dir, main page]

0% found this document useful (0 votes)
2 views6 pages

disecrete mathematics

The document provides a detailed explanation of C programming errors, categorizing them into syntax errors, run-time errors, and semantic errors, along with examples and solutions for each type. It also outlines various types of operators in C programming, including arithmetic, relational, logical, assignment, bitwise, and ternary operators, with examples demonstrating their usage. The content serves as an educational resource for understanding common programming issues and operator functionalities in C.

Uploaded by

leniustizanga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

disecrete mathematics

The document provides a detailed explanation of C programming errors, categorizing them into syntax errors, run-time errors, and semantic errors, along with examples and solutions for each type. It also outlines various types of operators in C programming, including arithmetic, relational, logical, assignment, bitwise, and ternary operators, with examples demonstrating their usage. The content serves as an educational resource for understanding common programming issues and operator functionalities in C.

Uploaded by

leniustizanga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

BACHELOR DEGREE OF INFORMATION AND TECHNOLOGY

BIT 1
STREAM “E”

REGISTRATION NUMBER :03.3107.01.01.2024


NAME :MAGDLENA SHIFTIAELI NYITI
MODULE : PROGRAMING IN C
LECTURE`S NAME : JOSEPH HAULE.
TASK :INDIVIDUAL ASSIGNMENT.
Question 1; to explain in details the types of C programming error
programming error refers to a problem in the code that prevents it from compiling, running
correctly, or producing the expected output. Programmers encounter different kind of errors
and those errors can be categorized into several types includes. Syntax error, run time error
and semantic error thus the following are deep details of those errors.
1. Syntax Errors;
A syntax error occurs when the rules or grammar of the programming language are violated.
This type of error is usually detected by the compiler or interpreter before the program runs.
Common syntax errors include missing semicolons, incorrect punctuation, or wrong
keywords.

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;
}

QUESTION 2; To explain types of operators in C programming.


1. Arithmetic Operators; the are are symbols used to perform mathematical operations on
numerical values. In C and most other programming languages, arithmetic operators allow
you to carry out addition, subtraction, multiplication, division, and more. The following are
the arithmetic operators and their function in C program.

i. + Addition; this is used to adds two numbers.


ii. - Subtraction; subtracts the second operand from the first.
iii. * Multiplication; multiplies two numbers.
iv. / Division divides the first number by the second.
v. % Modulus (remainder) gives the remainder when the first number is
divided by the second.

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;
}

2. Relational Operators;These are used in programming to compare two values or variables.


These operators evaluate the relationship between the values and return a Boolean result:
either true (non- zero) or false (zero). Relational operators are essential for decision-making
and controlling the flow of programs. Relational operators includes;

i. == this is used to checks if two values are equal.


ii. != tjis is used to checks if two values are not equal.
iii. > this checks if the left operand is greater than the right operand.
iv. < this is used to check if the left operand is less than the right operand.
v. >=This is used to checks if the left operand is greater than or equal to the right operand.
vi. <= this is used to check if the left operand is less than or equal to the right operand.

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;
}

3. Logical Operators; used to perform logical operations on expressions or conditions. These


operators are primarily used in decision-making to combine or modify the results of relational
or other logical expressions. Logical operators return either true (1) or false (0).they include;

i. && Logical AND returns true if both conditions are true.


ii. || Logical OR returns true if at least one condition is true.
iii. ! Logical NOT negates the condition, turning true to false and vice versa.

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;

i. = Assignment; this assigns a value to a variable.


ii. += Add and assign; Is shorthand operators for performing arithmetic operation and
assignment in a single step.
iii. -= Subtract and assign; Is shorthand operators for performing an arithmetic operation
and assignment in a single step.
iv. *= Multiply and assign; Is shorthand operators for performing an arithmetic operation
and assignment in a single step.
v. /= Divide and assign; Is shorthand operators for performing an arithmetic operation
and assignment in a single step.
vi. %= Modulus and assign; Is shorthand operators for performing an arithmetic operation
and assignment in a single step.

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.

Syntax: expression1 : expression2;


 If the condition is true, expression1 is evaluated; otherwise, expression2 is evaluated.

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”.

You might also like