Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
130 views

Module 1: Running The C Program Using Code::Blocks

The document describes exercises for students to practice using Code::Blocks software to write, compile, and run basic C programs. It includes examples of simple C programs that print text to the screen. Students are instructed to open Code::Blocks, create and save C files, compile and run example programs provided, and predict the output of additional example programs. The objectives are for students to gain experience with the basic structure of C programs and using an IDE to develop and test code.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views

Module 1: Running The C Program Using Code::Blocks

The document describes exercises for students to practice using Code::Blocks software to write, compile, and run basic C programs. It includes examples of simple C programs that print text to the screen. Students are instructed to open Code::Blocks, create and save C files, compile and run example programs provided, and predict the output of additional example programs. The objectives are for students to gain experience with the basic structure of C programs and using an IDE to develop and test code.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1 EET110 BASIC COMPUTER PROGRAMMING MODULE 1

MODULE 1: RUNNING THE C PROGRAM USING CODE::BLOCKS

OBJECTIVES

1. To enter the C source code, compile, and run the C program using CodeBlocks
software.
2. To identify the C program structure.
3. To load, edit and recompile the C program.

EXERCISE 1

1. Open the CodeBlocks software


a. To loading CodeBlocks software, double click icon CodeBlocks at the desktop.

b. The CodeBlocks software will be presented as below:

Figure 1.1

2. Steps to start the new file:

a. Click File icon > New > Empty File (you are now in the text editor).

b. Once the text editor started, automatically it will view an “untitled1”.


2 EET110 BASIC COMPUTER PROGRAMMING MODULE 1

Figure 1.2
c. Type code program as below:

1 // Program 1(a)
2
3 #include <stdio.h>
4
5 int main (void) {
6 printf(“Hello World!”);
7 return 0;
8 }

3. Steps to Create the New Folder:

a. Click File icon > Save file as.. > place your save file in Local Disk D > Create
Folder

b. Rename the New Folder as your Name and Matrix No.

4. Steps to Save the Source File:

a. Go to File menu, select Save file as option.

b. Save the file as welcome.c. Type welcome.c in the box besides the Name
location.
OR
if you want to save in a specified folder, select the folder in the save as dialog
box.
3 EET110 BASIC COMPUTER PROGRAMMING MODULE 1

(e.g. /home/student/foldername), double click the folder icon and then type
welcome.c in the box besides the Name location. Click Save button.
Notes: The filename must be in one word with extension “.c”, for example
Program.c or pgm_1.c.

Figure 1.3
5. Steps to Run and Build a Program:

a. Click Build icon > Build and Run (F9).

b. Command prompt will pop out and display the result of your program.

c. If there is no syntax error, the compilation process is considered successful and


output is displayed.

Figure 1.4
4 EET110 BASIC COMPUTER PROGRAMMING MODULE 1

d. If there are syntax errors, an error message will be displayed in the Compiler
section, and the programmer will edit the code with errors and recompile the
program until successful.

Figure 1.5
5 EET110 BASIC COMPUTER PROGRAMMING MODULE 1

EXERCISE 2

Practice to load existing C source file, edit the content and compile it.

1. Load the CodeBlocks software.


2. Open the existing source file as follows:
a. Click on the File menu and choose Open or Ctrl+O

Figure 1.6
b. Set the appropriate File location in Look in the field and select the appropriate
file name to be open.
c. Change and add a new line of code:
1 // Program 1(b)
2
3 #include <stdio.h>
4
5 int main (void) {
6 printf(“Hello World!\n”);
7 printf(“Welcome to UniMAP”);
8 return 0;
9 }

d. Compile and run the C program.


e. Show the output of the program.
3. Closed the C program.
6 EET110 BASIC COMPUTER PROGRAMMING MODULE 1

EXERCISE 3

1. The annotation of Program 1(b) will be as follows:


1 // Program 1(b)
2
3 #include <stdio.h>
4
5 int main (void) {
6 printf(“Hello World!\n”);
7 printf(“Welcome to UniMAP”);
8 return 0;
9 }

a. Line 1 is comment. A comment is a piece of a descriptive text (letters, number


or special symbols) which explains some aspect of a program. Program
comments are considered as non-executable statements that are totally ignored
by the compiler and are only intended for human readers. The symbol “//” is
used to begin the comment in a line.
b. Lines 3 are the pre-processor directive #include that include the contents
of the library file, named stdio.h in the program. stdio.h is a standard C
header file and contains definitions for input and output, such as printf and
scanf.
c. Line 5 defines a function called main that begins the program execution. The
brackets without any word indicate the main function have no parameters. The
return type for main is int (i.e., an integer number).
d. Line 5 also contains an open bracket “{” that marks the beginning of the body
of the main function.
e. Line 6 is a statement. The end of a statement is always marked with a semicolon
“:”. This statement causes the string “Hello World!” to be sent to the
printf output stream. A string is any sequence of characters enclosed in
double-quotes. The last character in this string “/n” is a newline character
which returns the cursor to the new line. Then string “Welcome to
UniMAP” to be sent to the output stream. A stream is an object which performs
input or output. printf is the standard output stream in C (standard output
usually means your computer monitor screen).
7 EET110 BASIC COMPUTER PROGRAMMING MODULE 1

f. Line 8 is the return statement that the control of the program to the operating
system. It also determines the value of the return type that matches the main
function.
g. Line 9 is a close brace “}” that marks the end of the body of main.

2. Given Program 1(c) below:


1 /* Program 1(c)
2 Topic: age calculator */
3
4 #include <stdio.h>
5
6 int main (void) {
7 const int currentYear = 2021;
8 char name[50];
9 int birthYear, age;
10
11 printf(“Insert your name : ”);
12 scanf(“%s”, &name);
13
14 printf(“Insert your year of birth : ”);
15 scanf(“%d”, &birthYear);
16
17 age = currentYear – birthyear;
18
19 printf(“Your name : %s”, name);
20 printf(“\nYour age : %d”, age, “ year”);
21
22 return 0;
23 }

Type, save, compile, and run Program 1(c) above. What will be the output of
the program for the given input?

3. Given program 1(d) below:


1 // Program 1(d)
2
3 #include <stdio.h>
4
5 int main (void) {
6 int workDays;
7 float workHour, payrate, weeklyPay;
8 EET110 BASIC COMPUTER PROGRAMMING MODULE 1

8
9 workDays = 5;
10 workHour = 6.5;
11 payrate = 20.50;
12
13 weeklyPay = workDays * workHour * payrate;
14
15 printf(“Weekly Pay : %f”, weeklyPay);
16
17 return 0;
18 }

Type, save, compile, and run Program 1(d) above. What will be the output of
the program for the given input?

You might also like