TP0_Answers (354335567)
TP0_Answers (354335567)
TP0_Answers (354335567)
The C programming language is one of the most influential and widely used programming languages
in the world. Developed in the early 1970s by Dennis Ritchie at Bell Labs, C was initially designed
for system programming and to develop the UNIX operating system. However, its versatility and
efficiency have made it a foundational language used in a wide range of applications, from operating
systems to embedded systems and game development.
C is a procedural language that provides low-level access to memory, which makes it ideal for writing
software that needs to be close to the hardware. It also features a relatively simple set of keywords,
allowing for the creation of highly efficient code. Despite its simplicity, C provides powerful
capabilities such as pointers, dynamic memory management, and bit-level manipulation.
One of the key strengths of C is its portability. Programs written in C can often be compiled and run
on different machines with minimal or no modification, which made it a preferred language for systems
that need to run on multiple platforms.
One of the most powerful features of C is its ability to provide low-level access to memory. This means
that C allows direct manipulation of memory addresses and bits, which is crucial in systems
programming, embedded systems, and other areas where efficiency and hardware control are critical.
The key tool that enables this in C is pointers.
C Portability
One of the key strengths of C as a programming language is its portability. Portability refers to the
ability to run C programs on different platforms (such as different operating systems or hardware
architectures) with minimal or no changes to the source code. This characteristic has contributed to
C's widespread use across various systems, from embedded devices to supercomputers.
1
Introduction to Code::Blocks: Setting Up and Running a C Project
The purpose of this lab is to o introduce you to the Code::Blocks development environment.
➢ To launch Code::Blocks, click on the shortcut available on your desktop or in the Start/-
CodeBlocks menu (or as instructed by your lab assistant).
➢ Choose the project type "Console application" from the list and click "Go".
➢ Give your project a name and choose the directory where it should be saved. Click "Next".
➢ In the compiler selection window, keep the default settings and press "Next".
➢ To exit Code::Blocks, go to the File/Quit menu or use the key combination Ctrl+Q .
Hello Program
Let’s start with a classic. We’ll begin by compiling and running a small C program that
is automatically generated by Code::Blocks when you create a new project. The listing
below shows the content of the main.c file in your project:
8 8}
2
1. Compile and run your project.
2. Modify the program to display the following sentence: "This is my first sentence" instead
of "Hello, World!"
After re-running the program, you'll observe that both statements are printed one after the
other:
This is my first sentence.
This is my second sentence.
• This shows that we can add multiple printf statements to output more lines of text, and the
program will execute them in the order they appear in the code.
• Each statement is executed sequentially, and the output reflects the added message without
changing the overall structure of the program.
\n
\t
3
7. What can you conclude?
Escape sequences like \n (newline) and \t (tab) in C allow us to format the output in a specific
way.
• Using \n moves the text to a new line, separating the two sentences vertically.
• Using \t adds a tab space, separating the sentences horizontally within the same line.
Figure (A) :
#include <stdio.h>
int main() {
// Manually print each row of asterisks
printf("* * * * *\n");
printf("* * * * *\n");
printf("* * * * *\n");
printf("* * * * *\n");
printf("* * * * *\n");
return 0;
}
Figure (B) :
#include <stdio.h>
int main() {
// Manually print each row of the triangle
printf("*\n"); // 1st row
printf("* *\n"); // 2nd row
printf("* * *\n"); // 3rd row
printf("* * * *\n"); // 4th row
printf("* * * * *\n"); // 5th row
return 0;
}
4
Figure (C) :
#include <stdio.h>
int main() {
// Manually print each row of the isosceles triangle
printf(" *\n"); // 1st row (4 spaces before 1 asterisk)
printf(" ***\n"); // 2nd row (3 spaces before 3 asterisks)
printf(" *****\n"); // 3rd row (2 spaces before 5 asterisks)
printf(" *******\n"); // 4th row (1 space before 7 asterisks)
printf("*********\n"); // 5th row (no spaces, 9 asterisks)
return 0;
}
4. Experiment with escape characters found in the table below and complete it with your
observations.
Escape Character Observation