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
Using Programming Tools to Write, Compile, Run and Debug a Simple C Program
Before you can write and run a C program, you need a set of programming tools. These include a text editor (where you type your code) and a compiler (which translates your code into language the computer can understand). Together, these tools form a development environment that allows you to create, test, and fix programs.
To start programming in C, you need two essential tools installed on your computer:
1. Text Editor
A text editor is where you write your C source code. Common text editors include:
- Windows: Notepad++, Visual Studio Code (VS Code), Sublime Text
- Linux/macOS: vim, nano, VS Code, gedit
- Cross-platform: VS Code, Atom, Sublime Text
The files you create are called source files and should have the .c extension (for example, program.c).
2. C Compiler
The computer cannot understand your C code directly. A compiler translates your human-readable source code into machine language (binary code) that the computer can execute. The most common free C compiler is GCC (GNU Compiler Collection).
On Windows, you can install MinGW-w64 to get GCC. After installation, you must add the compiler's folder to your system's PATH environment variable so that you can run it from any folder using the command prompt.
To verify your compiler is installed correctly, open Command Prompt and type:
gcc --version
If installed properly, this command will display the GCC version number.
A C program has a specific structure. Every program must have a main() function where execution begins. Here is a simple example that prints a greeting:
#include <stdio.h>
int main() {
printf("Habari, Tanzania!\n");
return 0;
}
Explanation of the code:
#include <stdio.h>— tells the compiler to include the standard input/output libraryint main()— the main function where the program startsprintf("Habari, Tanzania!\n");— prints text to the screen;\ncreates a new linereturn 0;— tells the operating system the program finished successfully
To write this program:
- Open your text editor (like Notepad++ or VS Code)
- Type the code exactly as shown above
- Save the file as
habari.cin a folder you can easily find
After writing the code, you must compile it to create an executable program. Compilation checks your code for errors and translates it into machine language.
Steps to compile:
- Open Command Prompt (Windows) or Terminal (Linux/macOS)
- Navigate to the folder where you saved your file using the
cdcommand - Type the following command to compile:
gcc habari.c -o habari
This command tells GCC to:
- Take
habari.cas input - Create an executable file named
habari(orhabari.exeon Windows)
If your code has no errors, the compilation will succeed and create the executable file. If there are errors, the compiler will display error messages showing what is wrong and on which line.
After successful compilation, you can run your program:
On Windows:
habari.exe
On Linux/macOS:
./habari
The output will display:
Habari, Tanzania!
When writing programs, you will encounter errors. There are three main types:
1. Syntax Errors
These occur when you break the rules of C, such as forgetting a semicolon or misspelling a keyword.
Example of syntax error:
#include <stdio.h>
int main() {
printf("Hello World\n") // Missing semicolon
return 0;
}
Error message: expected ';' before 'return'
Fix: Add a semicolon at the end of the printf line.
2. Logical Errors
These occur when your program runs but gives wrong results due to incorrect logic.
Example:
int a = 5, b = 2;
int result = a - b; // You wanted addition but wrote subtraction
Fix: Change the operator to + if you want to add.
3. Runtime Errors
These occur while the program is running, such as dividing by zero.
Example:
int a = 5, b = 0;
int result = a / b; // Division by zero causes crash
Fix: Check if the divisor is zero before dividing.
When your program does not work correctly, use these simple debugging methods:
-
Read error messages carefully — The compiler usually tells you where the error is and what type of error it is.
-
Use print statements — Add
printf()statements to check if variables have the values you expect at different points in your program. -
Check your code line by line — Compare your code with working examples from the textbook.
The programming workflow follows these steps:
- Write your C code in a text editor and save it with
.cextension - Compile the program using the
gcccommand - Run the compiled executable to see the output
- Debug any errors that appear by reading error messages and fixing the code
- Repeat the process until your program works correctly
In Tanzania, programming tools are used in many everyday situations. For example, a small shop owner in Dar es Salaam could use a simple C program to calculate the total price of items, apply discounts, and generate a receipt. By writing and running this program on their computer, they can quickly process customer orders and avoid calculation mistakes when adding up prices in Tanzanian shillings.
Swali
Which software tool is used to translate C source code into machine code that the computer can execute?
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