0% found this document useful (0 votes)
24 views

1b - Introduction To Programming in C

Uploaded by

Macdonald
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

1b - Introduction To Programming in C

Uploaded by

Macdonald
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

INTRODUCTION TO PROGRAMMING

IN C

10/23/2024 1
OUTLINE

 Definitions of Terms
 Programming Paradigms
 Compiler Terminology
 Errors
 C Language
 Programming Character Set

10/23/2024 2
PROGRAMS

 What is the general meaning?


 Noun
 A program of actions or events is a series of actions or events that are
planned to be done.
 Verb
 When you program a machine or system, you set its controls so that it
will work in a particular way.

10/23/2024 3
FOCUS

 Programming a machine
 Giving it instructions that it understands word for word
 Speak to it in a particular language
 How does the machine know which language?
 The machine must have some kind of brain in it (residing in processor, hard
drive etc.)
 What kind of machines?
 Phones, computers, scanners, conveyor belts, electric jugs, factory
machines, microwaves, smart-watches
 Which machine for this course?
 Computer

10/23/2024 4
WHY THE COMPUTER?

 Can be connected to other machines to make them work


 Program can be translated so that other machines understand it
 Computer serves as platform to test the program and simulate it
before mounting it on big machines
 Most machines now computer controlled

10/23/2024 5
SO HOW DO WE PROGRAM?

 First determine steps required to achieve a particular functionality


or task
 Algorithm

 Then translate the steps into precise steps


 Program

10/23/2024 6
ARE ALL PROGRAMS WRITTEN THE SAME
WAY?

 NO
 Different languages (means different ways of translating hence different
translators)
 Different syntax (i.e. Grammar and punctuation)
 Different functionalities achievable
 Some more complex than others

10/23/2024 7
SO LET’S BEGIN!

10/23/2024 8
WHAT IS AN ALGORITHM?

 An algorithm is a
 Series of STEPS which when followed achieve a TASK

 It is much like a recipe

Transf
ingre
ormat result
dient
ion s
s
steps
Tran
sfor
inpu mati outp
t on ut
step
s
10/23/2024 9
PROGRAMMING

 Telling the computer what to do


 Giving instructions in a language understood by the computer
 Instructions follow the algorithm
 Instructions are a formal writing of the algorithm
 Program = Complete set of formal instructions

 For accurate, expected results, Instructions must be


 Precise
 Unambiguous

10/23/2024 10
ACHIEVING ACCURATE RESULTS

 Logical sequential algorithm


 Clearly defined data to be used
 Clearly defined form of the input data
 Clearly defined form of the expected results (i.e. Output as we call
it)

10/23/2024 11
DISCUSSION 1

 Give an algorithm to prepare two whole fried eggs, unscrambled (2


minutes)

? Which
Ingredients
? In what form
? What Process
? What Output
? In what form

10/23/2024 12
DISCUSSION 2 – COMMENT ON THE RICE
ALGORITHMS

 Points to note:
 Algorithms are NOT specific
 in measures of ingredients
 on where or how the ingredients are stored,
 on utensils to be used,
 on size of utensils to be used
 on time , time varies per group and per exposure

 Results
 Inaccurate (too much rice or too little rice)
 Inconsistent (raw rice or overcooked rice)

10/23/2024 13
DISCUSSION 3 – SO WHAT ARE REQUIRED
CHARACTERISTICS OF PROGRAMS

 Requirements of algorithms and programs


 Accurate measures of inputs
 Accurate measures of time
 Accurate results
 Consistent results

10/23/2024 14
SIMPLE PROGRAMMING MODEL
 Fry two whole eggs.

• What is required before we start actually


cooking the rice?

input
10/23/2024 15
REAL PROGRAMMING MODEL

Input

Computer
machine
has data
Program
steps
ming =
writing
code to
manipula
te data

output

10/23/2024 16
DISCUSSION 4 (3 MINUTES)

 Give an algorithm to do two things:


1. add two numbers, display the result, then
2. multiply the numbers and output the multiplication result. (2 minutes)

 Given our previous discussion:


