Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CC102 Lesson Proper For Week 1 To 5

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 28
At a glance
Powered by AI
Some of the key takeaways from the document are that computers are not intelligent on their own and require programming instructions, programming involves a multi-step problem solving process, and there are different data types and operators used in C.

The steps involved in problem analysis are defining the problem and users, determining the desired outputs, determining the desired inputs, determining the desired processing, and double checking feasibility.

The goals of program design are to describe the algorithm for solving the problem by specifying the program logic and actions using tools like flowcharts, pseudocode, or object oriented design.

Computers are not intelligent.

Yes, computers
are fast. They are able to calculate complex
computations in mere seconds. However, being
fast does not make computers intelligent. It only
makes them highspeed followers, nothing more.
In reality, computers are only as good as
instructions or programs you feed them.
This is a very important point to consider in
programming.

Programming is the art and science of creating


programs. A program is a list of organized
instructions that the computer must follow in
Source: From Google
Program Analysis
order to process data into information.

Program Development Process


Program Design
You might think that programming simply
involves typing words and numbers in a
computer.
Program Coding
Actually, this activity, called coding, is only a
small part of the whole programming process.
Programming, also called software engineering,
is a multi-step process for creating a program. Program Testing and
Debugging
Programming is also a method of solving a
problem. Programming uses algorithms an
algorithm being a set of ordered steps for solving Program Documentation
a problem, and essentially a synonym of logic.
Analytical and critical thinking is needed in the And Maintenance
formulation of solutions to a problem.

Steps in program development


You solve problems most of the time. One simple
problem is deciding whether to do your assignment
before or after playing games. A more complicated one is
computing the average of your 6 quizzes in math. A
rather complex one is preparing your school’s payroll
processing system.
How, Then, do we approach the activity of programming? The following are the
procedural steps required in the problem-solving process called programming:
1. Problem Analysis
2. Program Design
3. Program Coding
4. Program Testing and Debugging
5. Program Documentation and Maintenance
The First Step: Problem Analysis
Problem analysis is defined by the American National Standards Institute (ANSI)
as the methodical investigations of a problem and the separation of the problems into
smaller related units for further detailed study.
Problem analysis requires performing the following steps:
1. Define the problem and the users.
Here, you need to write a statement on the objectives of the program you
are trying to accomplish (that is, the problem you are trying to solve). If it is
large program with many functions, divide it into its component parts or
modules and state the purpose for each module. You also need to know who
will be the users of the program, as well as their skills in using the program.

2. Determine the desired outputs.


Understand first the kind of outputs expected of your program (that
is, the information to be produced and its format) before you study the
inputs.
3. Determine the desired inputs.
Once you know the kind of outputs required, you can then think
about the inputs. Determine the kind of data needed to produce the
output, the format on how the data should appear, and the sources of
data.
4. Determine the desired processing.
Describe what operations are necessary to transform the inputs into
the desired results. Diagrams are often helpful to clearly define the
transformations involved.
5. Double-check the feasibility of implementing the program.
Clarify if the program you are supposed to create is feasible within the present
setup and/or budget. You may
probably decide to buy an existing
program and modify it rather than
write one from scratch.
6. Document the analysis.
Make sure you document everything
you do, such as writing the objective
specifications of the entire process of
problem analysis

The Second Step: Program Design


The program design process describes the
algorithm for the solution of the problem. Algorithms specify what actions and operations
a program will take. A programmer has several program design tools to choose from—
flowcharts, decision tables, or
pseudocodes in formulating an algorithm

Program design requires performing the following steps:


1. Determine the program logic using a top-down approach and modularization
Logically, laying out a program is like outlining a lengthy term paper before
you proceed to writing it. Currently, object-oriented programming is the approach
used by many programmers. This style produces programs with clean flow, clear
design, and a degree of modularity or hierarchical structure.
Structured programming takes a top-down approach that breaks down a large program
into smaller pieces or modular forms, called modules, and assembles these
pieces into a program with coherent logic flow. The concept of modularization is
useful as the entire program can be more easily developed because the parts
(the modules) can be develop and tested separately.
2. Design details using pseudo code and/or flowcharts.
When you have already determined the essential logic of your program
through the use of top-down programming and hierarchy chart, you can then go
to work to the details.
There are two ways to show details—write them by using pseudocode or
draw them by using flowcharts. Most projects use both methods.
Pseudocode: A pseudocode (pseudo means fake) is an algorithm written
in normal human-language statements to describe the logic and processing flow.
Many programmers write their programs first in pseudocode that looks much like
a mixture of English and their favorite programming language, such as C, and
then translate it line by line into the actual language being used.
A pseudocode can also contain such terms as IF, THEN, or ELSE. It has
to, however, follow the rules of control structures, an important aspect of
structured programming.
How do you write a pseudocode? Study the example below.
Problem:
Get the following data from the user: employee’s name, hourly rate, number of
hours worked, and deductions
Calculate the employee’s gross pay and net pay
Display the employee’s name, gross pay, and net pay
Pseudocode:
READ name, hourly rate, hours worked, deductions
Calculate: Gross pay = hourly rate multiplied by number of hours worked
Calculate: Net pay = Gross
- “Flowcharts use special shapes to represent different types of actions or
steps in a process.
Lines and arrows show the sequence of the steps, and the relationships
among them”.
- “Use of symbols and phrases to designate the logic of how a problem is
solved” (SCHN95).
- “A diagram representing the logical sequence in which a combination of steps
or operations is to be performed. It is a blueprint of the program”(LAPU86).
- A system flowchart is a diagrammatic representation of the broad flows of
work, documents and operations. A system flowchart shows what job is to be
done. Thus, a system flowchart merely names the processes to be performed
in the “predefined process”.
- A program flowchart is a diagrammatic representation of a solution to a given
problem for which the program is written. A program flowchart shows how the
job is to be done. Thus, a program flowchart outlines the details of the
sequence of operations to be executed. Algorithm Defined
- Algorithm is a finite set of instructions that specify a sequence of operations
to be carried out in order to solve a specific problem or class of problems”
(SWAZ89).
- An algorithm (pronounced AL-go-rith-um) is a procedure or formula for
solving a problem. The word derives from the name of the mathematician,
Mohammed ibn-Musa al-Khwarizmi, who was part of the royal court in
Baghdad and who lived from about 780 to 8'
1.2 Basic symbols used in Flowcharting

What it represents
Symbols
- Used to signify the beginning
and end of the flowchart.

Terminal

- Signifies the preparation of data


- Used to represent instructions or
group of instructions that will alter
or modify a program’s course of
execution.

Preparation/Initialization

- “Shows input and output. Data are


to be read into computer
memory from an input device or
data are to be passed from the
memory to an output device”
(SWAZ89).

Input / Output

- Performs any calculations that are


to be done

Processing
- Signifies any decisions that are
to be done.
- “Two alternative execution paths
are possible. The path to be
followed is selected during the
execution by testing whether or
not the condition specified within
the outline is fulfilled”
(SWAZ89).
Decision

- “Shows the entry or exit point of


the flowchart” (SWAZ89).
- “A non-processing symbol used
to connect one part of a
flowchart to
another without drawing
flowlines”
(LAPU86)
On-page connector

- “Designates entry to or exit from


one page when a flowchart
requires more than one page”
(LAPU86).

Off-page connector

- Signifies the process that is to be


executed next.

Flowlines
- Used as an input/output symbol
specifically for data moving in
and out of secondary storage.

Secondary Storage

- Contains the name assigned to a


set of instructions that are
separated from, but may be
called within, the main program;
used extensively when
programs are broken into
modules.
Predefined Process

3. Do a structured walkthrough.

Have you experienced, after thoroughly reviewing and editing your


term paper, having a friend or teacher point out some things you missed?
The same experienced happens to programmers.
There is an established part of the design phase called structured
walkthrough. It is a method for examining a computer system, including its
design and implementation, in a systematic way. In the structured
walkthrough, the programmers (as well as fellow programmers, systems
analysts and perhaps, users) working on different aspects of a software
development project meet and attempt to coordinate the various segments
of the overall project. The goals, requirements, and components of the
project are systematically reviewed in order to minimize the error rate of
the software under development.

Flowchart and Algorithm examples

