Professional Documents
Culture Documents
1 An Intro To Prog in C
1 An Intro To Prog in C
Terminologies
Computer
Programming Hardware
Intro Software
Program
Programming Language
A Computer
A device that is
programmable
electronic
outputs information.
Software Software
2 main categories: Application software
System software
helps users to solve problems and
Application software
accomplish tasks.
System Software
Hardware vs
Programming
Software
A computer program
Hardware? a set of instructions that when run cause the computer to behave
in a predetermined way.
Computer Programming
Language Language
To be useful and reliable, a language must have
a well defined syntax and semantics.
Syntax:
Not all languages are "reliable" The structure and punctuation of the
means of communication. language.
Semantics:
Syntax Syntax
An Example using English An Example using English
Structure:
NOTE:
Sentences have a subject (who/what the
sentence is about) and a predicate In order to write a program, you
(information about the subject). must use a computer language.
Programming
Language Levels
Programming Programming
Language Levels Language Levels
High level language instruction:
One Low level language instruction
machine language instruction = 1:M
translates to one instruction the
computer can understand Abstracts from low level processor
operations e.g. memory access
low level language
instruction:machine language User friendly
instruction = 1:1
Practically platform-independent
Evolution of 1GL/Machine
Computer Languages: Languages
1940’s – Machine Languages
What computers understand.
1950’s – Symbolic Languages
every program that you
1960’s through present – High-Level write will be translated to
Languages machine language before
it is executed by a
Present to Future – Natural
computer.
Languages
Strings of 1's
and 0’s stored
as binary
numbers
1GL 1GL
CriticalPast. (2014, June 28). Women technicians work on an Electronic Numerical Integrator And
Computer (ENIAC)...HD Stock Footage [Video]. YouTube.
Videos
Early programming
CriticalPast. (2014, June 28). Women technicians work on an Electronic Numerical Integrator And
Computer (ENIAC)...HD Stock Footage [Video]. YouTube. https://www.youtube.com/watch?v=to-50Kydrj4
plug-boards, or
front-panel switches.
1GL (First Generation
Computer
Programming Language):
Machine Language Languages
Later… At the lowest level computers are exchanging
information in binary format (0,1).
Programs written as sequences of Problem:
hexadecimal numbers. Combination’s of 0's and 1's can have
different meanings.
These were input via the terminals’
Depending on the "encoding scheme” used.
keyboards and monitors
E.g.:
Each hexadecimal digit represents a Assembly ! 00001001 ! (Use register 9)
four binary digit sequence. ASCII ! 00001001 ! (Tab character)
1GL
2GL – SYMBOLIC
LANGUAGES
Complex and tedious to write.
Created in the 1950’s.
Practical only for very
The idea:
small programs.
have each type of instruction
Not portable. represented by some intuitive
Program for each processor code word, and
type. allow data to be represented
as decimal numbers.
2GL – SYMBOLIC 2GL – SYMBOLIC
LANGUAGES LANGUAGES
pushal 3(r2)
calls #2,SCANF
mull3 –8(fp),-12(fp)
Example 2:
pusha 6(r2) Mov ax, 1
There are recognisable Mov bx, 2
words, numbers and some
Add bx
other symbols.
LDAA $80
you want to carry out a very
simple task LDAB $81
STAA $55
you have MC 6800 computer
hardware What do these LOC do?
1.1 General 1.1 General
Definition Definition
LDAA $80 —> Loading the number at
memory location 80
We’re simply adding 2 numbers!
LDAB $81 —> Loading the number at
memory location 81 We considered memory locations
and used our hardware knowledge
ADDB —> Adding these two numbers
of the system.
STAA $55 —> Storing the sum to memory
location 55
Translators
the assembler.
Translators:
Translators
A Simple Summary:
Translators:
A Simple Summary:
Compiler: takes input and produces
output:
11 volunteers
2 groups: 5 + 6
Compiler Analogy:
I need to communicate information to two people, McA (machine A:) and
Group 1: assembler analogy
McB.
Each speaks a different language from each other, languages that I don’t
understand.
Group 2: compiler analogy
These two people have their own translators (AsA and AsB) that only
understand the language I speak and that of their respective employers.
I get an international multilingual translator CompA2Z, who has access to
AsA and AsB.
Translators:
A Simple Summary:
Translators
A Deeper Delve
Translators Translators
A Deeper Delve A Deeper Delve
Compiling: 3 steps:
Translators Translators
A Deeper Delve A Deeper Delve
Compiler Summary:
3. The linker merges object code
Compiler: High Level to Assembly
produced from all the source files Language
composing the program with any
libraries code included in the Assembler: Assembly to object code (m/
c)
program to form a binary
“executable image” of the program. Linker: Object code + library =
executable file
Compile|Link/Load|Execute
A Summary
Translators
Task
A Deeper Delve
Before our next class…
Watch this video: The Java Virtual
Watch video:
Machine and Compilers Explained
The Art of Writing Software
https://www.youtube.com/watch?
v=XjNwyXx2os8 https://www.youtube.com/watch?
v=QdVFvsCWXrA
Objectives
To describe the pros/features of C
LAB
To explain what an IDE is
C C
1983-1988: A committee of the American
National Standards Institute (ANSI) establishes a
A language that is:
modern, comprehensive definition of C. portable,
Commonly known as ANSI C.
It is a small language
C Features/Pros C Features/Pros
Programmer oriented: you have access to low level
functionalities e.g. you can manipulate the memory.
Is a subset of C++. Hence it’s compact, efficient, and fast … but also
dangerous
Is compact, readable and writable
It’s portable.
A core skill when seeking Some systems come with the C compiler built in.
Is powerful.
Type the instructions using:
E.g. you can easily use it to e.g.:
compiling,
linking
program execution.
File > New > Project Get rid of the sample file (main.c).
Still under the Projects tab: Don’t omit the extension (.c)
Hello World!
Create a new project called hello_world.
Our First (Real) Get rid of the main.c file that is automatically created for
you.
It will also show any compilation errors. (Hint: modify the printf() statement).
0”)
/* First C program: Hello World
#include <stdio.h>
int main()
Edit, Recompile…
{
printf("Hello, World!\n”);
return 0;
Again:
Run
#include <stdio.h>
Dealing with Errors
int main()
{
Compile/build it
printf(“Oh no! Error! :( \n”)
Run it.
return 0;
}
Dealing with Errors Dealing with Errors
Not exactly though (it’s actually in line 4)… A semi-colon is missing hence two
statements ran into each other.
The compiler doesn’t realise something is
wrong until Line 5. No semicolon at the end of Line 4.
Dealing with Errors Dealing with Errors
With Code::Blocks:
return 0;
Very friendly :)
Very friendly :)
#include <stdio.h>
Dealing with Errors int main()
May not show the line number. creates a temporary object code
(.o) file
In such a case, just search for the
incorrect text. deletes it
Types of Errors
Syntax/Compiler
Runtime
Logical
The program is NOT built before the errors are Pieces of the program are built, but errors prevent them
caught being merged.
Task
language, form of thinking and conceptualisation. We could just tell the computer what to do
using natural language, and it would do it.
Portable and easily expandable
A lot of interest and research into Natural
Designed to reduce the overall time, effort and cost of
Language Processing.
software development.
Examples include query languages for extracting data E.g. Prolog, OPS5 and Mercury
from databases, data warehouses and big data.
COBOL C++
Slower
PL/I LISP
Need more resources
BASIC Prolog
PASCAL Logo
Name 1o others.
C Sidenote…
On the one hand…
C Sidenote… C Sidenote…
On the other hand…
C Sidenote… C Sidenote…
LAB Programming in C
What is programming?
Series of instructions to a computer to
accomplish a task
Revision
Instructions must be written in a way the
computer can understand
C C
1972:
1983-1988:
Dennis Ritchie creates first version
ANSI C
of C
A modern, comprehensive
1978:
definition of C
First edition of “The C Programming
Established by an ANSI committee.
Language” by Kernighan and Ritchie
C C
1988:
C is a high level language
Second edition of “The C
must be converted to machine
Programming Language” by
language before it can be executed.
Kernighan and Ritchie
C
Source file
C Programming C Programming
Consists of variables and functions.
These functions could be either
We shall delve deeper into these later
C Programming C Programming
ANSI C includes several libraries of
To call such library functions, you
functions that are provided to
must tell the compiler which
programmers.
libraries you are going to use.
These libraries
There must be a program that will
provide important functionality and attach these libraries to your
executable.
simplify programming.
C Programming
Linker
Objective
Hello World!
Hello World! Explained
#include <stdio.h> #include
Hello World!
A C Program
Explained
int main
Hello World!
Explained
The new command prompt will be on the
line below.
• Send both a spreadsheet with your member details and the REMEMBER
two .c files to my email address, latest one day before our 1. Send these documents to my email address latest one day before our
next class. next class.
• The subject of the email must be [CourseCode]Prog The subject of the email must be [CourseCode]Prog Assignment2
Assignment2
2.
235
Global Declarations
Statements
The C Program
Structure
i.e.:
the author,
Comments are not compiled:
date,
i. Documentation i. Documentation
Section Section
If not compiled, what’s the point of having them?
Comments at the top of the program
Several reasons - we use them to:
i. Documentation
Section
Readability/Dependencies
Readability int main()
printf("Hello, World!\n");
2.Whitespace/Indentations vs
return 0;
C ignores white space.
}
E.g. the compiler doesn’t care about indentation.
link the programmer’s functions To include them (using the #include preprocessor directive),
enclose the file name in either <> for standard library files or " "
with files from the C library. for the ones you've created yourself (user defined files).
Notice the chevrons (angle " " (double quote) tells the preprocessor to first look in the current
directory for your user defined header file i.e., the one that you
brackets < >) enclosing the stdio.h created yourself.
ii. Link/Header ii. Link/Header
Declaration Section Declaration Section
#include<stdio.h> #include<stdio.h>
printf(),
clrscr(),
getch().
ii. Link/Header
Header Files
Declaration Section
Are a software reuse technique. #include<xxx.h>
compiler directives
Define any symbolic constants here.
preprocessor directives
pre-information
iv. Global v. Main() Function
Declaration Section Section
Declare Special function in C that is invoked
when you run a C program.
any global variables here.
Any C program must have one and only
variables used by more than one one main() function in order to execute.
function.
This is where the program starts
execution from.
all functions you will define in
your program. It is the entry point to the program.
Note
All C statements are terminated with a semi colon.