0% found this document useful (0 votes)
62 views40 pages

Ste 10 Module 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views40 pages

Ste 10 Module 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

10

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.

Published by: Department of Education – SCHOOLS DIVISION OF CAPIZ


Schools Division Superintendent: Salvador O. Ochavo, Jr.
Assistant Schools Division Superintendent: Nicasio S. Frio

Development Team of the Module

Writer: John C. Camacho, Erden D. Gentolea, & TeeJe Allen Doroteo


Editors and Reviewers:
Evelyn Callar, Rossinni G. Medel, Christian Oropeo, Nilda Gallardo, Mark Villaruz,
Reynold Baes, Rechille Ann Denisado, Rezyl Fatima Lamier, Ethyl Lopez, Darrel
Duran, Rebecca S. Harion, Rodolyn Pemintel, Julie Magallanez, Suzette Bagares,
Sheila Marie F. Bergancia, Charmane Kae B. Fagutao, Dalene Dice, Louchemie D.
Corpin, Judith O. Betita, Chimene Subere
Illustrator: Ivy F. Faulve
Layout Artist: Julius E. Ubas
Management Team:
Salvador O. Ochavo, Jr.
Nicasio S. Frio
Segundina F. Dollete
Shirley A. De Juan
Rolando B. Jamora

Printed in the Philippines by Department of Education – SCHOOLS DIVISION OF CAPIZ


Office Address: Banica, Roxas City
Telephone No.: (036) 6210-974
10
STE ICT – 10
Quarter 1 – Module 2: Computer
Programming with C Language
Introductory Message
For the facilitator:

Welcome to the STE ICT - 10 Self- Learning Module (SLM) on Computer


Programming with C Language!
This module was collaboratively designed, developed and reviewed by educators
both from public and private institutions to assist you, the teacher or facilitator in
helping the learners meet the standards set by the K to 12 Curriculum while
overcoming their personal, social, and economic constraints in schooling.
This learning resource hopes to engage the learners into guided and
independent learning activities at their own pace and time. Furthermore, this also aims
to help learners acquire the needed 21st century skills while taking into consideration
their needs and circumstances.
In addition to the material in the main text, you will also see this box in the body
of the module:

Notes to the Teacher


This contains helpful tips or strategies
that will help you in guiding the learners.

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.

This module has the following parts and corresponding icons:


This will give you an idea of the skills or
What I Need to competencies you are expected to learn in
Know the module.

This part includes an activity that aims to


What I Know check what you already know about the
lesson to take. If you get all the answers
correct (100%), you may decide to skip this
module.

This is a brief drill or review to help you link


What’s In
the current lesson with the previous one.

In this portion, the new lesson will be


What’s New introduced to you in various ways such as
a story, a song, a poem, a problem opener,
an activity or a situation.

This section provides a brief discussion of


What is It the lesson. This aims to help you discover
and understand new concepts and skills.

This comprises activities for independent


What’s More practice to solidify your understanding and
skills of the topic. You may check the
answers to the exercises using the Answer
Key at the end of the module.

This includes questions or blank


What I Have sentence/paragraph to be filled in to
Learned process what you learned from the lesson.

This section provides an activity which will


What I Can Do help you transfer your new knowledge or
skill into real life situations or concerns.

This is a task which aims to evaluate your


iii
Assessment level of mastery in achieving the learning
competency.

In this portion, another activity will be


Additional given to you to enrich your knowledge or
Activities skill of the lesson learned. This also tends
to retention of learned concepts.

This contains answers to all activities in the


Answer Key module.

At the end of this module you will also find:

References This is a list of all sources used in


developing this module.

The following are some reminders in using this module:

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.

What I Need to Know

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.

The module mainly focuses one lesson:


Introduction to Computer Programming with C Language

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

Learning Competencies and codes:


Code:
After going through this module, you are expected to:
1. identify the basic keywords used in C Language
2. create a program by compiling source code
3. familiarize with variables, data types and constants
4. create program flowchart based on scenario

v
What I Know
Pretest
TRUE OR FALSE: Identify whether the statement is TRUE or FALSE

1. Programmers uses an editor to write a source code.


1. T
2. Compiler is a program used to create an executable file
2. F
3. Linker is a program that identifies whether the source code contains an error.
3. F
4. T 4. To run the program in a Turbo C++ editor, the user should press Ctrl+F9
5. T 5. Headers are usually files containing function declarations, macros and data
6. F types
7. T 6. An expression is a named data storage location in your computer's memory.
8. T 7. The first character of the variable name must be a letter
9. T 8. Data types specify how we enter data into our programs and what type of data we
10. T
enter.
11. F
12. T 9. A symbolic constant is a constant that is represented by a name

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