1. Describe the input, and output.
2. List the processing steps that transform the input into desired output.
 Issues:
 all using different languages to write  no consistency  varied
interpretations

10/23/2024 17
DIFFERENT APPROACHES TO PROGRAMMING

 Programming Paradigm:
 way of conceptualising what it means to perform computation and how
tasks to be carried out on a computer should be structured and organised
1. Imperative:
 how-to, algorithm explicit, goal implicit, use of functions e.g. procedural
programming, Fotran, C, C++, functional programming (such as Haskell)
2. Declarative:
 what-is, goal explicit, algorithm implicit, use of logical expressions e.g.
Prolog
3. Object-oriented:
 which objects , who? e.g. Smalltalk, C++, Java

10/23/2024 18
HOW DOES PROGRAMMING WORK?

Program
(instructions written
for someone to use
someone=user)

Compiler
(translates and
interprets
10/23/2024 19

instructions)
MORE ABOUT THE COMPILER

Source 1. Compiler:
program file Check syntax
(.c) errors

Produce object file


not yet linked to
other invoked
libraries (.obj, .o)

2. Linker: Build final


combine all
object files with executable
10/23/2024 20

libraries file (.exe)


ERRORS ENCOUNTERED WHEN
PROGRAMMING

Syntax (i.e. compiling) errors Runtime errors


 Un-matching braces/ brackets  Wrong results
 Wrong language constructs  Unintended output
 Registers used not defined  Division by zero (usually
 Registers not initialised caused by not controlling
input or wrong calculations)
 Invalid way of defining data
(according to language rules)

10/23/2024 21
SKILLS REQUIRED FROM YOU IN
PROGRAMMING

 Attention to detail
 Specific
 Unambiguous

 Knowing the computer is stupid


 Until you give it instructions.
 It will do exactly as you tell it to.

 Good memory
 Of syntax, defined names, names of prewritten functions

 Ability to think on several levels


 Compartmentalise into little box processes which perform useful tasks

10/23/2024 22
NOW LET’S TALK ABOUT C LANGUAGE

 Compact language
 Powerful (especially for efficient machine-dependent programming)
 Not as heavy as industrial-type languages like Php, Java
 Syntax heavily influenced industrial-type languages
 One of the best teaching languages.

10/23/2024 23
C LANGUAGE SPECIFICS

 Case sensitivity (all these are different!)

Myname2 myName2 Myname2


my_name2 MYname2 MYNAME2

 Uses a specific alphabet


 ASCII character set
 Each letter has different numeric equivalent
 Each letter represented as a binary number

10/23/2024 24
ASCII CHARACTER SET EXAMPLES
CHAR CODE CHAR CODE CHAR CODE
, 44 a 97 ± 241
- 45 ‗ 242
b 98
. 46 ¾ 243
c 99
/ 47 ¶ 244
d 100
0 48 § 245
1 49 e 101
÷ 246
2 50 f 102
¸ 247
3 51 g 103 ° 248
4 52 A 65 ¨ 249
5 53 B 66 · 250
6 54 C 67
7 55
10/23/2024 25
RUNNING C PROGRAMS

 Integrated Development Environment (IDE)


 With built-in compiler, linker, executor
 Better if it supports later standards like C99(ISO/IEC 9899:1999) and
C11(ISO/IEC 9899:2011).
 https://www.thefreecountry.com/compilers/cpp.shtml contains a list of varied
IDEs that support C programming.
 E.g. Bloodshed Dev C++, CodeBlocks, Orange C, Microsoft Visual Studio
Community (Microsoft C)

 Without built-in compiler and linker


 Requires installation of the GNU Compiler Collection gcc 4.9 to gcc 6.3 (or later)
from https://gcc.gnu.org

10/23/2024 26
SUMMARY

 Definition of programming:
 steps in defined language to accomplish a particular task

 Important requirements of algorithms and programs


 Precise, singular meaning, clear.

 Important properties of C
 versatile, learnable.
 Uses ASCII character set
 Case-sensitive

10/23/2024 27

You might also like