CC1101 Module
CC1101 Module
Jenneth R. Mondez
Kurt Robin A. San Jose
Marlon L. Atanacio
John Ren G. Santos
Numeriano B. Aguado
i
VISION
MISSION
ii
Table of Contents
iii
Course Code: IT 1102
Course Description:
The course covers the use of general-purpose programming language to solve
problems. The emphasis is to train the students to design implement test and debug
programs intended to solve computing problems using fundamental programming
constructs.
Course Requirements:
Assessment Tasks (AT) - 60%
Major Exams (ME) - 40%
Periodic Grade 100%
iv
MODULE 3
INTRODUCTION TO C LANGUAGE
Introduction
Now that you already know the logical steps on how to solve problems and formulate
solutions by creating algorithms, pseudocode, and flowcharts, it is a requirement to choose a
programming language where you can implement the solution. Programming language was
thoroughly discussed in the first module and as mentioned, C language is the high-level
programming language to be used for the entire course.
This module introduces you to C language where you will be able to learn its history
and features. By learning the features of C, you will be able to find out why it is the language
being used for beginners. In addition, you will be familiarized with the environment of C so
that it will be easy for you to navigate the tools as you create your program. Furthermore, the
essential elements of a C programs such as libraries, keywords, comments, and input/output
statements are also introduced.
Learning Outcomes
105
History of C Programming Language
Back in 1972 at AT&T’s Bell Laboratories, Dennis Ritchie, a computer scientist started
to develop some programs he needed for his own use. What he started developing then has
evolved into the C programming language, which by now is widely used around the world. He
was trying to make computing as simple as possible. Dennis Ritchie realized that the then-
current assembly language was much too complex. They attempted to reverse this trend by
building a small, simple programming language on a minicomputer. What Dennis Ritchie
wanted to maintain was not only an efficient computer programming language in which to
create programs, but also a computer programming language around which programming
community could form—fellowship. They knew based from previous experiences that the real
nature of joint computing as provided by time-shared, remote accessed systems is not just to
enter computer code into a terminal, but to motivate post programming communication.
(EasyProgramming, 2016)
106
required for applications. The C language has the following features: (EasyProgramming,
2016)
• Portability
• Flexibility
• Effectiveness and Efficiency
• Reliability
• Interactivity
Why use C?
C was initially used for system development work, particularly the programs that make-up
the operating system. C was adopted as a system development language because it produces
code that runs nearly as fast as the code written in assembly language. Some examples of
the use of C might be: (tutorialspoint, 2016)
Operating Systems
Language Compilers
Assemblers
Text Editors
Print Spoolers
Network Drivers
Modern Programs
Databases
Language Interpreters
Utilities
The source code is then compiled into object code. The program that creates the
object code is called a compiler. The traditional name of the C language compiler is CC, which
107
stands for C compiler. The compiler reads the source code file and generates an object code
file. Object code use the same filename as the original source code file.
The next step is called linking. It is often forgotten because modern compilers both
compile and link, but linking is really a separate step. The linker takes the object code file and
combines it with C language libraries. The libraries are the workhorse of the language. They
contain routines and functions that control whatever device you are programming. If all goes
well, the end result is a program file. You can then test run the program to make sure that it
works the way you want it to. And if not, you start the cycle all over again: edit, compile, and
link, or “build,” and test run.
All of these tools - the editor, compiler, and linker, all originated at the command
prompt or terminal. Programmers do a lot of coding at the command prompt because it is
quick. However, a common way to create a C program is to use an Integrated Development
Environment (IDE). (EasyProgramming, 2016)
Environment of C Language
In this course, Dev-C++ version 5.11 with the TDM-GCC 4.9.2 compiler will be the IDE
to be used. It has an integrated toolset designed to simplify the creation of your program. It
will help you to identify and minimize coding mistakes. Below are some of the features of this
IDE that will help you develop efficient and user-friendly C/C++ applications.
108
You can easily install the add-on libraries using the package manager provided by the
IDE.
This C++ IDE also provides CVS support for source code management.
Installation Process
Dev-C++ is a free software and is distributed under the GNU General Public License.
Thus, it can be distributed or modified freely. You may get the appropriate installable for Dev-
C++ IDE from https://sourceforge.net/projects/orwelldevcpp/. The stepwise installation for
Dev-C++ is given below. (Dev C++ IDE: Installation, Features And C++ Development, 2020)
2. Read the License Agreement. If you accept the terms of the agreement, click I
Agree to continue with the installation.
109
3. Select the components that you need to install as a part of the Dev-C++ installation.
It is suggested to check all the displayed components then click Next.
4. Browse for the destination folder where the Dev-C++ files/libraries are to be copied
or you may accept the default destination folder, then click Install.
110
5. The progress of the installation process will be displayed. Once completed, a
“Finish” dialog box that signals the end of the installation will be displayed. Then,
click Finish to launch the Dev-C++ IDE.
111
Parts of the IDE
112
Title Bar - a horizontal bar at the top of a window bearing the name of the application
as well as the location and name of the active program.
Menu Bar – a horizontal bar located below the title bar which contains drop-down
menus.
Toolbar – a horizontal bar located below the menu bar which contains tools for quick
access to frequently used operations and commands.
Line Numbers – pertains to the line number of the code which is important during the
debugging process. It also gives an idea to the developer on how long the code is.
Compiler – is the location where the developer may read the error message during the
compilation of the program. Other details related to compilation is also displayed in
this window.
Text Editor – is the area where the developer types the source code.
Below are the steps to be followed for any C program to create and get the output.
(C Programming Basics, 2020)
1. Create
2. Compile
To compile means to convert the source code written in a high-level
programming language into an object code written in machine
language. A compiler is the program responsible for the compilation
process.
During the compilation process, the compiler checks if the code
follows the standard rules set by the language also known as
113
syntax. If there is a violation in the syntax, the compiler provides an
error message stating the description of the error.
3. Execute or Run
4. Get the Output
Program Structure
Before we study the basic building blocks of the C language, let us look at the minimum
C program structure so that we can take it as a reference in the succeeding modules. A
program in C basically consists of the following parts:
Preprocessor Commands
Functions
Variables
Statements & Expressions
Comments
#include <stdio.h>
int main()
{
/* My first program in C */
printf("Hello, World! \n");
return 0;
}
114
5. The next line printf("Hello, World! \n") is another function available in C which
causes the message "Hello, World!" to be displayed on the screen.
6. The next line return 0; terminates the main() function and returns the value 0.
7. The line } indicates the end of the function main. (tutorialspoint, 2016)
Familiarization with the IDE: Using Dev-C++ version 5.11 with the TDM-GCC 4.9.2
compiler, try to encode the program for Hello World. Follow the
instructions below.
115
8. Source code may be complied and run by clicking Execute>Compile & Run or you
may also press F11 which the shortcut key for this command. Another option to
execute this command is by clicking in the toolbar.
9. For you to practice how to debug (the process of looking for errors and correcting
them), let us put an intentional syntax error in your code. Go back to your source
code, then remove the semicolon (;) in this line,
printf("Hello, World! \n");
10. Then, compile your program using one of the commands discussed above. Now,
you will see an error message in the Compiler window. Using the line numbers,
look for the line number where the error occurs and then correct it. You may also
double click the error message to directly go to the line number where the error
occurs. In this case, type “;” at the end of the printf statement to correct the syntax
error.
11. Again, compile your program and run it.
Basic Syntax
You have seen the basic structure of a C program, so it will be easy to understand
other basic building blocks of the C programming language.
Tokens in C
A C program consists of various tokens and a token is either a keyword, an
identifier, a constant, a string literal, or a symbol. For example, the following C
statement consists of five tokens:
printf
(
"Hello, World! \n"
)
;
116
Semicolons
In a C program, the semicolon is a statement terminator. That is, each
individual statement must be ended with a semicolon. It indicates the end of one logical
entity. Given below are two different statements:
Comments
Comments are like helping text in your C program and they are ignored by the
compiler. They start with /* and terminate with the characters */ as shown below:
/* My first program in C */
Identifiers
A C identifier is a name used to identify a variable, function, or any other user-
defined item. An identifier starts with a letter A to Z, a to z, or an underscore ‘_’ followed
by zero or more letters, underscores, and digits (0 to 9). C does not allow punctuation
characters such as @, $, and % within identifiers. C is a case-sensitive programming
language. Thus, Manpower and manpower are two different identifiers in C. Here are
some examples of acceptable identifiers:
Keywords
The following list shows the reserved words in C. These reserved words may
not be used as constants or variables or any other identifier names. (tutorialspoint,
2016)
117
The Output Statement: printf()
Syntax:
printf(“string argument”);
Welcome to C Programming!
Happy learning.
#include <stdio.h>
int main() {
printf("Welcome to C Programming!");
printf("Happy learning.");
return 0;
}
However, when run, this program will print:
Notice that the two strings are joined together, technically they are concatenated. This
happens because printf does not place output on a new line, unless this is specified
explicitly. In other words, printf does not automatically supply a newline character after
printing its argument(s). A newline character would cause subsequent output to begin
at the left margin of the next line. In the example, a newline character is not supplied
after “Welcome to C Programming!” is printed so that “Happy learning.” is printed
in the same line.
118
#include <stdio.h>
int main() {
printf("Welcome to C Programming!\n");
printf("Happy learning.");
return 0;
}
The \n terminates the current output line; subsequent output will start at the left`margin
of the next line. Thus “Happy learning.” will be printed in a new line. When run, this
program will print:
Welcome to C Programming!
Happy learning.
As an enhancement, suppose you want to put a blank line between the two lines of
output as shown below.
Welcome to C Programming!
Happy learning.
printf("Welcome to C Programming!\n\n");
printf("Happy learning.");
printf("Welcome to C Programming!\n");
printf("\nHappy learning.");
printf("Welcome to C Programming!\n");
printf(“\n”);
printf("\nHappy learning.");
Escape Sequences
Within the string argument to printf, the backslash (\) signals that a special
effect is needed at this point. The character following the backslash specifies what to
do. This combination (\ followed by another character) is referred to as an escape
119
sequence. The following are some escape sequences that you can use in a string in
a printf statement:
\n issue a newline character
\f issue a new page (form feed) character
\t issue a tab character
\" print "
\\ print \
For example, using an escape sequence is the only way to print a double quote as
part of your output. Suppose you want to print the line
If you typed
then C would assume that the double quote after Use ends the string (causing a
subsequent error when it cannot figure out what to do with to). Using the escape
sequence \", you can correctly print the line with:
(Kalicharan, 2015)
120
Activity 1
Programming: Do as stated.
1. Write a program that will display two (2) stanzas of your favorite song. The
lyrics should be center aligned. Name the program as Song_XXX, where XXX
is your initial. See sample output below:
2. Create a program that will display your simple resume and name it
Resume_XXX. See sample output below.
PERSONAL INFORMATION
EDUCATIONAL BACKGROUND
TERTIARY
Laguna University BSCS 2014
SECONDARY
PGMNHS 2010
PRIMARY
Central Elementary School 2006
121
Assessment Tasks
A. Direction: In a separate sheet of paper, answer the following items:
1. Do you agree that C language is a good programming language for
beginners? Why?
2. What is the difference between source code and object code?
3. What is the importance of placing comments in a program?
4. What does the compiler checks?
5. Differentiate identifiers from keywords?
B. Direction: Debug the following code snippets and write the corrected code snippet
inside a box.
2. printf(“”Laguna University”\n”);
printf(“Santa Cruz, Laguna”\n);
122
Summary
123
References
Dev C++ IDE: Installation, Features and C++ Development. (2020, August 2). Retrieved
from Software Testing Help: https://www.softwaretestinghelp.com/dev-cpp-ide/
NOTE
The Modules presented to you only covered the Fundamental of Programming
with Algorithmic and Logic Formulation
For an in-depth knowledge and understanding about C Language you can
download Module 4, 5, 6 and Module 7, 8,9.
124