This lesson provides the introduction in C programming and common


terminologies. This module also introduces the use of program flowchart
and ways to interpret it.

Notes to the Teacher


Ask the students what are the parts of the Turbo C++ Editor

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:

ALGO -> BCPL -> B -> Tradition C -> K&R C


-> ANSI C -> ANSI/ISO C -> C99.

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.

The C Development Cycle


Step 1. Use an editor to write your source code. By tradition, C source code files
have the extension .C (for example, MYPROG.C, DATABASE.C, and so on).
Step 2. Compile the program using a compiler. If the compiler doesn't find any
errors in the program, it produces an object file. The compiler produces
object files with an .OBJ extension and the same name as the source code
file (for example, MYPROG.C compiles to MYPROG.OBJ). If the compiler finds
errors, it reports them. You must return to step 1 to make corrections in your
source code.
Step 3. Link the program using a linker. If no errors occur, the linker produces an
executable program located in a disk file with an .EXE extension and the
same name as the object file (for example, MYPROG.OBJ is linked to create
MYPROG.EXE).
Step 4. Execute the program. You should test to determine whether it functions
properly. If not, start again with step 1 and make modifications and additions
to your source code.

Creating your first code. - Hello World

To create a simple C program which prints "Hello, World" on the screen:


Step 1. Open Turbo C++ on your desktop with the icon
Step 2. Select New Project
Step 3. Label your project as Hello and click save
Step 4. In the Turbo C++, click Options >> Directories
Step 5. Setup Directory as shown in the figure and click OK

8
Step 6. Write the code on the sample below

#include <stdio.h>
int main(void)
{
puts("Hello, World");
return 0;
}

Step 7. Follow as shown in the image

Step 8. To run, press Ctrl+F9


Step 9. To see the result, click File >> DOS shell

Output
as shown

Step 10. Type exit and press enter to continue


Step 11. To save, go to File >> Save
Step 12. Enter the filename. In this example we will set it as hello.cpp
9
Step 13. Click OK to continue

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;

When we defined main(), we declared it as a function returning an int, meaning it


needs to return an integer. In this example, we are returning the integer value 0, which
is used to indicate that the program exited successfully. After the return 0; statement,
the execution process will terminate.

What’s More

Complete the C Development Cycle


DIRECTIONS: Complete the following statements about what you have learned
about the lesson.

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

Draw the C++ Editor Interface


Direction: In a separate sheet of paper, draw the C++ Editor Interface

Assessmen
t
Activity 1

Direction: Create a program that displays your COMPLETE NAME on screen.

Write your code in the space provided below.

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.

Rules for Naming Variables

variable names must adhere to the following rules:

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

Variable Name Legality


Percent Legal
y2x5__fg7h Legal
annual_profit Legal
_1990_tax Legal but not advised
savings#accoun Illegal: Contains the illegal character
t #
double Illegal: Is a C keyword
9winter Illegal: First character is a digit

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.

Image source: https://www.studytonight.com

C's Numeric Data Types

Variable Type Keyword Bytes Range


Required
Character char 1 -128 to 127
Integer int 2 -32768 to 32767
Short integer short 2 -32768 to 32767
Long integer long 4 -2,147,483,648 to
2,147,438,647
Unsigned character unsigned 1 0 to 255
char
Unsigned integer unsigned int 2 0 to 65535
Unsigned short unsigned 2 0 to 65535
integer short
Unsigned long unsigned long 4 0 to 4,294,967,295
integer
Single-precision float 4 1.2E-38 to
floating-point 3.4E381
Double-precision double 8 2.2E-308 to
floating-point 1.8E3082
1
Approximate range; precision = 7 digits.
2
Approximate range; precision = 19 digits.

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:

int count, number, start; /* three integer variables */


float percent, total; /* two float variables */

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;

 Symbolic Constants - A symbolic constant is a constant that is represented by


a name (symbol) in your program. Like a literal constant, a symbolic constant
can't change. Whenever you need the constant's value in your program, you use
its name as you would use a variable name. The actual value of the symbolic
constant needs to be entered only once, when it is first defined. For example, to
calculate the circumference and area of a circle with a known radius, you could
write
circumference = 3.14159 * (2 * radius);
area = 3.14159 * (radius)*(radius);

1.1 Statements, Expressions and Operators


