C Prog - PowerPoint PPT Presentation

About This Presentation
Title:

C Prog

Description:

Programming – PowerPoint PPT presentation

Number of Views:0
Slides: 27
Provided by: Username withheld or not provided
Category:
Tags:

less

Transcript and Presenter's Notes

Title: C Prog


1
C PROGRAMMING ESSENTIALS
  • Chapter 1

DSE1103
GETTING STARTED WITH C PROGRAMMING
Mr. Bulega Herbert Moses
DIPLOMA IN SOFTWARE ENGINEERING (DSE) SEMESTER
ONE
JONATHAN WALLAS
2
Table of Contents
  • In this Course, lesson you will learn about
  • Getting Started with C Programming.
  • Basics of C programming.
  • Selection and Loop Statements in C.
  • Functions in C Programming.
  • Arrays in C Programming
  • Pointers in C Programming.
  • Structures in C Programming.

3
Learning Objectives
  • This lessons objectives are
  • Explore the meaning of Programming.
  • How to write C Programming Logic and Syntax
  • Discover what Algorithms are in C Programming?
  • Get knowledge about Algorithms, Pseudocode and
    Flow Charts, to Map Code
  • Acquire facts about History, Compilers and
    Benefits of Learning C.
  • To understand Programming Language
    Categories.
  • How to Setup a C Coding Environment with
    CODEBLOCKS.
  • How to Write a C Program Structure, Test and
    Debug it with CODEBLOCKS.

