C Programming Logic and Design Module Part 1
C Programming Logic and Design Module Part 1
Guide Questions
After reading this chapter, the student should be able to answer the following question.
Definition of Terms
Blank line refers to a line of code that contains no visible characters, typically used for formatting purposes to
enhance readability of the source code.
Code refers to a set of written instructions and statements in a particular programming language that a
computer can execute to perform specific tasks or solve problems.
Comment is a piece of text within the source code that is ignored by the compiler and is used for adding notes,
explanations, or disabling code temporarily.
Compiler is a program that translates source code written in a high-level programming language into machine
code or an intermediate code that can be executed by a computer.
Escape sequence is a sequence of characters beginning with a backslash (\) that represents a special character
or command, such as newline (\n) or tab (\t), within a string literal
Execute means to run or carry out the instructions in a program or script, causing the computer to perform the
specified actions.
Function is a self-contained block of code designed to perform a specific task, which can be called and executed
from different parts of a program, often accepting inputs (parameters) and returning an output.
Header file is a file containing declarations of functions, macros, and variables, which can be included in multiple
source files to facilitate code reuse and modularity.
Integrated Development Environment (IDE) is a software application that provides comprehensive facilities
such as a code editor, debugger, and compiler or interpreter to streamline and enhance the software
development process.
New line refers to a special character or sequence of characters that represents the end of a line of text, typically
denoted by '\n' in languages like C, C++, and Java, used for formatting and readability in output or source code.
Output refers to the data or information that is produced by a program and displayed to the user
Program is a set of instructions written in the C language that defines the behavior and operations to be
executed by a computer to achieve a specific task or solve a problem.
Statements are individual instructions that make up a complete unit of execution within a program, typically
ending with a semicolon in languages like C, Java, and JavaScript.
Syntax is the set of rules that define the correct combinations and sequence of symbols and characters to create
valid instructions and statements in a given programming language.
Text refers to a sequence of characters, such as letters, numbers, and symbols, that are treated as data often
manipulated or displayed to users.
User typically refers to an individual or entity interacting with the program during its execution, providing input
or receiving output based on the program's logic and functionality.
White spaces refer to characters such as spaces, tabs, and line breaks that are used for formatting and
readability in source code but are typically ignored by the compiler or interpreter.
What is C?
Why Learn C?
QuickStart
#include<stdio.h>
int main()
{
printf("Hello world!");
return 0;
}
Don't worry if you don't understand the code above - we will discuss it in detail in later chapters. For
now, focus on how to run the code.
In Coding C, it should look like this:
Then, press Run to run (execute) the program. The result will look something to this:
Congratulations! You have now written and executed your first C program.
Syntax
You have already seen the following code in the previous page. Let's break it down to understand it
better:
Line 1: #include <stdio.h> is a header file library that lets us work with input and output functions,
such as printf() (used in line 5). Header files add functionality to C programs. Don't worry if you
don't understand how #include <stdio.h> works. Just think of it as something that (almost) always
appears in your program.
Line 2: A blank line. C ignores white space. But we use it to make the code more readable.
Line 3: Another thing that always appear in a C program is main(). This is called a function. Any code
inside its curly brackets {} will be executed.
Line 5: printf() is a function used to output/print text to the screen. In our example, it will output
"Hello World!".
Line 7: The closing curly bracket } to actually end the main function.
Statements
The following statement "instructs" the compiler to print the text "Hello World!" to the screen:
Example
printf("Hello World!");
If you forget the semicolon (;), an error will occur and the program will not run:
Example
printf("Hello World!")
Output
Many Statements
The statements are executed, one by one, in the same order as they are written:
Example
printf("Hello World!");
printf("Have a good day!");
return 0;
Example explained
From the example above, we have three statements:
1. printf("Hello World!");
2. printf("Have a good day!");
3. return 0;
The first statement is executed first (print "Hello World!" to the screen).
Then the second statement is executed (print "Have a good day!" to the screen).
And at last, the third statement is executed (end the C program successfully).
Note: You will learn more about statements in the later chapters. For now, just remember to always
To output values or print text in C, you can use the printf() function:
Example
#include <stdio.h>
int main()
{
printf("Hello World!");
return 0;
}
Double Quotes
When you are working with text, it must be wrapped inside double quotations marks "".
If you forget the double quotes, an error occurs:
Example
Example
#include <stdio.h>
int main()
{
printf("Hello World!\n");
printf("I am learning C.");
return 0;
}
Output
You can also output multiple lines with a single printf() function. However, this could make the code
harder to read:
Example
#include <stdio.h>
int main()
{
printf("Hello World!\nI am learning C.\nAnd it is awesome!");
return 0;
}
IDE Window
Output
Tip: Two \n characters after each other will create a blank line:
Example
#include <stdio.h>
int main()
{
printf("Hello World!\n\n");
printf("I am learning C.");
return 0;
}
Output
What is \n exactly?
The newline character (\n) is called an escape sequence, and it forces the cursor to change its position
to the beginning of the next line on the screen. This results in a new line.
Comments can be used to explain code, and to make it more readable. It can also be used to prevent
Single-line Comments
Any text between // and the end of the line is ignored by the compiler (will not be executed).
int main()
{
//This is a comment
printf("Hello World!\n\n");
return 0;
}
C Multi-line Comments
Example
#include <stdio.h>
int main()
{
/* The code below will
print the words Hello World!
to the screen, and it is amazing
*/
printf("Hello World!\n\n");
return 0;
}
Single or multi-line comments?
It is up to you which you want to use. Normally, we use // for short comments, and /* */ for longer.
Good to know: Before version C99 (released in 1999), you could only use multi-line comments in C.
Exercises
Note:
✓ This programming exercise is the first quiz requirement.
✓ Your answer will be inspected before the start of the quiz.
✓ Your program code must be handwritten.
✓ Use long bond paper.
1-1) Create a program in C to display your full name, date of birth, contact number and address.
int main()
{
printf("Fullname: Juan Dela Cruz\n");
printf("DOB: April 1, 2001\n");
printf("Contact No.: 0987654321\n");
printf("Address: Sitio Kamangahan, Brgy. Bato, Biliran, Leyte \n");
return 0;
}
Note: The above program code is an example only, it is what you are supposed to create.
1-2) Create a program in C to display the first stanza of your favorite Christmas song.
Sample program output:
1-5) Create a program in C to display the five reasons why you enrolled in the BSCpE program.
Sample program output: