Some Terminologies
• Algorithm
– A step-by-step procedure for solving a particular problem.
– It has finite number of steps
– Independent of the programming language.
Algorithms can be represented in one of the three ways
also:
• flowchart
• structured English
• psuedo-code
1
Some Terminologies
• Flowchart
A flowchart is a graphical representation of an algorithm.
Programmers often use it as a program-planning tool to solve a problem.
• Structured English: The use of the English language to describe the steps of an
algorithm in clear, unambiguous statements that can be read from start to
finish.
• Pseudocode: Pseudo code, as the name suggests, is a false code or a
representation of code which can be understood by even a person with some
school level programming knowledge. It’s simply an implementation of an
algorithm in the form of annotations and informative text written in plain
English.
2
Flowchart: Example
Problem solving
• Step 1:
– Clearly specify the problem to be solved.
• Step 2:
– Draw flowchart or write algorithm.
• Step 3:
– Convert flowchart (algorithm) into program code.
• Step 4:
– Compile the program into object code.
• Step 5:
– Execute the program.
5
Flowchart: basic symbols
Computation
Input / Output
Decision Box
Start / Stop
Flow of control
6
Example 1: Adding three numbers
• Step 1: Start.
• Step 2: Read the three number suppose “A",“B",“C"
form the user.
• Step 3: Declared a variable “SUM“.
• Step 4: SUM=A+B+C;
• Step 5: Display “SUM”.
• Step 6: End .
7
Example 1: Adding three numbers
START
READ A, B, C
SUM = A + B + C
OUTPUT SUM
STOP
8
Example 2: Larger of two numbers
• Step 1: Start.
• Step 2: Read two integer numbers suppose “X“ and
“Y" form the user.
• Step 3: Check if “X“ is greater than “Y" .
• Step 4: If true, then display “X“ as the greatest
number. Go to step 6.
• Step 5: If false, then display “Y“ as the greatest
number.
• Step 6: End .
9
Example 2: Larger of two numbers
START
READ X, Y
YES IS NO
X>Y?
OUTPUT X OUTPUT Y
STOP
Example 3: Largest of three numbers
• Step 1: Start.
• Step 2: Read three integer numbers suppose “X“, “Y"
and “Z“ form the user.
• Step 3: Check if “X“ is greater than “Y" .
• Step 4: If true, then declare a variable Max and assign
“X“ in Max. Go to step 6.
• Step 5: If false, declare a variable Max and assign “Y“ in
Max.
• Step 6: Check if Max is greater than “Z“ .
• Step 7: If true, then display Max. Go to step 9.
• Step 8: If false, then display “Z“ .
• Step 9: End 11
Example 3: Largest of three numbers
START
READ X, Y, Z
YES IS NO
X > Y?
Max = X Max = Y
YES IS NO
Max > Z?
OUTPUT Max OUTPUT Z
STOP STOP
12
Our First C Program: Hello World
#include <stdio.h>
main ( )
{
printf ("Hello, World!\n");
}
13
Our First C Program: Hello World
Preprocessor
#include <stdio.h> Comments are good
/* This program prints “Hello World” */
main( ) means “start here”
main()
{
printf(“Hello World!\n”);
}
Library command
Brackets define code blocks
14
Our First C Program: Hello World
(Cont..)
• Comments
Text surrounded by /* and */ is ignored by computer Used to
describe program
• #include <stdio.h>
Preprocessor directive (a kind of program, function declaration)
Tells computer to load contents of a certain file
<stdio.h> allows standard input/output operations
• int main()
– C programs contain one or more functions, exactly one of which
must be main
– Parenthesis used to indicate a function
– int means that main "returns" an integer value
– Braces ({ and }) indicate a block
• The bodies of all functions must be contained in braces
15
Our First C Program: Hello World
(Cont..)
• printf( "Welcome to C!\n" );
– Instructs computer to perform an action
• Specifically, prints the string of characters within
quotes (“ ”)
– Entire line called a statement
• All statements must end with a semicolon (;)
– Escape character (\)
• Indicates that printf should do something out of the
ordinary
• \n is the newline character
16
Our First C Program: Hello World
(Cont..)
• return 0;
– A way to exit a function
– return 0, in this case, means that the program
terminated normally
• Right brace }
– Indicates end of main has been reached
• Linker
– When a function is called, linker locates it in the library
– Inserts it into object program
– If function name is misspelled, the linker will produce
an error because it will not be able to find function in
the library
17
Structure of a C program
• Every C program consists of one or more functions.
– One of the functions must be called main.
– The program will always begin by executing the main
function.
• Each function must contain:
– A function heading, which consists of the function
name, followed by an optional list of arguments
enclosed in parentheses.
– A compound statement, which comprises the
remainder of the function.
18
Desirable Programming Style
• Clarity
– The program should be clearly written.
– It should be easy to follow the program logic.
• Meaningful variable names
– Make variable/constant names meaningful to enhance program
clarity.
• ‘area’ instead of ‘a’
• ‘radius’ instead of ‘r’
• Program documentation
– Insert comments in the program to make it easy to understand.
– Never use too many comments.
• Program indentation
-- Use proper indentation.
– Structure of the program should be immediately visible. 19
Indentation Example: Good Style
#include <stdio.h>
/* FIND THE LARGEST OF THREE NUMBERS */
main()
{
int a, b, c;
scanf(“%d%d%d”, &a, &b, &c);
if ((a>b) && (a>c))
printf(“\n Largest is %d”, a);
else
if (b>c)
printf(“\n Largest is %d”, b);
else
printf(“\n Largest is %d”, c);
}
20
Indentation Example: Bad Style
#include <stdio.h>
/* FIND THE LARGEST OF THREE NUMBERS */
main()
{
int a, b, c;
scanf(“%d%d%d”, &a, &b, &c);
if ((a>b) && (a>c))
printf(“\n Largest is %d”, a);
else
if (b>c)
printf(“\n Largest is %d”, b);
else
printf(“\n Largest is %d”, c);
}
21
Keywords of C
• Flow control (6) – if, else, return,
switch, case, default
• Loops (5) – for, do, while, break,
continue
• Common types (5) – int, float, double,
char, void
• Structures (3) – struct, typedef, union
• Counting and sizing things (2) – enum, sizeof
22
Keywords of C (Cont..)
• Rare but still useful types (6) – extern, signed,
unsigned, long, short, const
• Evil keywords which we avoid (1) – goto
• (1) – auto
23
The C Character Set
• The C language alphabet:
– Uppercase letters ‘A’ to ‘Z’
– Lowercase letters ‘a’ to ‘z’
– Digits ‘0’ to ‘9’
– Certain special characters:
! # % ^ & * ( )
- _ + = ~ [ ] \
| ; : ‘ “ { } ,
. < > / ? blank
24
Some simple operations for variables
• In addition to +, -, * and / we can also use
+=, -=, *=, /=, -- and % (modulo)
n++ increment n
n-- decrement n
a+=5 is equivalent to a = a+5;
a-=5 is equivalent to a = a-5;
a*=5 is equivalent to a = a*5;
a/=5 is equivalent to a = a/5;
(x % y) gives the remainder when x is divided by y 25
Identifiers and Keywords
• Identifiers
– Names given to various program elements (variables, constants,
functions, etc.)
– May consist of letters, digits and the underscore (‘_’) character, with no
space between.
– First character must be a letter or underscore.
– An identifier can be arbitrary long.
• Some C compilers recognize only the first few characters of the name (16 or 31).
– Case sensitive
• ‘area’, ‘AREA’ and ‘Area’ are all different. 26
Valid and Invalid Identifiers
• Valid identifiers • Invalid identifiers
X 10abc
abc my-name
simple_interest “hello”
a123 simple interest
LIST (area)
stud_name %rate
Empl_1
Empl_2
avg_empl_salary
27
C Variables Names (1)
Variable Names:
• Names may contain letters, digits and underscores
• The first character must be a letter or an underscore.
– the underscore can be used but watch out!!
• Case matters!
• C keywords cannot be be used as variable names.
present, hello, y2x3, r2d3, ... /* OK */
_1993_tar_return /* OK but don’t */
Hello#there /* illegal */
double /* shouldn’t work */
2fartogo /* illegal */ 28
C Variables Names (2)
Suggestions regarding variable names:
• DO: use variable names that are descriptive
• DO: adopt and stick to a standard naming convention
– sometimes it is useful to do this consistently for the
entire software development site
• AVOID: variable names starting with an underscore
– often used by the operating system and easy to miss
• AVOID: using uppercase only variable names
– generally these are pre-processor macros (later)
29
Data Types in C (1)
int :: integer quantity
Typically occupies 4 bytes (32 bits) in memory.
char :: single character
Typically occupies 1 byet (8 bits) in memory.
float :: floating-point number (a number with a
decimal point)
Typically occupies 4 bytes (32 bits) in memory.
double :: double-precision floating-point number 30
Data Types in C (2)
• There are a number of qualifiers which can be applied to the basic
types
– length of data
• short int: v "shorter" int, <= number of bits in an int
v can also just write "short"
• long int: v a "longer int", >= number of bits in an int
v often the same number of bits as an int
v can also just write "long"
• long double v generally extended precision floating point
31
Data Types in C (3)
– signed and unsigned
• unsigned int v an int type with no sign
v if int has 32-bits, range from 0..232-1
v also works with long and short
• unsigned charv a number from 0 to 255
• signed char va number from –128 to 127 (8-bit signed value)
v very similar to byte in Java
32
Some Examples of Data Types
• int
0, 25, -156, 12345, 99820
• char
‘a’, ‘A’, ‘*’, ‘/’, ‘ ’
• float
23.54, 0.00345, 25.0
33