4
Computer and Programming Terms
A computer is an electronic device that accepts
raw data input, processes the data to produce the
desired output. A computer system has four main
parts Hardware, Software, Data and the User.
Computer Hardware are the physical parts that
make up a computer system for example Monitor,
Keyboard, System Unit, Mouse etc.
Computer Software is a set of programs that
govern the operations of a computer. A program is
a set of instructions written by a programmer
that a computer follows to perform a specific
task.
Computer Data are the raw meaningless facts
entered in a computer for processing e.g., the
data 11, February, and 1977 just represent
values.
Computer users are the people who interact with
computer systems also called human ware.
Programmers, data entry operators, system
analyst, computer hardware engineers all fall
into this category.
5
Computer Terms and Components (continued)
Programming is the skill and process of writing
computer instructions using a programming
language learned through lots of practice and
patience. Remember A program is a set of
instructions written by a programmer that a
computer follows to perform a specific task using
a CPU (Central Processing Unit). Its a set of
binary numbers (0s and 1s) arranged in some
order, which when given to the computer performs
some particular task.
Programming Language is a computer program that
provides a user-friendly environment to
programmers or software developers or for
creating software for example C, C, Visual
Basic, C, F etc.
Computer Programmer (Software Developer) is an
individual with the skills required to design,
create, and test computer program instructions
using a programming language.
Computers are dumb machines with tremendous
computational speed. Computers can give very
quick results to many difficult scientific
calculations but they can not perform the
required tasks on their own. They needs sets of
instructions to perform specific tasks. These
sets of computer instructions are kept in
computer programs. As formerly seen, computer
programs are in binary language i.e. series of 0s
and 1s. Numerous programs make computers
functional. So, without computer programs,
computers basically be unusable machines.
6
Programming Logic Overview
Programming logic is the development of orderly
actions that solve a specific problem.
Programmers apply logic to their programming
code to achieve the desired results. Programming
logic is not about the syntax of a programming
language. Programming logic is about the approach
to solving a problem and how developers use their
selected logic language programming tool to
attain this. Without it, programming languages
are just a set of keywords and symbols with no
meaningful actions. Programming logic gives
purpose and direction to these elements,
permitting developers to create software that is
both functional and efficient.
The Motive of Programming Logic Whether you are
coding in C, Java, Python, or C, the motive of
coding logic remains the same. It
includes ?Sequence The order in which the
computer instructions are executed or
implemented. ?Selection Decision-making with the
if-else statements, switch-case statements,
etc. ?Loops Repeating some actions using the for
loop, while loop, and the do-while
loops. ?Functions Groups of reusable code which
can be invoked anywhere in the program.
7
Programming Logic Overview (continued)
The Role of Programming Logic Programming logic
is a practical and critical skill all programmers
should master. Logical programming goes hand in
hand with critical thinking and problem-solving
skills that allow developers to ? Create
proficient algorithms. ? Debug code
effectively. ? Maintain and refactor code. ?
Collaborate with other developers. ? Learn new
languages quickly.
How to Improve Your Programming Logic Programming
logic is more than just the ability to code. Its
the difference between writing code and creating
solutions. As technology develops and new
programming languages emerge, the basics of
programming logic remain a constant, stable guide
for creators. A deep understanding of these
concepts paves way for software that is not only
functional and efficient but also elegant and
robust. In order to improve your Programming
logic ? Improve your basics ? Join Coding
Community ? Analyze code written by someone
else ? Solve Puzzles ? Use Pen and Paper ? Use
Flowchart ? Practice ? Patience
8
Algorithms in C Programming
Algorithm is a method of representing the
step-by-step logical procedures for solving a
problem. Algorithms are one of the most basic
tools used to develop the program solving logic.
Properties of an algorithm are ?Finite An
algorithm must terminate in a set number of
steps. ?Definite Each step of the algorithm
must be precisely defined such without ambiguity
or contradiction. ?Effectiveness Each step must
be effective, easily converted into program
statement and can be
performed exactly in the amount of
time. ?Generality An algorithm must be complete
so as to work well to solve a problem for which
it is defined. ?Input/Output Every algorithm
must have one or more quantities an input data
and give one or more output.
Algorithm 2, Add two numbers entered by the user
Step 1 Start Step 2 Declare variables num1,
num2 and sum. Step 3 Read values num1 and num2.
Step 4 Add num1 to num2 and assign result to
sum. Step 5 Display sum Step 6 Stop
Algorithm 1, Input and display three names. Step
1 Start Step 2 Declare the variables
firstname, middlename, lastname. Step 3 Read
values firstname, middlename and lastname. Step
4 Display all the firstname, middlename,
lastname at once. Step 5 Stop
9
Algorithms in C Programming (continued)
Example 3, Find the greatest of the two numbers.
Step 1 Start Step 2 Declare variables num1
and num2 Step 3 Read values num1 and num2.
Step 4 Check if num1 is greater than num2.
/Logic / If true, then print num1 as the
greatest number. If false, then print num2 as
the greatest number. Step 6 Stop
Example 4 Multiply two numbers and show
output. Step 1 Start Step 2 declare three
integers x, y z Step 3 define values of x
y Step 4 multiply values of x y Step 5
store result of step 4 to z Step 6 - print z Step
7 - Stop
Example 5 Write an algorithm to find the average
of 3 subjects. Step 1 Start Step 2 Declare
and Read 3 Subject, lets say s1, s2, s3. Step 3
Calculate sum for 3 subject values and store
result in sum. (S1S2S3) Step 4 Divide Sum by 3
and assign it to Average variable. (Average
Sum/3) Step 5 Print the value of Average of 3
Subjects Step 6 End of Solution
10
Algorithms in C Programming (continued)
Qualities of Good Algorithms ? Efficiency ?
Correctness ? Clarity ? Scalability ?
Reliability ? Optimality ? Robustness ?
Adaptability ? Simplicity
Advantages of Algorithms Problem-solving
Algorithms offer systematic approaches to handle
complex problems effectively. Efficiency
Algorithms simplify processes, leading to faster
and more optimized solutions. Reproducibility
Algorithms yield consistent results when provided
with the same inputs. Sub-Problems A programmer
can divide the tasks which makes them easier to
code. Automation Algorithms enable automation of
tasks, reducing the need for manual
intervention. Easy to understand Since it a
step-by-step method for a solution to a problem,
its easy to understand. Language Independent
Its easy to understand since its not dependent
on any programming language. Debug / Error
Finding Every step is independent so it will be
easy to spot and fix the error.
11
Algorithms in C Programming (continued)
Disadvantages of Algorithms Complexity
Developing difficult algorithms can be
challenging and time-consuming. Limitations Some
problems may not have efficient algorithms,
leading to suboptimal solutions. Resource
Intensive Certain algorithms may require
significant computational resources. Inaccuracy
Inappropriate algorithm design or implementation
can result in incorrect outputs. Maintenance As
technology evolves, algorithms may require
updates to stay relevant and effective.
Types of Algorithms ? Brute Force Algorithm ?
Recursive Algorithm ? Encryption Algorithm ?
Searching Algorithm ? Sorting Algorithm ?
Hashing Algorithm ? Divide and Conquer Algorithm
? Greedy Algorithm ? Randomized Algorithm
12
Pseudocode in C Programming
A pseudocode is a collection of plain English
instructions that represents a programmers
computer program or algorithm which solves a
problem.
PSEUDO CODE 1 Input and display Three Names
firstname, middlename, lastname Start Program
Enter three names firstname, middlename,
lastname. Read values of firstname, middlename
and lastname. Display all firstname, middlename,
lastname at once. End Program
PSEUDO CODE 2 Add two numbers entered by the
user Start Program Enter num1, num2 and sum.
Read values num1 and num2. Add num1 and num2
and assign the result to sum. sum?num1num2
Display the sum End Program
PSEUDO CODE 3 Find the greatest of two numbers
Start Program Enter two numbers num1 and num2.
Read values num1 and num2. Check if num1 is
greater than num2. /Logic / If true, then
print num1 as the greatest number. If false,
then print num2 as the greatest number. End
Program
13
Flow Charts in C Programming
A flowchart is a pictorial representation of an
algorithm where the steps are drawn, in form of
different shapes of boxes and the logical flow
indicated by inter connecting arrows. Once the
flowchart is drawn, it becomes easy to write a
program in any high level language.
SYMBOL SYMBOL NAME DESCRIPTION
Start/Stop Used at the beginning and end of the algorithm to show start and end of the program.
Process Indicates processes like mathematical operations.
Input/ Output Used for denoting program inputs and outputs.
Decision Stands for decision statements in a program, where answer is usually Yes or No.
Arrow Shows relationships between different shapes.
On-page Connector Connects two or more parts of a flowchart, which are on the same page.
14
Flow Charts in C Programming
Sample flow charts in C programming.
15
C Programming Language History
C Language was invented in the year 1972 by
Dennis Ritchie (Dennis MacAlistair Ritchie). An
American Computer Scientist who worked at Bell
Telephone Laboratories (Bell Labs) as a
researcher along with Ken Thompson New Jersey
USA. He is said to be the Father of C.
The C language was created for a specific
purpose to design the UNIX Operating System
(which is used on many computers). From the
start, C was intended to be useful to allow busy
programmers to get things done.
Other Programming languages that existed before
the C language include
?ALGOL (1960) ALGOL Programming Language by
ALGOL group.
?BPCL (1967) Basic Combined Programming
Language by Martin Richards.
?B (1970) B Programming Language by Ken
Thompson. Lastly
?C (1972) C Programming Language by Dennis
Ritchie at the ATT Bell Labs in New Jersey USA.
C language combined
features from B, BCPL Pascal, and ALGOL
languages. C language is
the mother of all programming languages.
16
Benefits of Learning C Programming Language
?Learning C programming as a first language is
very vital to beginners while studying other
programming languages. C shares similar
concepts like data types, operators, control
statements etc. ?C language has more than 32
keywords, various data types and a set of
powerful built-in functions that make
programming very efficient. ?C language can
extend itself. A C program has several functions
which are part of a library. We can add
functions to the C library and then access and
use them anytime. This makes it very easy to
work with the difficult C language big
projects. ?Many C language compilers are
available in the market that can be used for
executing programs written in the C
language. ?The C language is a highly portable
language meaning programs written in C can run on
other machines. ?C is a powerful and flexible
language. What you can accomplish with C is
limited only by your imagination. C is used
for big projects like operating systems, word
processors, spreadsheets, and even compilers. ?C
is modular. C code can be written in routines
called functions. These functions can be reused
in other programs. By passing pieces of
information to the functions, you can create
useful, reusable code
17
Programming Language Categories
Over the years, computer languages have evolved
from Low-Level to High-Level Languages. In the
earliest days, only binary language was used to
write programs. Low-Level Language (Machine
Language) is a language that computer systems can
directly understand. Binary Language is an
example of a low-level language made of 0s and
1s. A computer CPU directly executes binary
language so fast because they dont need
translation. Binary digits (1,0) instructions are
difficult to implement and their programs are not
portable, hard to maintain and are error prone.
LLL are First-Generation Language (1GL).
18
Programming Language Categories
Middle-level language is a language where
instructions are given in English like words e.g.
ADD, SUM, MOV called mnemonics e.g. Assembly
language. Binary code instructions in low-level
language are replaced with mnemonics in
middle-level language. Computers do not
understand mnemonics, so a translator called an
Assembler is used to translate mnemonics to
binary language. It takes assembly code and
produces machine code. Assembler is used to
translate middle-level language into low-level
language.
High-level language is a computer language that
is understood by humans. Computers do not
understand high-level languages. High-level
languages must first be converted into low-level
languages for computers to understand them. We
use a Compiler or an interpreter to convert a
high-level language into low-level language.
Languages such as COBOL, BASIC, C, JAVA etc are
high-level languages.
  • Advantages of Low-Level Languages (Machine
    Languages)
  • A computer can easily understand the low-level
    language.
  • Low-level language instructions are executed
    directly without any translation.
  • Low-level language instructions require very less
    time for their execution.