A statement is a complete direction instructing the computer to carry out some
task. In C, statements are usually written one per line, although some statements span
multiple lines. C statements always end with a semicolon. For example:
x = 2 + 3;
is an assignment statement. It instructs the computer to add 2 and 3 and to assign the
result to the variable x.
White Space
The term white space refers to spaces, tabs, and blank lines in your source code.
The C compiler isn't sensitive to white space. When the compiler reads a statement in
your source code, it looks for the characters in the statement and for the terminating
semicolon, but it ignores white space. Thus, the statement
x=2+3;
is equivalent to this statement:
x = 2 + 3;
It is also equivalent to this:
x =
2
+
3;

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

 Complex Expressions - Complex expressions consist of simpler expressions


connected by operators. For example:
2+8
is an expression consisting of the subexpressions 2 and 8 and the addition
operator +. The expression 2 + 8 evaluates, as you know, to 10. You can also
write C expressions of great complexity:
1.25 / 8 + 5 * rate + rate * rate / cost
When an expression contains multiple operators, the evaluation of the
expression depends on operator precedence.
Look at the following assignment statement:
x = a + 10;
This statement evaluates the expression a + 10 and assigns the result to x. In
addition, the entire statement x = a + 10 is itself an expression that evaluates to the
value of the variable on the left side of the equal sign.
Thus, you can write statements such as the following, which assigns the value of
the expression a + 10 to both variables, x and y:
y = x = a + 10;
You can also write statements such as this:
x = 6 + (y = 4 + 5);
The result of this statement is that y has the value 9 and x has the value 15.
Note the parentheses, which are required in order for the statement to compile.

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:

• The assignment operator


• Mathematical operators
• Relational operators
• Logical operators
Assignment Operator - The assignment operator is the equal sign (=). Its use
in programming is somewhat different from its use in regular math. If you write
x = y;
in a C program, it doesn't mean "x is equal to y." Instead, it means "assign the
value of y to x." In a C assignment statement, the right side can be any expression, and
the left side must be a variable name. Thus, the form is as follows:
variable = expression;
When executed, expression is evaluated, and the resulting value is assigned to
variable.
Mathematical Operator - C's mathematical operators perform mathematical
operations such as addition and subtraction. C has two unary mathematical operators
and five binary mathematical operators.
 Unary Mathematical Operators - The unary mathematical operators are so
named because they take a single operand.

Operator Symbo Action Example


l s
Increment ++ Increments the operand by ++x, x++
one
Decremen -- Decrements the operand by --x, x--
t one
 Binary Mathematical Operators - C's binary operators take two operands.
The binary operators, which include the common mathematical operations found
on a calculator.

Operator Symbo Action Exampl


l e
Addition + Adds two operands x+y
Subtraction - Subtracts the second operand from the first operand x-y
Multiplication * Multiplies two operands x*y
Division / Divides the first operand by the second operand x/y
Modulus % Gives the remainder when the first operand is divided x % y
by the second operand
 Relational Operators - C's relational operators are used to compare
expressions, asking questions such as, "Is x greater than 100?" or "Is y equal to

17
0?" An expression containing a relational operator evaluates to either true (1) or
false (0).

Operator Symbo Question Asked Exampl


l e
Equal == Is operand 1 equal to operand 2? x == y
Greater than > Is operand 1 greater than operand 2? x>y
Less than < Is operand 1 less than operand 2? x<y
Greater than or equal >= Is operand 1 greater than or equal to x >= y
to operand 2?
Less than or equal to <= Is operand 1 less than or equal to operand 2? x <= y
Not equal != Is operand 1 not equal to operand 2? x != y

Relational operators in use


Expression How It Reads What It Evaluates
To
5 == 1 Is 5 equal to 1? 0 (false)
5>1 Is 5 greater than 1? 1 (true)
5 != 1 Is 5 not equal to 1? 1 (true)
(5 + 10) == (3 * Is (5 + 10) equal to (3 * 1 (true)
5) 5)?

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

Operato Symbo Example


r l
AND && Exp1 &&
exp2
OR || Exp1 || exp2
NOT ! !exp1

C’s Logical operator in use


Expression What It Evaluates To
(5 == 5) && (6 != True (1), because both operands are
2) true
(5 > 1) || (6 < 1) True (1), because one operand is true
(2 == 1) && (5 == False (0), because one operand is false
5)
!(5 == 4) True (1), because the operand is 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.

What I Have Learned

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

Primary Data Type


Direction: In a separate sheet of paper, draw the hierarchy diagram of a Primary Data Type in C

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:

 Literal text is displayed exactly as entered in the format string.


 An escape sequence provides special formatting control. An escape sequence
consists of a backslash (\) followed by a single character.
 A conversion specifier consists of the percent sign (%) followed by a single
character.

The printf() Escape Sequences


Sequenc Meaning
e
n The character n
\n Newline
\" The double quotation
character
" The start or end of a string

The printf() Conversion Specifiers


