3. Contents
• Introduction to C Programming Language
• History
• Simple Program
• Explanation of the Code
• IDE: Dev C++
• Algorithm Writing & Logic Development
• Algorithm
• Writing an algorithm
• Logic
• Developing Logic
• Flowchart Basics
• Shapes
• Flow
• Symbols & Tutorial
• IDE: Visio, creately.com
4. Introduction to C Programming
• The C language facilitates a structured and disciplined approach to computer
program design.
• Created in 1972 to write operating systems (Unix in particular)
• By Dennis Ritchie
• Bell Labs
• Evolved from B
• Can be portable to other hardware (with careful design – use Plauger’s The
Standard C Library book)
• Built for performance and memory management – operating systems, embedded
systems, real-time systems, communication systems
• It is usually used for programming Microcontrollers
• C language natively doesn't support class but has structures
5. Brief History of development
• All history available here
• 1989 ANSI and ISO -> Standard C
• 1999 C99
• 2011 C11
• Don’t get thrown when you lookup information on websites and find
conflicts based upon standards
• 1979 C++ by Bjarn Stroustrup also at Bell
• Object orientation
• 1991 Java by Sun
• Partial compile to java bytecode: virtual machine code
• Write once, run anywhere
• Memory manager – garbage collection
• Many JVMs written in C / C++
6. Compiler, Interpreter and Assembler
Compilers
• A compiler takes the source code as a whole and translates it into object code all in one go. Once
converted, the object code can be run at any time. This process is called compilation. C uses
compiler.
Interpreters
• An interpreter translates source code into object code one instruction at a time. It is similar to a
human translator translating what a person says into another language, sentence by sentence.
The resulting object code is then executed immediately. The process is called interpretation.
Assemblers
• Assemblers are a third type of translator. The purpose of an assembler is to translate assembly
language into object code. Whereas compilers and interpreters generate many machine
code instructions for each high-level instruction, assemblers create one machine code instruction
for each assembly instruction.
• Learn more here
7. Loader
Loader
• A loader is a piece of software that is responsible for loading
executable files into memory to enable the central processing unit
(CPU) to run them. It calculates the size of a program and dedicates
enough memory space for it to run efficiently.
8. Syntax and Symantec Error
• Syntax is not following the rules of a language
• I eating apple
• A=X+-B
• 2+2=2 (in c language)
• Symantec is following language rules but writing a wrong logic
• One dozen of milk
• Two and two makes five
11. A Simple C Program: Printing a Line of Text (Cont.)
• Lines 1 and 2
• /* Fig. 2.1: fig02_01.c
A first program in C */
• begin with /* and end with */ indicating that these two lines are a
comment.
• You insert comments to document programs and improve program
readability.
• Comments do not cause the computer to perform any action when the
program is run.
12. A Simple C Program: Printing a Line of Text
(Cont.)
• Comments are ignored by the C compiler and do not cause any
machine-language object code to be generated.
• Comments also help other people read and understand your program.
13. A Simple C Program: Printing a Line of Text
(Cont.)
• C99 also includes the C++ language’s // single-line comments in which
everything from // to the end of the line is a comment.
• These can be used as standalone comments on lines by themselves or as end-
of-line comments to the right of a partial line of code.
• Some programmers prefer // comments because they’re shorter and they
eliminate the common programming errors that occur with /* */ comments.
• Line 3
• #include <stdio.h>
• is a directive to the C preprocessor.
14. A Simple C Program: Printing a Line of Text
(Cont.)
• Lines beginning with # are processed by the preprocessor before the program
is compiled.
• Line 3 tells the preprocessor to include the contents of the standard
input/output header (<stdio.h>) in the program.
• This header contains information used by the compiler when compiling calls to
standard input/output library functions such as printf.
• Line 6
• int main( void )
• is a part of every C program.
• The parentheses after main indicate that main is a program building block
called a function.
15. A Simple C Program: Printing a Line of Text
(Cont.)
• C programs contain one or more functions, one of which must be
main.
• Every program in C begins executing at the function main.
• The keyword int to the left of main indicates that main “returns”
an integer (whole number) value.
• We’ll explain what it means for a function to “return a value” when we
demonstrate how to create your own functions in Chapter 5.
16. A Simple C Program: Printing a Line of Text
(Cont.)
• For now, simply include the keyword int to the left of main in each
of your programs.
• Functions also can receive information when they’re called upon to
execute.
• The void in parentheses here means that main does not receive any
information.
17. A Simple C Program: Printing a Line of Text
(Cont.)
• A left brace, {, begins the body of every function (line 7).
• A corresponding right brace ends each function (line 11).
• This pair of braces and the portion of the program between the braces is called
a block.
• Line 8
• printf( "Welcome to C!n" );
• instructs the computer to perform an action, namely to print on the screen the
string of characters marked by the quotation marks.
• A string is sometimes called a character string, a message or a literal.
18. A Simple C Program: Printing a Line of Text
(Cont.)
• The entire line, including printf, its argument within the parentheses and
the semicolon (;), is called a statement.
• Every statement must end with a semicolon (also known as the statement
terminator).
• When the preceding printf statement is executed, it prints the message
Welcome to C! on the screen.
• The characters normally print exactly as they appear between the double
quotes in the printf statement.
• Notice that the characters n were not printed on the screen.
• The backslash () is called an escape character.
• It indicates that printf is supposed to do something out of the ordinary.
19. A Simple C Program: Printing a Line of Text
(Cont.)
• When encountering a backslash in a string, the compiler looks ahead at
the next character and combines it with the backslash to form an
escape sequence.
• The escape sequence n means newline.
• When a newline appears in the string output by a printf, the
newline causes the cursor to position to the beginning of the next line
on the screen.
• Some common escape sequences are listed in Fig. .
20. A Simple C Program: Printing a Line of Text
(Cont.)
• Because the backslash has special meaning in a string, i.e., the
compiler recognizes it as an escape character, we use a double
backslash () to place a single backslash in a string.
• Printing a double quote also presents a problem because double quotes
mark the boundary of a string—such quotes are not printed.
• By using the escape sequence " in a string to be output by printf,
we indicate that printf should display a double quote.
21. A Simple C Program: Printing a Line of Text
(Cont.)
• Line 10
• return 0; /* indicate that program ended successfully */
• is included at the end of every main function.
• The keyword return is one of several means we’ll use to exit a
function.
• When the return statement is used at the end of main as shown here,
the value 0 indicates that the program has terminated successfully.
• The right brace, }, (line 12) indicates that the end of main has been
reached.
22. A Simple C Program: Printing a Line of Text
(Cont.)
• We said that printf causes the computer to perform an action.
• As any program executes, it performs a variety of actions and makes
decisions.
• In Chapter 3, we discuss this action/decision model of programming in
depth.
23. A Simple C Program: Printing a Line of Text
(Cont.)
• Standard library functions like printf and scanf are not part of the C
programming language.
• For example, the compiler cannot find a spelling error in printf or scanf.
• When the compiler compiles a printf statement, it merely provides space in
the object program for a “call” to the library function.
• But the compiler does not know where the library functions are—the linker
does.
• When the linker runs, it locates the library functions and inserts the proper
calls to these library functions in the object program.
24. A Simple C Program: Printing a Line of Text
(Cont.)
• Now the object program is complete and ready to be executed.
• For this reason, the linked program is called an executable.
• If the function name is misspelled, it’s the linker that will spot the
error, because it will not be able to match the name in the C program
with the name of any known function in the libraries.
25. A Simple C Program: Printing a Line of Text
(Cont.)
• The printf function can print Welcome to C! several different
ways.
• For example, the program of Fig. 2.3 produces the same output as the
program of Fig. 2.1.
• This works because each printf resumes printing where the
previous printf stopped printing.
• The first printf (line 8) prints Welcome followed by a space and
the second printf (line 9) begins printing on the same line
immediately following the space.
26. A Simple C Program: Printing a Line of Text
(Cont.)
• One printf can print several lines by using additional newline
characters as in Fig. 2.4.
• Each time the n (newline) escape sequence is encountered, output
continues at the beginning of the next line.
27. Integrated Development Environment (IDE)
• https://www.bloodshed.net/
• Contains support for both C and C++
• Turbo C can also be used: https://turbo-c.apponic.com/
• Online Environment: https://www.onlinegdb.com/online_c_compiler
28. Algorithm writing and Logic Development
• In mathematics and computer science, an algorithm is a finite
sequence of well-defined, computer-implementable instructions,
typically to solve a class of problems or to perform a computation.
• Algorithms are always unambiguous and are used as specifications for
performing
1. Calculations
2. data processing
3. automated reasoning
and other tasks.
29. Simple Algorithm
• Adding two number
• Find a page
• Write first number
• Write second number below first number while taking care of digit and
decimal places
• Add the first digit places
• Add the portion as carry to the second digit place if the sum of these is
greater than 9
• Else, write the number as added.
• Continue till there is no number on the right side of the numbers.
30. Logic
• Logic is the theory behind any program that will be executed
• Logic can be wrong or write, In either case, the program will run
• Sometimes we think that the logic is correct from certain outputs but
in actual it is not
• Logic is very different from Logic Programming
• A logic behind a program is called complete logic if the it specifies all
the condition that are probable to be happen, then, if not now.
31. Logic and Algorithm
• The statements of logic are termed as algorithm
• Executing any idea required certain steps. Each of these steps are
termed as logical statements.
• Algorithm is the set of instructions (logical steps) that are to be
followed for executing a certain task
32. Making a picnic program (developing logic)
• Select a place (it should be reachable within dates)
• Find the commute (in budget and fastest)
• Find the costs (minimize each events cost so that it is at least
affordable)
• Select friends who will go (those who are willing to go and free)
• Fix the dates and timing (manageable timing and durations)
• Decide tasks that will be performed on going, staying, coming back
• Bring, inform, plan or buy ingredients for each task/event
• Create and execution Plan (the logical steps to follow)
33. Execution plan
• If the date is due
• Call your one friend to bring car
• Call other friends to come to a single meet point
• Put the tents, edibles and other stuff in the trunk
• Fuel it enough to reach the destination
• Build the camp
• Do photography
• Prepare the meal and eat it
• Night Photography or Music
• Sleep
• Roam in the locality all day
• Repeat Task X:
• Go back to Home city
• Drop off all friends
• Calculate and share expenses
Task X:
34. Flow Charting
• A flowchart is a diagram that depicts the “flow” of a program.
• The figure shown here is a flowchart for the pay-calculating
program shown.
• A flow chart can be Programming Flow chart or it can be
process flowchart in which you may see different images or clip
arts etc. Such chart is known as composite flow chart.
START
Display message “How many
hours did you work?”
Read Hours
Display message “How much do
you get paid per hour?”
Read PayRate
Multiply Hours by PayRate.
Store result in GrossPay.
Display GrossPay
END
36. Terminals
• Start and End are termed as terminals
• represented by rounded rectangles
• indicate a starting or ending point
START
END
37. Processes
Process is a task done in a program/algorithm
• Represented by rectangles
• Indicates a process such as a mathematical computation or variable
assignment
Multiply Hours
by PayRate.
Store result in
GrossPay.
39. Input/output
It represents the requirements of a program or displays certain data
• Represented by parallelograms
• indicate an input or output operation
Display message
“How many
hours did you
work?”
Read Hours
40. Four types of flow
• Sequence
• Decision
• Repetition
• Case
41. Sequence Structure
• A series of actions are performed in sequence
• The pay-calculating example was a sequence flowchart.
41
42. Decision Structure
• The flowchart segment below shows how a decision structure is expressed in C++
as an if/else statement.
42
YESNO
x < y?
Calculate a
as x times 2.
Calculate a
as x plus y.
if (x < y)
a = x * 2;
else
a = x + y;
Flowchart C Code
43. Decision Structure
• The flowchart segment below shows a decision structure with only one action to
perform. It is expressed as an if statement in C++ code.
43
if (x < y)
a = x * 2;
Flowchart C Code
YESNO
x < y?
Calculate a
as x times 2.
44. Repetition Structure
• The flowchart segment below shows a repetition structure expressed in C as a
while loop.
44
while (x < y)
x++;
Flowchart C Code
x < y? Add 1 to x
YES
45. Controlling a Repetition Structure
• The action performed by a repetition structure must eventually cause the loop to
terminate. Otherwise, an infinite loop is created.
• In this flowchart segment, x is never changed. Once the loop starts, it will never
end.
• QUESTION: How can this
flowchart be modified so
it is no longer an infinite
loop?
45
x < y? Display x
YES
46. Controlling a Repetition Structure
• ANSWER: By adding an action within the repetition that changes the value of x.
46
x < y? Display x Add 1 to x
YES
47. Case Structure
47
CASE
years_employed
1 2 3 Other
bonus = 100 bonus = 200 bonus = 400 bonus = 800
If years_employed = 1,
bonus is set to 100
If years_employed = 2,
bonus is set to 200
If years_employed = 3,
bonus is set to 400
If years_employed is
any other value, bonus
is set to 800
48. IDE for Flow chart
• http://www.creately.com/
• Microsoft Visio (Office365 service) PAID/Crack