19
Programming Language Categories
  • Disadvantages of Low Level Languages (Machine
    Languages)
  • Low-level language instructions are very
    difficult to use and understand.
  • LLL instructions are machine-dependent, that
    means a program written for a particular machine
    does not execute on another machine.
  • In LLL, there is more chance for errors and it is
    very difficult to find errors, debug and modify.
  • Advantages of Middle Level Languages (Assembly
    Languages)
  • Writing instructions in a middle-level language
    is easier than writing instructions in a
    low-level language.
  • Middle-level language is more readable compared
    to low-level language.
  • Easy to understand, find errors and modify.
  • Disadvantages of Middle Level Languages (Assembly
    Languages)
  • Middle-level language is specific to a particular
    machine architecture, that means it is
    machine-dependent.
  • Middle-level language need to be translated into
    low-level language.
  • Middle-level language executes slower compared to
    low-level language.

20
Programming Language Categories
  • Advantages of High Level Languages
  • Writing instructions in a high-level language is
    easier.
  • A high-level language is more readable and
    understandable.
  • The programs created using high-level language
    runs on different machines with little change or
    no change.
  • Easy to understand, create programs, find errors
    and modify.
  • Disadvantages of High Level Languages
  • High-level language needs to be translated into
    low-level language.
  • High-level language executes slower compared to
    middle and low-level languages.