1. Design a flowchart that will accept and display a number. Write its equivalent
algorithm
Algorithm Start

Step 1: Read in the value of N

Step 2: Print the Read N value of N

Print N

End

The Third Step: Program Coding


Once the program design has been developed and
reviewed in a structured walkthrough, the actual
writing of the program begins. This process is called
coding. In coding, the logic requirements from
pseudocode, algorithm or flowcharts are translated
into lines of instructions using a programming
language—the letters, numbers and
symbols arranged accordingly to syntax or
language rules that make up the program. The
purpose of this step is basically to prepare the
program to be inputted to the computer system.

Here are the steps in coding the program:


1. Select the appropriate programming language
A programming language—such as C,
Visual Basic and Java—is a language used to
write instructions for the computer.
Not all languages are appropriate for all uses. While some have
strengths in mathematical and statistical processing, others are more
appropriate for database management. Hence, in selecting a
programming language, you can base it on considerations as:
 What purpose the program is designed to serve, and
 What languages are already being used in the organization or field you are in.
2. Follow the syntax.
Just as in human languages (Filipino, English, Spanish, etc.),
programming languages have their own grammar—the system of rules for
the languages by which words are formed and put together to make
sentences.
Syntax is the grammar of a language. It consists of the rules
governing the structure and content of the statements. For a computer Program to
work, you have to follow strictly and perfectly the
syntax of its language.

The Fourth Step: Program Testing and


Debugging
Program testing involves running various tests, such
as desk-checking and debugging (These two test are
called alpha testing.), and then running actual or real
data to make sure the program works
(This is called beta testing.).
The following are the steps in testing a program:
1. Perform desk checking
Desk checking is simply reading through, or manually testing, the
solution to make sure that it is free of errors and that the logic works. Desk
checking is like proofreading. You should do desk-checking before running
your program.
2. Debug the program
After desk-checking, the programmer needs to compile the source code to convert it into
an object code. This is necessary because computers cannot understand a
programming language directly unless it is transformed into a machine language. A
source code refers to the instructions in a programming language while object code
refers to the

instructions in a machine language, which is a language of computers. The


process of converting a source code into object code is called compilation.

