Science 7 - LAND and SEA BREEZE and its Characteristics
INTRODUCTION TO C PROGRAMMING
1. JEENA SARA VIJU
ASSISTANT PROFESSOR
DEPARTMENT OF COMPUTER APPLICATIONS
MACFAST
JEENA SARA VIJU, ASST. PROF OF MCA1
INTRODUCTION TO
PROGRAMMING IN C
2. UNIT I
JEENA SARA VIJU, ASST. PROF OF MCA2
Introduction to algorithm, flowchart, structured
programming concept, programs – Compiler,
Interpreter.
Introduction to C Language: The C character set,
identifiers and keywords, data types, how floats and
doubles are stored,
constants,variables,arrays,declarations,expressions,
statements,Lvalues and Rvalues, type conversion,
symbolic constants.
3. ALGORITHM
JEENA SARA VIJU, ASST. PROF OF MCA3
An algorithm is a procedure or step-by-step instruction
for solving a problem. They form the foundation of
writing a program.
For writing any programs, the following has to be
known:
Input
Tasks to be preformed
Output expected
4. JEENA SARA VIJU, ASST. PROF OF MCA4
PROPERTIES OF ALGORITHM
1)Finiteness:An algorithm must always terminate after a
finite number of steps.
2)Definiteness:Each step of an algorithm must be
precisely defined. It is done by well thought actions to be
performed at each step of the algorithm.
3)Input:Any operation you perform need some beginning
value/quantities associated with different activities in the
operation. So the value/quantities are given to the
algorithm before it begins.
4)Output:One always expects output/result (expected
value/quantities) in terms of output from an algorithm.
The result may be obtained at different stages of the
algorithm.
5. JEENA SARA VIJU, ASST. PROF OF MCA5
If some result is from the intermediate stage of the
operation then it is known as intermediate result and
result obtained at the end of algorithm is known as end
result. The output is expected value/quantities always
have a specified relation to the inputs.
5)Effectiveness:
Algorithms to be developed/written using basic
operations. Actually operations should be basic, so that
even they can in principle be done exactly and in a finite
amount of time by a person, by using paper and pencil
only.
6. JEENA SARA VIJU, ASST. PROF OF MCA6
Example 1: Telling A Friend How To Boil Water In A
Kettle.
Let’s look at the 3 requirements for writing an algorithm
for this task:
Input – Kettle, Water
Tasks to be preformed – Fill up the kettle, Boil water
Output expected – Boiled water
Instructions to a Friend:
Step 1: Fill the kettle with water.
Step 2: Place it on the stove and turn on the burner.
Step 3: Turn off the burner when the water starts
boiling.
7. JEENA SARA VIJU, ASST. PROF OF MCA7
Instructions to the Computer:
Step 1: Start.
Step 2: Put the kettle under the tap.
Step 3: Turn on the tap.
Step 4: Check if 90% of the kettle is filled.
Step 5: If not, repeat the above step.
Step 6: Turn off the tap.
Step 7: Place the kettle in the burner.
Step 8: Turn on the burner.
Step 9: Check if the water is 100 C
Step 10: If not, repeat the above step.
Step 11: Turn off the burner.
Step 12: Stop
8. ALGORITHM TO ADD TWO NUMBERS
JEENA SARA VIJU, ASST. PROF OF MCA8
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to
sum. sum←num1+num2
Step 5: Display sum
Step 6: Stop
9. FLOWCHART
JEENA SARA VIJU, ASST. PROF OF MCA9
Flowchart is a graphical representation of an algorithm.
Programmers often use it as a program-planning tool to
solve a problem.
It makes use of symbols which are connected among
them to indicate the flow of information and processing.
The process of drawing a flowchart for an algorithm is
known as “flowcharting”.
10. JEENA SARA VIJU, ASST. PROF OF MCA10
General Rules for flowcharting
1. All boxes of the flowchart are connected with Arrows.
(Not lines)
2. Flowchart symbols have an entry point on the top of
the symbol with no other entry points. The exit point for
all flowchart symbols is on the bottom except for the
Decision symbol.
3. The Decision symbol has two exit points; these can
be on the sides or the bottom and one side.
4. Generally a flowchart will flow from top to bottom.
However, an upward flow can be shown as long as it
does not exceed 3 symbols.
5. Connectors are used to connect breaks in the
flowchart.
6. All flow charts start with a Terminal or Predefined
12. FLOWCHART TO ADD TWO NUMBERS
JEENA SARA VIJU, ASST. PROF OF MCA12
13. ASSIGNMENTS
JEENA SARA VIJU, ASST. PROF OF MCA13
Algorithm & Flowchart :
To find the area of a circle of radius r.
Convert temperature Fahrenheit to Celsius.
To find whether a number is positive or negative.
14. STRUCTURED PROGRAMMING CONCEPT
JEENA SARA VIJU, ASST. PROF OF MCA14
Structured programming is the set of design and
implementation processes that yield well-structured
programs.
Also known as Modular Programming
A disciplined approach to programming
Programs are divided into small self contained
functions
Top-down design
Step-wise refinement using a restricted set of program
structures
TOP-DOWN DESIGN
A program is divided into a main module and its
related modules. Each module is in turn divided into
sub modules until the resulting modules are
15. JEENA SARA VIJU, ASST. PROF OF MCA15
PROGRAMS
A computer program is a collection of instructions that
performs a specific task when executed by a computer. A
computer requires programs to function.
A computer program is usually written by a computer
programmer in a programming language.
16. JEENA SARA VIJU, ASST. PROF OF MCA16
From the program in its human-readable form of
source code, a compiler can derive machine code - a
form consisting of instructions that the computer can
directly execute.
Alternatively, a computer program may be executed
with the aid of an interpreter.
PROGRAM CHARACTERISTICS
Integrity: This refers to the accuracy of the
calculations. It should be clear that all other program
enhancements will be meaningless if the calculations
are not carried out correctly. Thus, the integrity of the
calculations is an absolute necessity in any computer
program.
Clarity: refers to the overall readability of the
program, with particular emphasis on its underlying
logic. If a program is clearly written, it should be
possible for another programmer to follow the
17. JEENA SARA VIJU, ASST. PROF OF MCA17
Simplicity: The clarity and accuracy of a program are
usually enhanced by keeping things as simple as
possible, consistent with the overall program
objectives.
Efficiency: is concerned with execution speed and
efficient memory utilization. These are generally
important goals, though they should not be obtained
at the expense of clarity or simplicity.
Modularity: Many programs can be broken down into
a series of identifiable subtasks. It is good
programming practice to implement each of these
subtasks as a separate program module. In C, such
modules are written as functions.
Generality: Usually we will want a program to be as
general as possible, within reasonable limits. For
example, we may design a program to read in the
values of certain key parameters rather than placing
18. COMPILERS & INTERPRETERS
JEENA SARA VIJU, ASST. PROF OF MCA18
A computer program in the form of a human readable
is called source code. Source code may be converted
into an executable form by a compiler or executed
immediately with the aid of an interpreter.
Compilers are used to translate source code from a
programming language into either object code or
machine code.
Object code needs further processing to become
machine code, and machine code consists of the
central processing unit's native instructions, ready for
execution. Compiled computer programs are
commonly referred to as executables, binary images,
or simply as binaries – a reference to the binary file
format used to store the executable code.
Interpreters are used to execute source code from a
programming language line-by-line.
20. INTRODUCTION TO C LANGUAGE
JEENA SARA VIJU, ASST. PROF OF MCA20
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.
21. JEENA SARA VIJU, ASST. PROF OF MCA21
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
STRUCTURE OF C PROGRAM
Every C program consists of one or more modules
called functions. One of the functions must be called
main.
The program will always begin by executing the main
function, which may access other functions. Any other
function definitions must be defined separately, either
ahead of or after main.
23. THE C CHARACTER SET
JEENA SARA VIJU, ASST. PROF OF MCA23
C uses the uppercase letters A to Z, the lowercase
letters a to z, the digits 0 to 9, and certain special
characters as building blocks to form basic program
elements (e.g., constants, variables, operators,
expressions, etc.).
C uses certain combinations of characters, such as
b, n and t, to represent special conditions such as
backspace, newline and horizontal tab, respectively.
These character combinations are known as escape
sequences.
24. IDENTIFIERS & KEYWORDS
JEENA SARA VIJU, ASST. PROF OF MCA24
ldentifiers are names that are given to various program
elements, such as variables, functions and arrays.
Identifiers consist of letters and digits, in any order, except
that the first character must be a letter.
Both upper- and lowercase letters are permitted, though
common usage favours the use of lowercase letters for
most types of identifiers. Upper- and lowercase letters are
not interchangeable (i.e., an uppercase letter is not
equivalent to the corresponding lowercase letter.)
The underscore character ( - ) can also be included, and is
considered to be a letter. An underscore is often used in the
middle of an identifier. An identifier may also begin with an
underscore, though this is rarely done in practice.
25. JEENA SARA VIJU, ASST. PROF OF MCA25
Examples
X Y12 sum_1
_temperature
Names area tax_rate
TABLEINVALID IDENTIFIERS REASON
4th The first character must be
a letter.
“x” Illegal characters (“ “).
order-no Illegal character (-).
Error flag Illegal character (blank
space).
26. JEENA SARA VIJU, ASST. PROF OF MCA26
There are certain reserved words, called keywords,
that have standard, predefined meanings in C. These
keywords can be used only for their intended
purpose; they cannot be used as programmer-defined
identifiers.
The keywords are all lowercase.
27. DATA TYPES
JEENA SARA VIJU, ASST. PROF OF MCA27
Data types are declarations for memory locations or
variables that determine the characteristics of the data
that may be stored and the methods (operations) of
processing that are permitted involving them.
Data type determines the type of data a variable will
hold. If a variable x is declared as int. it means x can
hold only integer values.
C supports several different types of data, each of
which may be represented differently within the
computer’s memory.
int, char, float and double are the basic data types.
29. JEENA SARA VIJU, ASST. PROF OF MCA29
The basic data types can be augmented by the use of the
data type qualifiers : short , long, signed and unsigned. For
example, integer quantities can be defined as short int,
long int or unsigned int.
Thus, a short int may require less memory than an
ordinary int or it may require the same amount of memory
as an ordinary int , but it will never exceed an ordinary int
in word length. Similarly, a long int may require the same
amount of memory as an ordinary int or it may require
more memory, but it will never be less than an ordinary int
.
30. CONSTANTS
JEENA SARA VIJU, ASST. PROF OF MCA30
Constants refer to fixed values that the program may not
alter during its execution. These fixed values are also
called literals.
There are four basic types of constants in C.
integer constant, a floating constant, a character constant,
or a string literal. There are enumeration constants as well.
INTEGER CONSTANT
Integer-valued number. Consists of a sequence of digits.
Integer constants can be written in three different number
systems: decimal (base 10), octal (base 8) and
hexadecimal (base 16).
A decimal integer constant can consist of any combination
of digits taken from the set 0 through 9. If the constant
contains two or more digits, the first digit must be
something other than 0.
31. JEENA SARA VIJU, ASST. PROF OF MCA31
An octal integer constant can consist of any
combination of digits taken from the set 0 through 7.
The first digit must be 0,in order to identify the
constant as an octal number.
A hexadecimal integer constant must begin with
either Ox or OX followed by any combination of digits
taken from the sets 0 through 9 and a through f
(either upper- or lowercase).
Letters a through f (or A through F) represent the
(decimal) quantities 10 through 15, respectively.
UNSIGNED AND LONG INTEGER CONSTANTS
Unsigned integer constants may exceed the
magnitude of ordinary integer constants by
approximately a factor of 2, though they may not be
negative.
32. JEENA SARA VIJU, ASST. PROF OF MCA32
Identified by appending the letter U (either upper- or
lowercase) to the end of the constant.
Long integer constants may exceed the magnitude of
ordinary integer constants, but require more memory
within the computer. by appending the letter L (either
upper or lowercase) to the end of the constant.
An unsigned long integer may be specified by
appending the letters UL to the end of the constant.
FLOATING POINT CONSTANT
A floating-point constant is a base- 10 number that
contains either a decimal point or an exponent (or
both).
Floating-point constants have a much greater range
than integer constants.
33. JEENA SARA VIJU, ASST. PROF OF MCA33
CHARACTER CONSTANT
A character constant is a single character, enclosed in
apostrophes (i.e., single quotation marks).
Character constants have integer values.
Eg. ‘a’ ‘b’ ‘1’ ‘5’ ‘ ’ ‘?’
STRING CONSTANTS
A string constant consists of any number of
consecutive characters (including none), enclosed in
(double) quotation marks.
Eg. “green” “$19.95”
34. VARIABLES AND ARRAYS
JEENA SARA VIJU, ASST. PROF OF MCA34
A variable is an identifier that is used to represent
some specified type of information within a designated
portion of the program.
A variable is an identifier that is used to represent a
single data item; i.e., a numerical quantity or a
character constant.
The data item must be assigned to the variable at
some point in the program.
The data item can then be accessed later in the
program simply by referring to the variable name.
A given variable can be assigned different data items at
various places within the program.
Data type associated with the variable cannot change.
35. JEENA SARA VIJU, ASST. PROF OF MCA35
An array is an identifier that refers to a collection of
data items that all have the same name. The data
items must all be of the same type (e.g., all integers,
all characters, etc.).
The individual data items are represented by their
corresponding array-elements (i.e., the first data item
is represented by the first array element, etc.).
The individual array elements are distinguished from
one another by the value that is assigned to a
subscript.
Eg. char a, b, c, d;
a = ‘c’;
b = ‘a’;
c = ‘l’; etc
36. DECLARATIONS
JEENA SARA VIJU, ASST. PROF OF MCA36
A declaration associates a group of variables with a
specific data type.
All variables must be declared before they appear in
executable statements.
A declaration consists of a data type, followed by one
or more variable names, ending with a semicolon.
Each array variable must be followed by a pair of
square brackets, containing a positive integer which
specifies the size (i.e., the number of elements) of the
array.
37. JEENA SARA VIJU, ASST. PROF OF MCA37
int a, b, c ;
f loat rootl, root2;
char flag, text [80] ;
EXPRESSIONS
An expression represents a single data item, such as
a number or a character.
Consist of a single entity, such as a constant, a
variable, an array element or a reference to a
function.
Also consist of some combination of such entities,
interconnected by one or more operators.
Eg. a+b a=b a==c ++a
c=a+b
38. STATEMENTS
JEENA SARA VIJU, ASST. PROF OF MCA38
A statement causes the computer to carry out some
action.
Three different classes of statements in C : expression
statements, compound statements and control
statements.
An expression statement consists of an expression
followed by a semicolon.
a = 3;
c = a + b ;
++i;
A compound statement consists of several individual
statements enclosed within a pair of braces { }.
39. JEENA SARA VIJU, ASST. PROF OF MCA39
The individual statements may be expression
statements, compound statements or control
statements.
{
p i = 3.141593;
circumference = 2. * p i * radius;
area = p i * radius * radius;
}
Control statements are used to create special
program features, such as logical tests, loops and
branches.
while (count <= n) {
sum += n;
++count;
}
40. LVALUES & RVALUES
JEENA SARA VIJU, ASST. PROF OF MCA40
“l-value” refers to memory location which identifies an
object.
l-value may appear as either left hand or right hand
side of an assignment operator(=).
l-value often represents as identifier.
“r-value” refers to data value that is stored at some
address in memory.
A r-value is an expression that can’t have a value
assigned to it which means r-value can appear on
right but not on left hand side of an assignment
operator(=).
Eg. int a = 3;
41. TYPE CONVERSION
JEENA SARA VIJU, ASST. PROF OF MCA41
Refers to changing a variable of one data type into
another.
The compiler will automatically change one type of
data into another if it makes sense. For instance, if
you assign an integer value to a floating-point
variable, the compiler will convert the int to a float.
Implicit type conversion
Explicit type conversion
When the type conversion is performed automatically
by the compiler without programmers intervention,
such type of conversion is known as implicit type
conversion or type promotion.
42. JEENA SARA VIJU, ASST. PROF OF MCA42
The type conversion performed by the programmer by
posing the data type of the expression of specific type
is known as explicit type conversion. The explicit
type conversion is also known as type casting.
43. SYMBOLIC CONSTANT
JEENA SARA VIJU, ASST. PROF OF MCA43
A symbolic constant is a name that substitutes for a
sequence of characters. The characters may
represent a numeric constant, a character constant or
a string constant.
A symbolic constant allows a name to appear in place
of a numeric constant, a character constant or a
string.
When a program is compiled, each occurrence of a
symbolic constant is replaced by its corresponding
character sequence.
Usually defined at the beginning of a program.
Eg. #define pi 3.14