Ste 10 Module 2
Ste 10 Module 2
STE ICT – 10
Quarter 1 – Module 2: Computer
Programming with C
Language
Quarter 1 – Module 2: Computer Programming with C Language
First Edition, 2020
Republic Act 8293, section 176 states that: No copyright shall subsist in any work of the
Government of the Philippines. However, prior approval of the government agency or office wherein
the work is created shall be necessary for exploitation of such work for profit. Such agency or office
may, among other things, impose as a condition the payment of royalties.
Borrowed materials (i.e., songs, stories, poems, pictures, photos, brand names, trademarks,
etc.) included in this book are owned by their respective copyright holders. Every effort has been
exerted to locate and seek permission to use these materials from their respective copyright
owners. The publisher and authors do not represent nor claim ownership over them.
As a facilitator you are expected to orient the learners on how to use this
module. You also need to keep track of the learners' progress while allowing them to
manage their own learning. Furthermore, you are expected to encourage and assist the
learners as they do the tasks included in the module.
ii
For the learner:
Welcome to the STE ICT - 10 Self- Learning Module (SLM) on Computer Programming
with C Language!
The hand is one of the most symbolized part of the human body. It is often used to
depict skill, action and purpose. Through our hands we may learn, create and
accomplish. Hence, the hand in this learning resource signifies that you as a learner is
capable and empowered to successfully achieve the relevant competencies and skills
at your own pace and time. Your academic success lies in your own hands!
This module was designed to provide you with fun and meaningful opportunities for
guided and independent learning at your own pace and time. You will be enabled to
process the contents of the learning resource while being an active learner.
a. Use the module with care. Do not put unnecessary mark/s on any part of the
module. Use a separate sheet of paper in answering the exercises.
b. Don’t forget to answer What I Know before moving on to the other activities
included in the module.
c. Read the instructions carefully before doing each task.
d. Observe honesty and integrity in doing the tasks and checking your answers.
e. Finish the task at hand before proceeding to the next.
f. Return this module to your teacher/facilitator once you are through with it.
If you encounter any difficulty in answering the tasks in this module, do not hesitate to
consult your teacher or facilitator. Always bear in mind that you are not alone.
We hope that through this material, you will experience meaningful learning and gain
deep understanding of the relevant competencies. You can do it.
This module was designed and written with you in mind It is here to help you
master the knowledge and skills in Computer Programming with C Language. The scope
of this module permits it to be used in different learning situations. The language used
recognizes the diverse vocabulary level of students. The lessons are arranged to follow
the standard sequence of the course. But the order in which you read them can be
changed to correspond with the textbook you are now using.
iv
2.1: First Code: Hello World
2.2: Variables, Data Types and Constants
2.3: Using printf Function
2.4: Using scanf Function
2.5: Flowchart
v
What I Know
Pretest
TRUE OR FALSE: Identify whether the statement is TRUE or FALSE
13. T 10. A printf() format string specifies how the output is formatted.
14. T 11. Escape Sequence is displayed exactly as entered in the format string.
15. T 12. The scanf() function reads data from the keyboard according to a specified
format and assigns the input data to one or more program variables
13. A flowchart is a picture of the separate steps of a process in sequential order.
14. Program flowchart can help programmers to find the bug in the process before
carrying out.
15. With the help of program flowchart, communicating the logic of a system to all
concerned gets much easier.
1.
6
Lesso
n
First Code: Hello World
1
What’s In
What’s New
C Language was not called C at the beginning. It has been named as C after
passing many stages of evolution. Evolution of C:
7
What’s In
Preparing to Program
When creating a program in C, you should follow a similar sequence of steps:
1. Determine the objective(s) of the program.
2. Determine the methods you want to use in writing the program.
3. Create the program to solve the problem.
4. Run the program to see the results.
8
Step 6. Write the code on the sample below
#include <stdio.h>
int main(void)
{
puts("Hello, World");
return 0;
}
Output
as shown
You can edit the code or you may close the application by going to File >> Quit
Code Explained:
Let's look at this simple program line by line
#include <stdio.h>
This line tells the compiler to include the contents of the standard library header
file stdio.h in the program. Headers are usually files containing function declarations,
macros and data types, and you must include the header file before you use them. This
line includes stdio.h so it can call the function puts().
int main(void)
This line starts the definition of a function. It states the name of the function
(main), the type and number of arguments it expects (void, meaning none), and the
type of value that this function returns (int). Program execution starts in the main()
function.
{
…
}
The curly braces are used in pairs to indicate where a block of code begins and
ends. They can be used in a lot of ways, but in this case they indicate where the
function begins and ends.
puts("Hello, World");
This line calls the puts() function to output text to standard output (the screen,
by default), followed by a newline. The string to be output is included within the
parentheses. "Hello, World" is the string that will be written to the screen. In C, every
string literal value must be inside the double quotes "…". In C programs, every
statement needs to be terminated by a semi-colon (i.e. ;).
return 0;
What’s More
1. Use ______________________________________________________________________________________
10
2. Compile _________________________________________________________________________________
3. Link _____________________________________________________________________________________
4. Execute _________________________________________________________________________________
What I Have
Learned
What are you going to press when you want to run your program?
_______________________________________________________________________
What I Can Do
Assessmen
t
Activity 1
11
Write here the expected result of your program
12
Lesso
n Variables, Data Types and
2 Constants
What’s In
Variable
A variable is a named data storage location in your computer's memory. By using
a variable's name in your program, you are, in effect, referring to the data stored there.
The name can contain letters, digits, and underscore character (_).
The first character of the name must be a letter. The underscore is also a legal
first character, but its use is not recommended.
Case matters (that is, upper- and lowercase letters). Thus, the names count and
Count refer to two different variables.
C keywords can't be used as variable names. A keyword is a word that is part of
the C language.
The following list contains some examples of legal and illegal C variable names:
Data Types
Data types specify how we enter data into our programs and what type of data
we enter. C language has some predefined set of data types to handle various kinds of
data that we can use in our program. These datatypes have different storage
capacities.
C language supports 2 different type of data types:
1. Primary data types - These are fundamental data types in C namely
integer(int), floating point(float), character(char) and void.
13
2. Derived data types - Derived data types are nothing but primary
datatypes but a little twisted or grouped together like array, structure,
union and pointer.
Data type determines the type of data a variable will hold. If a variable x is
declared as int. it means x can hold only integer values. Every variable which is used in
the program must be declared as what data type it is.
Variable Declaration
A variable declaration tells the compiler the name and type of a variable and
optionally initializes the variable to a specific value. If your program attempts to use a
variable that hasn't been declared, the compiler generates an error message. A
variable declaration has the following form:
14
typename varname;
typename specifies the variable type and must be one of the keywords listed in the
table above
varname is the variable name, which must follow the rules mentioned earlier. You can
declare multiple variables of the same type on one line by separating the variable
names with commas:
Constants
Like a variable, a constant is a data storage location used by your program.
Unlike a variable, the value stored in a constant can't be changed during program
execution.
Literal Constants - A literal constant is a value that is typed directly into the
source code wherever it is needed. Here are two examples:
int count = 20;
float tax_rate = 0.28;
15
Null Statements
If you place a semicolon by itself on a line, you create a null statement--a
statement that doesn't perform any action. This is perfectly legal in C.
Compound Statements
A compound statement, also called a block, is a group of two or more C
statements enclosed in braces. Here's an example of a block:
{
printf("Hello, ");
printf("world!");
}
Expressions
In C, an expression is anything that evaluates to a numeric value. C expressions
come in all levels of complexity.
Simple Expressions - The simplest C expression consists of a single item: a
simple variable, literal constant, or symbolic constant. Here are four expressions:
Expression Description
PI A symbolic constant (defined in the program)
20 A literal constant
Rate A variable
-1.25 Another literal constant
16
Operators
An operator is a symbol that instructs C to perform some operation, or action,
on one or more operands. An operand is something that an operator acts on. In C, all
operands are expressions. C operators fall into several categories:
17
0?" An expression containing a relational operator evaluates to either true (1) or
false (0).
Logical Operators - C's logical operators let you combine two or more
relational expressions into a single expression that evaluates to either true or
false.
What’s More
Naming Variables
Direction: List at least 5 sample variable names based on the rules stated.
1. Valid variable names
18
i.
ii.
iii.
iv.
v.
2. Invalid variable names
i.
ii.
iii.
iv.
v.
Complete Me
Direction: Complete the statements based on the different type of data types
1. Primary data types are __________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
2. Derived data types are nothing
__________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
What I Can Do
19
Assessmen
t
Activity 2
Direction: Unscramble each of the clue words. Copy the letters in the numbered cells to other
cells with the same number.
20
Lesso
n
Using printf Function
3
What’s In
A printf() format string specifies how the output is formatted. Here are the three
possible components of a format string:
What’s More
Complete Me
Direction: Complete the following statements
1. Literal Text is
_____________________________________________________________________________
2. Escape sequence provides
_______________________________________________________________
3. Conversion specifier consists of
_________________________________________________________
4. printf() specifies _________________________________________________________________________
22
_______________________________________________________________________
_______________________________________________________________________
23
What I Can Do
24
Assessment
Activity 3
Direction: Write a source code for a program that displays your complete name, age, present
address, and zip code in this format:
25
Lesso
n
Using scanf Function
4
What’s In
The scanf() function reads data from the keyboard according to a specified
format and assigns the input data to one or more program variables. Like printf(),
scanf() uses a format string to describe the format of the input. The format string
utilizes the same conversion specifiers as the printf() function. For example, the
statement
scanf("%d", &x);
reads a decimal integer from the keyboard and assigns it to the integer variable x.
Likewise, the following statement reads a floating-point value from the keyboard and
assigns it to the variable rate:
scanf("%f", &rate);
The & symbol is C's address-of operator is used to return the address of the
variable.
A single scanf() can input more than one value if you include multiple conversion
specifiers in the format string and variable names (again, each preceded by & in the
argument list). The following statement inputs an integer value and a floating-point
value and assigns them to the variables x and rate, respectively:
This gives you considerable flexibility. In response to the preceding scanf(), you could
enter
10 12.45
or this:
10
12.45
As long as there's some white space between values, scanf() can assign each value to
its variable.
26
27
#include <stdio.h>
main()
{
float y;
int x;
puts( "Enter a float, then an int" );
scanf( "%f %d", &y, &x);
printf( "\nYou entered %f and %d ", y, x );
return 0;
}
What’s More
Complete Me. Complete the statement and write your answer in a separate page.
1. The scanf() function _____________________________________________________________________
_____________________________________________________________________________________________
2. The & symbol is __________________________________________________________________________
_____________________________________________________________________________________________
3. White space can be ______________________________________________________________________
_____________________________________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
What I Can Do
Essay
Direction: In a separate paper, compare the functions printf() and scanf(). Your answer should
not be less than 50 words.
28
Assessment
Activity 4
Direction: Write a source code for a program that performs the following:
5. Accept the month of birth in numeric form
6. Accept the day of birth in numeric form
7. Accept the year of birth in numeric form
8. Display the result in month/day/year format
Sample Output:
Enter your birthday details
Month: 12
Day: 20
Year: 2000
29
Lesso
n
Flowchart
5
What’s In
A flowchart is a picture of the separate steps of a process in sequential order. It is a generic tool
that can be adapted for a wide variety of purposes, and can be used to describe various processes, such
as a manufacturing process, an administrative or service process, or a project plan. It's a
common process analysis tool and one of the seven basic quality tools.
Program flowchart is a diagram that uses a set of standard graphic symbols to represent the
sequence of coded instructions fed into a computer, enabling it to perform specified logical and
arithmetical operations. It is a great tool to improve work efficiency.
Flowchart Symbols
30
31
Benefits of Program Flowchart
The advantages of program flowchart are as follows:
Program flowchart can help programmers to find the bug in the process before
carrying out.
It works as a blueprint when analyzing the systems and developing programs,
which makes coding more efficient.
It improves programmers’ efficiency in maintaining the operating program.
With the help of program flowchart, communicating the logic of a system to all
concerned gets much easier.
Start
Read gender
Is Yes Print
gender == M
? “user is male”
No
Print
“user is female”
End
Sample Output
Gender: M
Male
Hint: Use relational operator for creating condition in the decision symbol.
32
What’s More
1. A flowchart is ____________________________________________________________________________________
2. A program flowchart is a _______________________________________________________________________
_____________________________________________________________________________________________________
_____________________________________________________________________________________________________
3. Terminal symbol is a ____________________________________________________________________________
4. Decision symbol is a
______________________________________________________________________________
5. Off page connector symbol is a _________________________________________________________________
_____________________________________________________________________________________________________
What I Have
Learned
Comparison: Compare the following flowchart symbols. Use a separate page for your answer.
33
What I Can Do
Activity 5
Scenario: Identify whether the grade is passed or failed based on the user’s input.
Start
No
Yes
End
Sample Output
Grade: 79
Passed
34
Assessment
Post Test
Multiple Choice: Choose the letter of the best answer. Write the chosen letter on a
separate sheet of paper.
1. This is where the programmer writes the source code for the program
a. Editor b. Compiler c. Link d. Program
2. A part of the C development cycle where the errors are found
a. Source code b. Program c. Compile d. Link
3. It contains function declaration, data types and macros
a. Header b. Footer c. Body d. Main
4. This function provides text output in a standard form followed by a new line
a. printf() b. scanf() c. puts() d. getch()
5. The return value equivalent to 0 mean that the program exited
a. Successfully b. Undefined c. With errors d. Nothing
6. It is a named data storage location in your computer's memory.
a. Expression b. Variable c. Operators d. Statement
7. Which of the following cannot be part of the variable name?
a. Letter b. Digits c. Underscore d. Dollar Sign
8. It specifies how data are entered into the program
a. Variable b. Expression c. Data Types d. Operators
9. It is similar to variable but with unchangeable stored value
a. Variable b. Operator c. Constant d. Typename
10.It is a complete direction instructing the computer to carry out some task
a. White Space b. Statement c. Expression d. Operators
11. The term for tabs and blank lines in source code
a. White Space b. Statement c. Expression d. Operators
12. A symbol that instructs C to perform some operation or action
a. White Space b. Statement c. Expression d. Operators
13. It is a string function that specifies how the output is formatted
a. printf() b. scanf() c. puts() d. getch()
14. A function that reads data from keyboard according to a specified format
a. printf() b. scanf() c. puts() d. getch()
15. A picture of separate steps of process in sequential order
a. Function b. Flowchart c. Main d. Loop
35
Additional Activities
2. Expressions –
3. Operators –
4. Statements –
5. Flowcharts –
36
37
DISCLAIMER