0% found this document useful (0 votes)
119 views24 pages

CC1101 Module

1. C programming language was developed in 1972 by Dennis Ritchie at Bell Laboratories to build a small, simple programming language for writing operating systems. 2. C is a general purpose, procedural programming language that is efficient, portable and reliable. It produces code that runs nearly as fast as assembly language. 3. A C source code is a plain text file with a .c extension that is compiled into object code by a C compiler. The object code is then linked with libraries to produce an executable program file.

Uploaded by

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

CC1101 Module

1. C programming language was developed in 1972 by Dennis Ritchie at Bell Laboratories to build a small, simple programming language for writing operating systems. 2. C is a general purpose, procedural programming language that is efficient, portable and reliable. It produces code that runs nearly as fast as assembly language. 3. A C source code is a plain text file with a .c extension that is compiled into object code by a C compiler. The object code is then linked with libraries to produce an executable program file.

Uploaded by

Bryan Mon Rosa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Programming Fundamentals – ALF

Jenneth R. Mondez
Kurt Robin A. San Jose
Marlon L. Atanacio
John Ren G. Santos
Numeriano B. Aguado

i
VISION

Laguna University shall be a socially responsive educational institution


of choice providing holistically developed individuals
in the Asia-Pacific Region

MISSION

Laguna University is committed to produce academically prepared


and technically skilled individuals who are socially
and morally upright

ii
Table of Contents

Module 3: Introduction to C Language


Introduction 105
Learning Outcomes 105
Content and Discussion
History of C Programming Language 106
Definition and Features of C language 106
Why use C? 107
What is a Source Code? 107
Environment of C 108
Installation Process 109
Parts of the IDE 112
Steps to write C programs 113
Program Structure 114
Test Your Self 1 115
Basic Syntax 116
Tokens 116
Semicolons 11/
Comments 117
Identifiers 117
Keywords 117
The Output statement: printf() 118
The Newline Character: \n 118
Escape Sequences 119
Activity 1 121
Assessment Tasks 122
Summary 123
References 124

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.

An introductory lecture/lab course on the basic foundation in programming.


This course deals with the basic programming concepts with the applications of
common program designing tools such as flowchart, pseudocode; program
structures like conditions, loops and iteration, branching, sorting, and array
processing.

Course Intended Learning Outcomes (CILO):


1. Identify and describe the basic program elements, components and structures
2. Analyze simple problems, and identify and define the computing requirements for
the problem
3. Demonstrate and apply the fundamental steps in the program development
4. Construct the general form/format/syntax inf a computer program
independently based on applicable program design technique/paradigm.
5. Appreciate the values and importance of computer programming as a solution
tool.

Course Requirements:
 Assessment Tasks (AT) - 60%
 Major Exams (ME) - 40%
Periodic Grade 100%

Prelim Grade (PG) = (AT x 60%) + (ME x 40%)


Midterm Grade (MG) = ((AT x 60%) + (ME x 40%)) x 70%)+(PG x 30%)
Final Grade = ((AT x 60%) + (ME x 40%)) x 70%)+(MG x 30%)

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

At the end of the lesson, the student is expected to:


1. Recall the history of C language;
2. Recognize the features of C language;
3. Demonstrate familiarity with the environment of C;
4. Familiarize with the program structure, basic syntax and keywords;
5. Apply the output statement of C; and
6. Construct simple C programs to solve problems.

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)

Other Facts about C


 C was invented to write an operating system called UNIX.
 C is a successor of B language which was introduced around the early 1970s.
 The language was formalized in 1988 by the American National Standard Institute
(ANSI).
 The UNIX OS was totally written in C.
 Most of the state-of-the-art software have been implemented using C.
 Today's most popular Linux OS and RDBMS MySQL have been written in C.
(tutorialspoint, 2016)

Definition and Features of C Language

The C programming language is a general purpose and structured programming


language. It is also called a procedural oriented programming language. C is not specifically
designed for specific application areas. However, it was well suited for business and scientific
applications. It has various features like control structures, looping statements, and micros

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

What is a Source code?


A source code is a plain text file written in a programming language. In C, the file is
saved with a .c filename extension. To create a source code, you use a text editor. Any text
editor can do, although some editors offer helpful features like color coding, line numbers,
syntax checking and other tools.

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

An IDE, or Integrated Development Environment, combines the tools for editing,


compiling, linking, and running. It also adds tools for debugging, creating complex programs,
graphical tools and other features. Beneath it all, however, is the command line compiler and
linker. The process is the same: edit, compile and link, run. (EasyProgramming, 2016).

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.

 Dev-C++ supports GCC-based compilers including Cygwin, MinGW among others.


You can either install a dev-C++ IDE along with the compiler integrated or just an IDE
if you already have a compiler on your system.
 It provides support for Class Browser as well as Debug Variable Browser.
 It has a Project Manager that helps to manage various projects.

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)

1. Select the language of your choice as shown in the screenshot.

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

Title Bar Menu Bar Toobar

Line Numbers Compiler Source Code Text Editor

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.

Source Code – the human readable code of the developer.

Text Editor – is the area where the developer types the source code.

Steps to write C programs

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

Example 1: Hello World

#include <stdio.h>
int main()
{
/* My first program in C */
printf("Hello, World! \n");
return 0;
}

Let us take a look at the various parts of the program above:


1. The first line of the program #include <stdio.h> is a preprocessor command, which
tells a C compiler to include stdio.h file before going to actual compilation.
2. The next line int main() is the main function where the program execution begins.
3. The line { indicates the beginning of the function main.
4. The next line /* My First Program in C */ will be ignored by the compiler and it has
been put to add additional comments in the program. Such lines are called comments
in the program.

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)