Specifie Meaning Types Converted
r
%c Single character char
%d Signed decimal integer int, short
%ld Signed long decimal integer long
%f Decimal floating-point float, double
number
%s Character string char arrays
%u Unsigned decimal integer unsigned int, unsigned short
%lu Unsigned long decimal unsigned long
integer

1: /* Demonstration using printf() to display numerical values. */


2:
3: #include <stdio.h>
4:
5: int a = 2, b = 10, c = 50;
6: float f = 1.05, g = 25.5, h = -0.1;
7:
21
8: main()
9: {
10: printf("\nDecimal values without tabs: %d %d %d", a, b, c);
11: printf("\nDecimal values with tabs: \t%d \t%d \t%d", a, b, c);
12:
13: printf("\nThree floats on 1 line: \t%f\t%f\t%f", f, g, h);
14: printf("\nThree floats on 3 lines: \n\t%f\n\t%f\n\t%f", f, g, h);
15:
16: printf("\nThe rate is %f%%", f);
17: printf("\nThe result of %f/%f = %f\n", g, f, g / f);
18:
19: return 0;
20: }

Decimal values without tabs: 2 10 50


Decimal values with tabs: 2 10 50
Three floats on 1 line: 1.050000 25.500000 -0.100000
Three floats on 3 lines:
1.050000
25.500000
-0.100000
The rate is 1.050000%
The result of 25.500000/1.050000 = 24.285715

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 _________________________________________________________________________

What I Have Learned


What are the three possible components of a format string?
_______________________________________________________________________

22
_______________________________________________________________________

_______________________________________________________________________

23
What I Can Do

The printf() Conversion Specifiers


Direction: Complete the table below.

Specifie Meaning Types Converted


r
%c char
%d Signed decimal integer
Signed long decimal long
integer
%f float, double
%s Character string
Unsigned decimal integer unsigned int, unsigned short
%lu unsigned long

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:

Name: your complete name here


Age: your age
Address: present address
Zip Code: your zip code

Write your code here:

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:

scanf("%d %f", &x, &rate);


When multiple variables are entered, scanf() uses white space to separate input
into fields. White space can be spaces, tabs, or new lines. Each conversion specifier in
the scanf() format string is matched with an input field; the end of each input field is
identified by white space.

This gives you considerable flexibility. In response to the preceding scanf(), you could
enter
10 12.45

You also could enter this:


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 Have Learned


What is the function of address-of operator?
_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

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

Your birthday is 12/20/2000

Write your source code here

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

SYMBOL NAME DESCRIPTION


a flowchart symbol that contains either the
Terminal Symbol
program start or the word or program stop.
a flowchart symbol that is used to store and
I/O Symbol
display instructions
a flowchart symbol that contains logical and
Decision Symbol relational manipulation of data that is
answerable by yes or no.
Arrowheads/Flow a flowchart symbol that is used to direct the
Symbol flow of the flowchart.
a non-processing symbol used to connect one
On page
part of the flowchart to another within the
Connectors
same page.

Preparation it represents preparatory steps or actions


Symbol such as initialization or program variables.

it represents a named operation or


Pre-defined
programmed steps not explicitly detailed in
Symbol
the program flowchart.
it contains descriptive comments or
Annotation
explanatory notes for clarification of a
Symbol
process.
a non-processing symbol used to connect one
Off page
part of the flowchart to another located on
Connector Symbol
another page.

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.

Scenario: Identify whether user is a male or female based on users’ input.

Start

Declare variable gender

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

Complete Me. Write your answers on a separate page.

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.

1. Compare On page connector and Off page connector

2. Terminal Symbol and Decision Symbol

3. I/O Symbol and Preparation symbol

33
What I Can Do

Activity 5

Direction: Complete the flowchart by filling in the contents in the symbol.

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

In a separate sheet of paper, describe the following terminologies


1. Variables –

2. Expressions –

3. Operators –

4. Statements –

5. Flowcharts –

36
37
DISCLAIMER

This Self-Learning Module (SLM) was developed by DepEd, Schools Division of


Capiz with the primary objective of preparing for and addressing the new normal. Contents
of this module were based on DepEd’s Most Essential Learning Competencies (MELC).
This is supplementary material to be used by all learners of the Schools Division of Capiz in
all public schools beginning SY 2020-2021. The process of LR development was observed
in the production of this module. This is version 1.0. We highly encourage feedback,
comments, and recommendations.

For inquiries or feedback, please write or call:

Department of Education – SCHOOLS DIVISION OF CAPIZ


Learning Resource Management System (LRMS)

Address: Banica, Roxas City


Telephone No.: (036) 6210-974

You might also like