Figure 1.2
Compilations
Compile Object code
Source code
(Machine
language)
(Programming
language
During compilation, some program errors (called bugs) may be encountered, causing
the program’s failure to produce the desired output.
The programmer needs to go through the process of
detecting, locating, and removing these errors (also
called debugging) in order for the program to be able
to solve the problem at hand. There are three types of
program errors: syntax error, run-time error, and logic
error.
Syntax error is the most common error that you will
encounter in programming. It is caused by
typographical errors (such as misspelled words or
incorrect symbols) and/or failure to strictly follow the
syntax of the programming language (such as omitted
punctuation or unmatched parenthesis).
Run-time error is a software error that occurs while a program is being executed, as
detected by a compiler or other supervisory programs.
Mostly, run-time errors occur in numeric calculations,
such as dividing a number by zero (resulting to “divide
error”); in evaluating an expression that would result in
an illegal data, such as assigning to an integer type
variable a value greater than 32 767 (which results to an “overflow”); or in giving an
improper input, such as when a user entered a character data where an integer vale is
expected.
Logic errors are caused by incorrect use of control
structures. These errors are much more difficult to
detect than syntax errors because the computer
cannot tell an error in logic in a program.
3. Run real data
After desk-checking and
debugging, the
program, then needs to be tested with real
data, called beta-testing. There is a need to
also test a program with bad data—data that is
faulty, incomplete, or in overwhelming
quantities—to see if this can make the system
crash. In programming, programmers should
consider the fact that there could be users who are far more heavy-handed,
ignorant, and careless.
The Fifth Step: Program Documentation and
Maintenance Documentation is the set of
instructions shipped with a program or a piece of
hardware. Documentation usually includes necessary
instructions on the use and maintenance of the
product. It is not just an end-stage process of
programming. In fact, it needs to be done throughout
the entire programming steps. It is needed for people
—users, computer operators, and programmers—who
will be using the program or who will be involved with
the program in the future.
Program documentation and maintenance consists of the following steps:
1. Prepare user documentation.
The printed, on disk, or online manuals in commercial software
packages are user documentation which can help nonprogrammers use the
software.
2. Prepare operator documentation.
The people who run computers are called computer operators. They
are not necessarily programmers; hence, they also need information (the
documentation) on what to do when the program malfunctions.
3. Prepare program documentation.
Program documentation consists of written, graphic, and electronic
Descriptions of what a program is and how to use it. This makes the program
understandable even at a later time. The variables are defined and the
comments are made to be informative. New programmers can maintain an
existing system because of program documentation. Good documentation
reduces effort on program maintenance and major program overhaul.
4. Maintain the program.
Maintenance is any activity designed to keep programs error-free, up-
to-date, and in good working condition. It includes tests, adjustments,
replacements, repairs, and so on. Since organizations change their products,
marketing strategies, accounting systems, etc., these changes should be
documented to help programmers make adjustments in existing systems.

NOTE: Algorithm – a finite sequence of steps for solving a


logical or mathematical problem, or performing a task.
Logic- a theory of reasoning
Module – a standalone routine which usually performs a
specific function, such as accepting data input or printing a
report.
Top-down design – a program design methodology that starts with defining program
functionality at the highest level (a series of task) and then breaks down each task into
lower-level tasks, and so on.

2.1 History of C Language

C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to


develop the UNIX operating system at Bell Labs. C was originally
first implemented on the DEC PDP-11 computer in 1972.

In 1978, Brian Kernighan and Dennis Ritchie produced the first


publicly available description of C, now known as the K&R
standard.

The UNIX operating system, the C compiler, and essentially all


UNIX application programs have been written in C. C has now
become a widely used professional language for various reasons

• Easy to learn
• Structured language
• It produces efficient programs
• It can handle low-level activities
• It can be compiled on a variety of computer platforms

2.2 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.
• Today C is the most widely used and popular System Programming Language.
• 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.

2.3 Uses of 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 −

• Operating Systems
• Language Compilers
• Assemblers
• Text Editors
• Print Spoolers
• Network Drivers
• Modern Programs
• Databases
• Language Interpreters
• Utilities

Programs
A C program can vary from 3 lines to millions of lines
and it should be written into one or more text files with
extension ".c"; for example, hello.c.

Why Program in C Language?

Here are the advantages of C:

C is a small language, yet powerful.

It has only 32 keywords (or reserved words), which are the


commands that make up the C Language. Turbo C contains 11
more keywords to support various enhancements and extensions. For comparison, BASIC for the IBM PC
contains 159 keywords.

C is the native language of UNIX.

UNIX is the major interactive operating system on workstations, servers, and mainframes. C is the
standard development language for personal computers. Most Windows applications, database
programs, graphics libraries, and other large application are written in C.

C code is very portable.

This means, C is capable of running on more than one computer system or under more than one
operating system. It is easy to get a program written in C on one machine to run on a different machine.
You can easily transfer application programs written in C from one system to another. C is portable
mainly because the actual language definition is very small. There are very few features built into C, but
using functions, you can expand the language. The C language does not even have built-in routines for
printing to the screen or reading from the keyboard. However, there are standard functions for these
and other tasks which are supplied with the C compiler. C programs use a standard library that works
the same in all machines. These libraries allow programmers to build programs that can run in any
machine that has these libraries.

C is a structured language.

C emphasizes structured programming. In this style of programming, a complex program is broken


down into subprograms, each of which can be coded and tested individually. The various programs need
not even be programmed by the same programmer.

C is a “middle-level language”.

Although C cannot beat assembly language at producing code that is highly compact and tuned, it is the
language that probably comes closest to keeping up with assembly language. C has a powerful set of
operators that allows the direct manipulation of bit, bytes, words, and pointers. Using C, a programmer
can produce programs that are as efficient as those written in assembly language while still enjoying the
usefulness of high-level language.

C is the foundation for C++ and Java.

Though C is often criticized to have a complicated syntax since it can allow the multiple use of some
symbols such as “*” and “=”, yet this same feature is what makes C very attractive to power
programmers. C is an elegant language. Many of the constructs and methodologies that are routinely
used in C are also used by C++ and Java programs. Thus, learning C can be considered a first step in
learning C++ and Java, which both support object-oriented programming (OOP).

IV. LESSON PROPER


Turbo C Compiler
Turbo C is a particular version of C developed by Borland International Corporation in 1987. Turbo C is
designed to run on various microcomputer systems. Borland C is the professional version of Turbo C.
although Borland C contains essentially the same compiler as Turbo C. it comes with more programming
support tool.

Turbo C provides both an


integrated development environment (IDE) and Source Code
the more traditional command-line version to satisfy the (C language program)
needs and desires of many programmers. Using #include <stdio.h>
C in its integrated environment main()
makes programming more flexible. This interface { printf(“ hello world”);
includes a complete environment to create, edit, test, return 0;
compile, link, and run your C programs. This } - C Compiler
programming environment consists of a number of
components which include the following:
Editor. The Turbo C Editor is used to create a C program Object Code
called the source code. (machine program)
1111001001110011110110
0000100001000001110000
0101011000100010011100

Extended C Language. Turbo C is significantly


Figure 3.1 The C compiler converts a C
extended from the “bare-bones” language of Ritchie’s
program ( the source code) into
specifications. The extensions included enhancements machine language
which make Turbo C compatible with the ANSI C (object code)before the
standard. computer can execute the
program.
Compiler. A compiler is used to translate a source code into the machine language object code (see
Figure 3.1). Turbo C has become one of the world’s most widely used C compilers. It is known for its
speed of compilation and the efficiency of the code that it produces. Many well-known software
products were created using Turbo C.

Debugger. A debugger is used for testing a program and locating programming errors.
Run-Time Environment. A run-time environment is an environment with the capability of
running programs within the Turbo C system.
User Interface. The user interface is the portion of a program where a user interacts. The various
features of Turbo C are integrated into a single program that allows you to smoothly proceed from
sourcecode entry to compilation to debugging and to running
without ever leaving the Turbo C environment.

Using the Turbo C IDE


In actual coding, the interface is not necessary. You just
need a text editor, such as Notepad or Write, to write a C
program. Just make sure to save and name your C program file
using 8 characters or less and use the file extension “.c” , for
example “myprog1.c”. You can then load this program into
Turbo C and run the program. From the C prompt, you can also
type “myprog1.c” to load and compile your program in Turbo C.

To make things easier, you can use the Turbo C integrated development environment (IDE). As
previously mentioned, the Turbo C IDE, complete with a text editor and compiler, makes it possible to
edit, compile, link, and run a C program without even leaving the Turbo C environment. This result in an
extremely rapid compilation process and recompilation cycles, thus making the creation, testing, and
debugging of your C program easier and faster.

Starting and quitting Turbo C


Turbo C is basically a DOS-based program, but it can be
started in Windows. Here are the steps on starting and
quitting Turbo C:
1. Find TC.EXE among your programs. You can
rightclick on start à explore (or search) to
explore/search the drive and directory (or folder)
where Turbo C is installed.
2. Run the Turbo C program by double-clicking on TC
or TC.EXE
3. When Turbo C begins and waits at the Copyright screen, press, Enter.

4. The main Turbo C appears (see figure 3.2), with the main menu active, showing its File option
highlighted.
To choose a Main menu item where at he Main menu bar:
 Use arrow keys to highlight the item, then press Enter; or
 Type the first letter of the menu item you want (e.g, press E or e to select
the Edit

The following are the parts of the main Turbo C IDE:


Main menu bar

Edit Window

Message
Window

“Hotkey”
Reference
line

Figure 3.2 the main Turbo C screen, with parts labeled


The Main menu bar contains a list of menus which organizes all the commands and operations used in
Turbo C.

The Edit window is used by Turbo C’s Editor. It is the area where you enter and edit your C program. It is
activated by pressing Alt + E or F10 while in the Main menu. The top line of the Edit window is the Edit
status line, which tells you various things about the state of the Editor and the file you are editing. Line
and Col tells of the location of the cursor (_) on the Edit window. When you start a new program by
choosing File à New, C calls it NONAME.C.

The Message window is used to display various compiler or linker messages. The “Hotkey” reference
line contains a list of useful shortcuts or key combination used to immediately execute a defined option
or function. The Help menu, for example, can be activated by pressing F1.
5. To quit Turbo C, choose Fileà Quit or simply press Alt + X. If your current file on the Edit window has
been modified since it was saved, Turbo C will ask whether you wish to save your file or not.

COMPUTER PROGRAMMING 1 Page 4 of 15

IV. LESSON PROPER

Program Structure

A C program basically contains the following:

1. Headers

The segment of a C programs where libraries are included in the compilation


process and where macros to be defined are written and stated. It may contain the
following:

a. Preprocessor. A system program that modifies a C program prior to its


compilation.
b. Preprocessor directive. A C program line beginning with # that provides
an instruction to the preprocessor.

The two most commonly use directives are:

#include- for libraries used in the program

#define- for constant macro definition.

A constant macro is a name that is replaced by a particular constant value before the program is
sent to the compiler.

Example

#define pi_value 3.1416

Note: The contents of a named constant memory location cannot change during program execution.
Commonly used include files in C Language

• alloc.h – declares memory management functions


• conio.h – declares various functions used in calling IBM-PC ROM BIOS
• ctype.h – contains information used by the classification and character
conversion macros.
• math.h – declares prototype for the math functions.
• stdio.h – defines types and macros needed for standard I/O.
• string.h – declares several string manipulation and memory manipulation
routines.

2. Declaration
All functions, variables, units or objects that contain values or group of values must
be made known to the computer so that memory spaces will be allocated for
each of them

3. Functions
These are parts of a C program that performs a specific task. The task assigned to a
function is performed whenever C encounters the function name. In C, there
should be at least one function and it is the function main.

The main () in C language declares the main program and call other functions.

Note: C is case sensitive!

4. Comments

These are remarks and non-executable phrases or sentences.

Importance of Comments on Programs


• Program documentation contains information that enhances the
readability of the program.

• Comments are part of program documentation because they help


others read and understand the program. The compiler, however,
ignores comments and they are not translated into machine
language.

Types of comments:
• Multiple line comments – are comments enclosed by /* and */.
• Single line comments – are comments placed after // symbols.

5. Identifiers
Identifiers are names given to various program elements, such as variables,
functions and arrays. The following are the guidelines in creating identifiers:

• It consists of letters and digits without space, in any order, except that
the first character must be a letter.
• Uppercase and lowercase are permitted but they are not
interchangeable. As an exception, an underscore may be used as the
first character of the identifier.

• Reserved words and standard functions must not be redefined.

Example of Valid Identifiers

X num_1 y1 _area NAME Sum

Example of Invalid Identifiers Reason for Invalidity

1st starts with a digit


tax rate contains space

$area contains a special character

Void main a reserved word int

must not be redefined

6. Reserved words
Reserved words are keywords that have standard predefined meaning in C. These
reserved words can be used only for intended purpose and they cannot be used
as programmer’s defined identifiers.
Table 4.1 The 32 Turbo C keywords as defined by the ANSI standard

Auto Double int Struct

Break Else long Switch

Case enum register Typedef

Char extern return Union

Const float short Unsigned

continue for signed Void

Default goto sizeof Volatile

Do If static While

We had just finished the discussion on C Program Structure

Let’s move on to the next higher level of activities or exercisesthat


demonstrates your potential skills/knowledge of what you have
learned.
IV. LESSON PROPER

5.1 Data types


There are five elementary data types in C: character

(char), integer (int), and floating point, double floating point and
void. Values of type char are used to hold ASCII characters or any 8-
bit quantity. Values of type int are used to hold integer quantities.
Values of type double and float are used to hold real numbers. Real
numbers have both an integer and fractional component. The type
void has three uses: The first is to declare explicitly a function as
returning no value. The second is to declare explicitly a function
having no parameters. The third is to create generic pointers.

Type Bidwidth Range

Char 8 0 to 255
Int 16 -32768 to 32767
Float 32 3.4E-38 to 3.4E+38
Double 64 1.7E-308 to 1.7E+308
Void 0 Value less
Table 5.1 Size and Range of Turbo C’s Basic Data Types

Type Modifiers
Except type void, the basic data types may have various modifiers preceding them. A Modifier is
used to alter the meaning of the base type to fit the needs of various situations more precisely. The
list of modifiers includes the following:
1. Signed 3. long
2. Unsigned 4. Short
Type Storage size Value range

Char 1 byte -128 to 127 or 0 to 255

unsigned char 1 byte 0 to 255

signed char 1 byte -128 to 127

int 2 or 4 bytes -32,768 to 32,767

unsigned int 2 or 4 bytes 0 to 65,535

Short 2 bytes -32,768 to 32,767

unsigned short 2 bytes 0 to 65,535

Long 4 bytes -2,147,483,648 to 2,147,483,647

unsigned long 4 bytes 0 to 4,294,967,295

1.2 Variables defined

Variables are identifiers that can store a changeable value. These can be
different data types.
1.2.1 Variable declaration

All variables must be declared before they may be used. The general form
of declaration is shown here:

Type variable list; example: int i, j, k;


short i, j, k;

Note: - before declaring variable, specify first the data type of the variable/s .

- Variables must be separated


by comma All declarations must be terminated
by a semicolon (;)

1.2.2 Two kinds of variable

A. Local variables

Variables that are declared inside a function are called local variables. It can only be
referenced by statements that are inside the block in which the variables are declared.

Example:
#include <stdio.h>
main()
{
int a, b, c; local variables
__________;
__________;
__________;
}

B. Global Declarations
Global variables are known throughout the entire program and may be used by any piece of
code. Global variables are created by declaring them outside of any function.

Example:
#include <stdio.h>
int a, b, c; global declarations
main()
{
__________;
__________;
__________;
}

5.3 Constants Defined


Constants are identifiers/variables that can store a value that cannot be changed during
program execution.

Example: const int count =100;

Where integer count has a fixed value of 100

5.4 Arithmetic, Logical, Relational, and Bitwise Operators


Operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations. There are four classes of
operators in C: arithmetic, logical, relational, and bitwise

5.4.1 Arithmetic Operators

Operator Action

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulus Divisor

-- Decrement a value

++ Increment a value
5.4.2 Relational Operators
Relational operators used to determine the relationship of one quantity to another. They always
return 1 (true) or (false) depending upon the outcome of the test. It refers to the
relationships that values can have with one another.

5.4.2.1 Relational Operators


Operator Action
> Greater than

>= Greater than or equal to

< Less than

<= Less than or equal to

== Equal to

!= Not equal to

= Used as assignment operator

1.4.1 Logical Operators


Logical operators are used to show the ways through which these relationships.

Operator Action
&& And

||
or
! not

1.4.1 Bitwise Operator


Bitwise operations are the testing, setting or shifting of the actual bits in a
byte or a word, which corresponds to C’s standard char and int data types and
variants. Bitwise operators cannot be used on type float, double, long double, void
or other more complex types.
Operator Action

& AND
| OR
^ Exclusive OR(XOR)
~ One’s complement

>> Shift right

<< Shift left

1.4.2 The ? Operator

? Operator is a very powerful and convenient operator that can be used to


replace certain statements of the if-then-else form.

Example: y = x > 9 ? 100:200 Is equivalent to if (x>9)

y = 100;

else y = 200;

5.5 Evaluation of Expression

Expression refers to anything that evaluates to a numeric value.


Order Precedence

(), !, unary +, Highest


- , * , /, %,
binary +, -,
< , <= , > , >=, ==, !=, &&, || Lowest

Examples:
A. Evaluate the following:

1. Given : z =5; a=3; w=2; y= -5

Expression: z – (a*b / 2) + w * y
5 3 9 2 -5

|__| |___|
27 -10
|____|
13
|_____|
-8
|____________|
-18

B. Evaluate the following expressions and determine whether they yield a value of TRUE or
FALSE
1. Given: dei = 0; y = 4.0; z = 2.0; x =3.0

Expression:! dei || ( y + z > = x – z )


0 4.0 2.0 3.0 2.0
|___| |____| |____|
1.0 1.0

|________|
1
|___________| 1 = TRUE We had just finished the
discussion on Data types,

Operators, Expression and Variables. Let’s move on to the next


higher level of activities or exercises that demonstrates your
potential skills/knowledge of what you have learned.

You might also like