Test your self 1:

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.

1. Open Dev-C++ IDE.


2. In the menu bar click File>New>Source File or you may also click the in the
toolbar.
3. Go to the text editor, then type the source code in Example 1.
4. To save your source code, click File>Save As in the menu bar or you may also
click in the toolbar. In the Save dialog box, browse for the location where you
want to save your first program. In the [File name], type HelloWorld (do not include
spaces between Hello and World), then click the drop-down arrow in the [Save as
type] box and choose “C source files (*.c)”.
5. To compile your program, click Execute>Compile. You may also click in the
toolbar or press F9 in the keyboard.
6. Check the compilation details in the lower part of the screen. If there are errors,
the details regarding the error will be displayed, otherwise the compilation is
successful and you may now run the program.
7. To run the program, click Execute>Run. You may also click in the toolbar or
press F10 in the keyboard. You should be able to get the output shown 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");

The individual tokens are:

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:

printf("Hello, World! \n");


return 0;

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:

Mohd zara abc move_name a_123


myname50 _temp j a23b9 retVal

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)

auto else long switch break enum


register typedef case extern return union
char float short unsigned double for
signed void continue goto struct volatile
default if static while do int

117
The Output Statement: printf()
Syntax:
printf(“string argument”);

The output statement in C language is printf(). Suppose you want to write a


program to print the following lines:

Welcome to C Programming!
Happy learning.

Your initial attempt might be this:

#include <stdio.h>
int main() {
printf("Welcome to C Programming!");
printf("Happy learning.");
return 0;
}
However, when run, this program will print:

Welcome to C Programming!Happy learning.

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.

The Newline Character: \n


To get the desired effect, you must tell printf to supply a newline character after
printing “Welcome to C Programming!”. This can be done by using the character
sequence \n (backslash n). The source code will be,

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.

Each of the following set of statements will accomplish this:

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.");

C gives a lot of flexibility on how to write statements to produce a desired effect.

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

Use " to begin and end a string

If you typed

printf("Use " to begin and end a string\n");

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:

printf("Use \" to begin and end a string\n");

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

Magkasama Tayo sa Kwento ng Pasko

Sa iisang awit ngayong Pasko


Magkayakap ang tinig ko’t sa’yo
Sa ‘ting himig ipagdiriwang ang pag-ibig
At ito ay tatawid sa buong daigdig.

Sa iisang awit ngayong Pasko


Magkayakap maglilingkod sa’yo
Sa ‘ting himig nadarama na ang mahalaga
Ay magkasama tayo sa Kuwento ng Pasko.

2. Create a program that will display your simple resume and name it
Resume_XXX. See sample output below.

PERSONAL INFORMATION

NAME: Juan dela Cruz


BIRTHDAY: January 1, 2000 AGE: 14
ADDRESS: Brgy. Bubukal Sta. Cruz, Laguna
HEIGHT: 5’7” WEIGHT: 100 lbs.
STATUS: Single

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.

1. printf(“Laguna University\nshall be a socially responsive);


printf(“/n educational institution of choice”);
printf(“\n providing holistically developed individuals”)
printf(“\n\n in the Asia Pacific Region”);

2. printf(“”Laguna University”\n”);
printf(“Santa Cruz, Laguna”\n);

122
Summary

 Dennis Ritchie, a computer scientist developed C programming language in 1972 at


AT&T’s Bell Laboratories.
 C programming language is a general purpose and structured programming language.
It is also called a procedural oriented programming language.
 The features of C language are portability, flexibility, effectiveness and efficiency,
reliability, and interactivity.
 A source code is a plain text file written in a programming language. The source code
is then compiled into object code. The program that creates the object code is called
a compiler.
 An IDE, or Integrated Development Environment, combines the tools for editing,
compiling, linking, and running. It also adds tools for debugging, creating complex
programs, graphical tools and other features.
 Dev-C++ is a free software and is distributed under the GNU General Public License.
 Create, compile, execute or run, and get the output are the steps to be followed when
writing a C program.
 A program in C basically consists of preprocessor commands, functions, variables,
statements & expressions, and comments.
 A C program consists of various tokens and a token is either a keyword, an identifier,
a constant, a string literal, or a symbol.
 In a C program, the semicolon is a statement terminator. That is, each individual
statement must be ended with a semicolon.
 Comments are like helping text in your C program and they are ignored by the
compiler.
 A C identifier is a name used to identify a variable, function, or any other user-defined
item.
 printf() is the output statement in C.

123
References

C Programming Basics. (2020). Retrieved from Fresh 2 Refresh:


https://fresh2refresh.com/c-programming/c-basic-program/

Dev C++ IDE: Installation, Features and C++ Development. (2020, August 2). Retrieved
from Software Testing Help: https://www.softwaretestinghelp.com/dev-cpp-ide/

EasyProgramming. (2016). The C Programming Language. EasyProgramming Publisher.


Retrieved from PDFDRIVE: https://www.pdfdrive.com/c-programming-language-the-
ultimate-beginners-guide-e158124142.html

Kalicharan, N. (2015). Learn to Program with C. Apress. Retrieved from PDFDRIVE:


https://www.pdfdrive.com/learn-to-program-with-c-learn-to-program-using-the-
popular-c-programming-language-d166650744.html

tutorialspoint. (2016). Learn C Programming: C Programming Language. TutorialsPoint.


Retrieved from PDFDRIVE: https://www.pdfdrive.com/learn-c-programming-c-
programming-language-tutorialspoint-d158140781.html

- END OF MODULE FOR FINAL TERM PERIOD –


Preliminary Examination will be given on December ___, 2022.
Kindly check you class schedule for this course.

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

You might also like