COMPILERS, INTERPRETERS AND TRANSLATORS We have
three types of translators i.e., a.Compiler,
b.Interpreter and c.Assembler. Compiler and
interpreter are used to convert the high-level
language into machine level language. A program
written in a high-level language is called a
source program and the corresponding machine
level language program called an object program.
Both the compiler and interpreter perform the
same task but their working is different. A
compiler reads the entire program at once and
searches for errors to lists them. When the
program is error free, its then converted into
object program. An interpreter reads one line at
a time of the source code and converts it to
object code.
21
Setup a C Coding Environment with CODEBLOCKS
The two things we need to begin programming in C
are a Computer and an IDE Compiler. A computer is
the main tool we shall use to write and compile
our source code. We also need a text editor to
write our source code and an IDE compiler to
translate our source code into a program. IDE
Compilers for the C programming language for
Windows, Mac and Linux are available. Examples
include
WINDOWS OS MAC OS LINUX OS
Code Blocks Code Blocks Code Blocks
Visual Studio Code Visual Studio Code Visual Studio Code
Eclipse Eclipse Eclipse
Codelite Codelite Codelite
CLion CLion CLion
NetBeans NetBeans NetBeans
Dev C Dev C Dev C
Lazarus IDE Lazarus IDE Lazarus IDE
Apache Arrow Apache Arrow Apache Arrow
Brackets Brackets Brackets
22
Setup a C Coding Environment with CODEBLOCKS
Step 1 Download CodeBlocks ?Go to a website
http//www.codeblocks.org/downloads ?Follow a
link to "Download a binary release" (direct
link) ?Go to the Windows 2000 / XP / Vista / 7
section search for file that has.
(22mingw-setup.exe). ?Save the file to your
desktop. Step 2 Install CodeBlocks ?Double
click the installer. ?Click next several times. I
assume you have installed in C\Program
Files\CodeBlocks. You can also install
elsewhere if you like. ?Do a Full
Installation. ?Launch CodeBlocks.
23
Setup a C Coding Environment with CODEBLOCKS
At the moment, you will have your main.c file,
which you can modify. For now, it just type
"Hello ISBAT University!", so we can run it as
is. Hit F9, which will first compile it and then
run it. You now have a running program! You can
simply edit main.c and then hit F9 to compile it
and run it again. Currently, you've finished
setting your compiler up and it's time to learn
to program in C.
24
C Program Structure
1.PREPROCESSOR include ltstdio.hgt
At the top is a Preprocessor command also called
a compiler directive used to include header
files. We use the include statement to add any
header file in a C program.
2.MAIN( )
All C programs must have the main( ) function.
Its where code execution starts in C when a
program starts. The include directive tells the
compiler to fetch the needed header files,
stdio.h.
3.OPENING AND CLOSING BRACES
The open brace indicates the beginning of a
block which belongs to the main method. In a C
program, every block begins with an open brace
symbol. The close brace indicates the
end of a block which belongs to the main method.
In C program, every block ends with a symbol.
Here, we declare variables and functions that are
local to the function or block in which they are
declared. A statement in C is terminated with a
semicolon .
25
C Program Structure
5.COMMENTS
COMMENTS are used to provide descriptions of
programs. You can put comments anywhere in a C
program--any text between the characters / and
/ is ignored by the compiler. That means they
are not executed. In C, there are two types of
comments. 1.Single Line Comments begin with //
symbol. We can write any number of single line
comments. 2.Multiple Lines Comments begin with
/ symbol and ends with /. We can write several
multiple lines comments in a program. Though,
well-chosen comments can make a program much
easier to read.
26
Thank you
Write a Comment
User Comments (0)
About PowerShow.com