Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

TP0_Answers (354335567)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Larbi Tebessi University- Tebessa

Department of mathematics and computer science, L1

Algorithms and data structure

Lab0: Getting Started with C


Programming
C Programming Language

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.

Explanation of some terms


Procedural language

C is known as a procedural programming language, which means it follows a structured approach,


where the program is divided into functions (or procedures). Each function is a sequence of
instructions that performs a specific task. In procedural programming, emphasis is placed on functions
and the flow of control within the program, rather than on data.

Low-level Access to Memory in C

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

➢ Once you have started Code::Blocks, select "Create a new project" or go to


File/New/Project.

➢ 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".

➢ Select the C language and click "Finish".


➢ In the left-hand panel under "Projects", expand the project’s file structure by clicking the
small "+" sign to display the list of project files. You should have at least one main.c file
with a little bit of source code already in it. You can open the main.c file by double-clicking
on it.

➢ To save a file, go to the File/Save menu or press Ctrl+S .

➢ To open a file (or project), go to the File/Open... menu or press Ctrl+O.

➢ To compile a C program (i.e., generate the executable program), go to the Build/Build


menu or press Ctrl+F9 .

➢ To run a program, go to the Build/Run menu or press Ctrl+F10.

➢ For assistance, go to the Help/CodeBlocks menu. To get help on an instruction appearing


in a script, place the mouse cursor on it and go to the Help/CodeBlocks menu.

➢ 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:

Listing 1: First C Program


1# include < stdio .h >
2 # include < stdlib .h >
3
4 int main ()
5 {
6 printf (" Hello world !\n") ;
7 return 0;

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!"

3. What do you notice?

After modifying the program :


• We notice that the output changes from "Hello, world!" to "This is my first sentence."
• This demonstrates that by simply altering the printf statement, we can easily change the
program's behavior, specifically what it prints to the console.
• The structure of the program remains the same, but the output reflects the new text.

4. Add another statement to display "This is my second sentence."

5. Re-run the program. What do you observe?

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.

6. Separate the two sentences using \n and then \t.

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

8. Modify the program to draw

a) a solid square of asterisks (Figure A).

b) a right-angled triangle of asterisks (Figure B).

c) an isosceles triangle of asterisks (Figure C).

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

1. Create a new project.

2. Modify the display statement in the main.c file as follows:


1 # include < stdio .h >
2
3 int main ()
4{
5 printf (" Hello \n Toto ") ;
6 return 0;
7 }

3. Compile and run your project.

4. Experiment with escape characters found in the table below and complete it with your
observations.
Escape Character Observation

\n Inserts a new line. Moves the cursor to the next line.


Inserts a tab space (horizontal tab). It creates a larger gap between
\t
words or text.
\b Performs a backspace, deleting the previous character.
Moves the cursor to the beginning of the current line (carriage
\r
return). Subsequent text overwrites the existing text in the line.
\\ Prints a single backslash (\).
Represents the null character, used to indicate the end of a string. It
\0
doesn't produce visible output in printf.
\a Produces an audible alert or beep sound, if supported by the system.

You might also like