0% found this document useful (0 votes)
18 views14 pages

C Programming Logic and Design Module Part 1

Uploaded by

dykv17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
18 views14 pages

C Programming Logic and Design Module Part 1

Uploaded by

dykv17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 14

Programming Logic and Design Module Part 1

Prepared: Engr. Estelito R. Medallada, Jr.

Guide Questions

After reading this chapter, the student should be able to answer the following question.

1. Who created the C programming language and when?


2. What are three reasons to learn C?
3. What is the basic structure of a C program?
4. What does the printf() function do in C?
5. How do you end statements in C?
6. How can you insert a new line in the output of a C program?
7. What are the two types of comments in C and how are they written?
8. What is an escape sequence in C?
9. What is the purpose of the #include <stdio.h> line at the beginning of a C program?
10. What happens if you forget to put double quotes around text in a printf() function?

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?

✓ C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories


in 1972.
✓ It is a very popular language, despite being old. The main reason for its popularity is because it is
a fundamental language in the field of computer science.
✓ C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Why Learn C?

✓ It is one of the most popular programming languages in the world


✓ If you know C, you will have no problem learning other popular programming languages such as
Java, Python, C++, C#, etc, as the syntax is similar
✓ C is very fast, compared to other programming languages, like Java and Python
✓ C is very versatile; it can be used in both applications and technologies

Get Started With C

To start using C, you need the following:


✓ On an Android smartphone: Coding C IDE for mobile
✓ On an iPhone smartphone: C-programming language for OS IDE
✓ For Windows OS: Code::Blocks. Follow the instructions in the PDF file on how to download and
use it.

QuickStart

Let's create our first C file.

Open Coding C and go to Menu > Hello World.

The following C code will already be written:

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

Hello World code explained

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

Note that: Every C statement ends with a semicolon ;


Note: The body of int main() could also been written as:
int main(){printf("Hello World!");return 0;}
Remember: The compiler ignores white spaces. However, multiple lines makes the code more
readable.

Line 6: return 0 ends the main() function.

Line 7: The closing curly bracket } to actually end the main function.
Statements

A computer program is a list of "instructions" to be "executed" by a computer.

In a programming language, these programming instructions are called statements.

The following statement "instructs" the compiler to print the text "Hello World!" to the screen:
Example

printf("Hello World!");

It is important that you end the statement with a semicolon ;

If you forget the semicolon (;), an error will occur and the program will not run:

Example
printf("Hello World!")

Output
Many Statements

Most C programs contains 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

end them with a semicolon to avoid any errors.


Output (Print Text)

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

printf("This sentence will work!");

printf(This sentence will produce an error.);

Many printf Functions


You can use as many printf() functions as you want. However, note that it does not insert a new line
at the end of the output:
Example
#include <stdio.h>
int main()
{
printf("Hello World!");
printf("I am learning C.");
printf("Maka inspired man diay!");
return 0;
}
Output:
New Lines
To insert a new line, you can use the \n character:

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.

Examples of other valid escape sequences are:

Escape Sequence Description

\t Creates a horizontal tab

\\ Inserts a backslash character (\)

\" Inserts a double quote character


Comments in C

Comments can be used to explain code, and to make it more readable. It can also be used to prevent

execution when testing alternative code.

Comments can be singled-lined or multi-lined.


Note: Comments do not display anything when the code is executed.

Single-line Comments

Single-line comments start with two forward slashes (//).

Any text between // and the end of the line is ignored by the compiler (will not be executed).

This example uses a single-line comment before a line of code:


#include <stdio.h>

int main()
{
//This is a comment
printf("Hello World!\n\n");
return 0;
}

C Multi-line Comments

Multi-line comments start with /* and ends with */.

Any text between /* and */ will be ignored by the compiler:

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.

Sample program code:


#include <stdio.h>

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.

Sample program output:

Fullname: Juan Dela Cruz


DOB: April 1, 2001
Contact No.: 0987654321
Address: Sitio Kamangahan, Brgy. Bato, Biliran, Leyte

1-2) Create a program in C to display the first stanza of your favorite Christmas song.
Sample program output:

First stanza of the song “Christmas In Our Hearts”


Whenever I see girls and boys Selling lanterns on the streets I remember the child
In the manger as he sleeps
1-3) Create a program in C to display the names and addresses of your elementary, secondary, and
tertiary schools.
Sample program output:

Elementary School: Culaba Central School


Address: Culaba, Biliran
Secondary School: Bool National High School
Address, Bool, Culaba, Biliran
Tertiary School: Naval State University
Address: Naval, Biliran

1-4) Create a program in C to display your favorite life motto.


Sample program output:

My favorite life motto is:


“Life is a never-ending process of learning.”

1-5) Create a program in C to display the five reasons why you enrolled in the BSCpE program.
Sample program output:

The reasons I enrolled to the BSCpE program are:


1.) I wanted to be able to fix computers.
2.) I wanted to learn how to create software.
3.) I enjoy the challenges in learning new things.
4.) BSCpE careers wages are above average.
5.) I wanted to control hardware using code.

You might also like