Mada za sehemu hiiDemonstrate an understanding of basic principles of computer programming (using an appropriate structured programming language such as C, Python, etc.)Mada 7
- Describe the concept of programming language (categories, paradigm, generic structures) and programming tools (compiler/interpreter, text editor, IDE, debugger)
- Describe programming tools (compiler/interpreter, text editor, IDE, debugger)
- Install and configure the selected programming language (compiler/interpreter, text editor, IDE, debugger)
- Use programming tools of selected programming language to write a program (compile/run and debug a simple program)
- Use variables, constants, and data types of a selected programming language in a program (operators and expressions)
- Use syntax and constructs of the selected programming language to write programs with branching
- Debug computer programs
Variables, Constants, and Data Types in C Programming
A variable is a named storage location in memory that holds a value which can change during program execution, while a constant is a value that remains fixed throughout the program. In C programming, we use different data types to specify what kind of value a variable can store, such as whole numbers, decimal numbers, or single characters.
A variable is like a labelled box in the computer's memory where we store data. Each variable has a name that we use to find and use it.
Declaring a Variable
Before using a variable, we must declare it by specifying its data type and name:
int age; // Declares an integer variable named 'age'
float height; // Declares a float variable named 'height'
char grade; // Declares a character variable named 'grade'
Initialising a Variable
Initialising means giving a variable a value when we declare it:
int age = 16; // Declares and initializes 'age' with 16
float height = 5.9; // Declares and initializes 'height' with 5.9
char grade = 'A'; // Declares and initializes 'grade' with 'A'
Updating a Variable
We can change the value of a variable after it has been declared:
int age = 15; // age is 15
age = 20; // Now age is updated to 20
C provides four fundamental data types for storing different kinds of values:
| Data Type | Purpose | Example |
|---|---|---|
| int | Stores whole numbers | int num = 10; |
| float | Stores decimal numbers (single precision) | float pi = 3.14; |
| double | Stores decimal numbers (higher precision) | double price = 1250.50; |
| char | Stores a single character | char letter = 'B'; |
Choosing the Right Data Type
- Use int when storing whole numbers like age, quantity, or count
- Use float or double when storing numbers with decimals like price or temperature
- Use char when storing single characters like grades or initials
A constant is a value that cannot be changed after it is set. We define constants using either the const keyword or the #define macro.
Using the const Keyword
const int MAX_SCORE = 100; // Cannot be changed later
const float PI = 3.1415; // Value of PI is locked
Using #define Macro
#define PI 3.1415 // Defines a constant without using memory
Constants are useful for values that should never change, such as the value of Pi or maximum allowed scores.
An operator is a symbol that tells the compiler to perform specific mathematical or logical operations. An expression combines variables, constants, and operators to produce a value.
Arithmetic Operators
Arithmetic operators perform mathematical calculations:
| Operator | Description | Example (a=10, b=3) |
|---|---|---|
+ | Addition | a + b = 13 |
- | Subtraction | a - b = 7 |
* | Multiplication | a * b = 30 |
/ | Division | a / b = 3 |
% | Modulus (remainder) | a % b = 1 |
Relational Operators
Relational operators compare two values and return true (1) or false (0):
| Operator | Description | Example (a=5, b=8) |
|---|---|---|
== | Equal to | a == b is false (0) |
!= | Not equal to | a != b is true (1) |
> | Greater than | a > b is false (0) |
< | Less than | a < b is true (1) |
>= | Greater than or equal | a >= b is false (0) |
<= | Less than or equal | a <= b is true (1) |
Logical Operators
Logical operators combine multiple conditions:
| Operator | Description | Example |
|---|---|---|
&& | Logical AND (both must be true) | (5 > 3 && 10 < 20) is true |
| ` | ` | |
! | Logical NOT (reverses the result) | !(5 == 5) is false |
Assignment Operators
Assignment operators assign values to variables and can combine with arithmetic:
| Operator | Example | Equivalent To |
|---|---|---|
= | a = b | Assigns b to a |
+= | a += b | a = a + b |
-= | a -= b | a = a - b |
*= | a *= b | a = a * b |
/= | a /= b | a = a / b |
Increment and Decrement Operators
These operators increase or decrease a variable's value by 1:
int x = 5;
x++; // x is now 6 (postfix increment)
++x; // x is now 7 (prefix increment)
x--; // x is now 6 (postfix decrement)
--x; // x is now 5 (prefix decrement)
When an expression has multiple operators, C follows precedence rules to determine which operation happens first. Multiplication and division (*, /, %) have higher precedence than addition and subtraction (+, -).
int result = 8 + 4 * 3; // Result is 20, not 36
// Because 4 * 3 is calculated first
Use parentheses () to force a specific order:
int result = (8 + 4) * 3; // Result is 36
The following program demonstrates variables, data types, constants, and operators working together:
#include <stdio.h>
#define PI 3.1416 // Defining a constant using #define
int main() {
// Declaring and initialising variables
int age = 16;
float height = 5.9;
char grade = 'A';
const int MAX_SCORE = 100; // Constant using 'const'
// Using arithmetic operators
int sum = age + 10; // Addition
int product = age * 2; // Multiplication
// Using relational operators
if (age >= 18) {
printf("Adult\n");
} else {
printf("Minor\n");
}
// Printing output
printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Grade: %c\n", grade);
printf("Max Score: %d\n", MAX_SCORE);
printf("Sum: %d\n", sum);
printf("Product: %d\n", product);
return 0;
}
Output:
Minor
Age: 16
Height: 5.9
Grade: A
Max Score: 100
Sum: 26
Product: 32
In everyday life, variables and data types are used in many practical situations in Tanzania. For example, when a shopkeeper uses a simple program to calculate the total price of items, they use int variables for quantities and float variables for prices in Tanzanian shillings (TZS). The program might calculate the total using arithmetic operators like multiplication (*) for quantities and addition (+) for multiple items, then use relational operators to check if the customer gets a discount for purchases above TZS 50,000. This same principle applies to mobile money transactions, school grade calculations, and market inventory management.
Swali
Which type of variable in C is only accessible within the function where it is declared?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuuliza