Title: CS115 Introduction to Programming
1CS115 Introduction to Programming
- Inst. Senem Kumova Metin
- senem.kumova_at_ieu.edu.tr
- Textbook A Book on C, A. Kelly and I.Pohl
- Lecture Notes http//homes.ieu.edu.tr/skumova/
- Office hours TBA
- Office 408
2WHAT is LANGUAGE ?
3WHAT is PROGRAMMING??scheduling or performing a
task or / and eventWHAT is COMPUTER
PROGRAMMING??creating a sequence of steps for a
computer to follow in performing a task
4WHAT is a PROGRAMMING LANGUAGE ?
A set of rules, symbols, and special words used
to construct a computer program
5Programming language rules consist of Rules of
Syntax which specify how valid instructions are
written in the language(like natural language
rules? subject verb object ) Rules of
Semantics which determine the meaning of the
instructions (what the computer will do)(like
natural language rules? A book has bitten a car )
6 A COMPUTER PROGRAM ?
- A set of machine instructions which in turn are
represented as sequences of binary digits
(0001010.111011) - The execution sequence of a group of machine
instructions is known as the flow of control.
7FLOW OF CONTROL
- SCENARIO
- you have 2 integers x,y
- if x is greater than 0 then
- do x y 1
- else
- do x y-1
- print the value of x
int x and int y
x gt 0
YES
NO
x y1
x y-1
print x
8Will we write codes in binary ??
- SCENARIO
- you have 2 integers x,y
- if x is greater than 0 then
- do x y 1
- else
- do x y-1
- print the value of x
000110001
100010.01
YES
NO
1011..0101
101010000
100..11100010
9ASSEMBLY LANGUAGE
- Assembly language (or assembler code) was our
first attempt at producing a mechanism for
writing programs that was more palatable to
ourselves
movl 0x1,n compare cmpl oxa,n cgt
end_of_loop acddl 0x1,n bra
compare end_of_loop
- Of course a program written in assembly code, in
order to run, must first be translated
(assembled) into machine code.
10HIGH LEVEL LANGUAGE
- From the foregoing we can see that assembler
language is not much of an improvement on machine
code! - A more problem-oriented (rather than
machine-oriented) mechanism for creating computer
programs would also be desirable. - Hence the advent of high(er) level languages
starts with the introduction of Autocodes, and
going on to Algol, Fortran, Pascal, Basic, Ada,
C, etc.
11PROGRAM PROCESSING
- A program written in a high level language
(source code) can only be run in its machine
code equivalent format. - SOURCE CODE ? MACHINE CODE
- There are two ways of achieving this
- Interpretation, and
- Compilation
121. INTERPRETATION
- Interpretation requires the use of a special
program that reads and reacts to source code. - Such a program is called an interpreter.
- During interpretation run-time errors may be
detected and meaningful error messages produced.
132. COMPILATION
- Compilation requires the use of a special program
(called a compiler) that translates source code
into object code. - SOURCE CODE ? OBJECT CODE
- Sometimes various library files must be linked
in using another special program called a
linker, which produces executable code. - OBJECT CODE ? MACHINE CODE
14LIBRARIES
- Libraries (in computer programming terms) contain
chunks of precompiled (object) code for various
functions and procedures that come with a
programming language that requires compilation - For example functions and procedures to
facilitate I/O.
15Why C?
- Native language of UNIX
- Standard development language for personal
computers - Portable (can be moved to other machine !)
- Powerful set of operators and powerful libraries
(some operators ,--.) - Basis for Java, C..
16 17INTRODUCTION TO C
- Your First C programs
-
- Basic I/O functions printf / scanf
- Including libraries
- Writing comments
- Defining variables .
- if statements
18Learn printf
- include ltstdio.hgt // library file
- void main(void)
-
- printf("from sea to shining C\n")
19Learn printf
- include ltstdio.hgt
- void main(void)
-
- printf("from sea )
- printf(to shining C\n")
20Learn printf / scanf
- include ltstdio.hgt
- void main(void)
-
- int x0
- printf(x d,x) // print x 0
- scanf(d,x) / scan the value from
screen and assign this value to x / - printf(d,x)
21Comments
- / Ignored part by
- the compiler /
- // Ignored part by the compiler (only this line)
- void main()
- //..
22Comments
- Comments are arbitrary strings of symbols placed
between the delimiters / and / - Comments are not tokens but white spaces for the
C compiler
c) / a comment
/
a) / a comment /
d) // / a comment /
//
b) / a comment /
23Variables and Assignment
- includeltstdio.hgt
- void main(void)
-
- int kurus // declarations of variables (int is a
keyword, takes integer values) - int lira0 // declaration and initialization of
a variable - int toplam_kurus
- lira 13 // Assignment statement, is the
assignment operator - kurus56 // Assignment
- printf( Money is d lira d kurus\n, lira,
kurus) // printf statement - toplam_kurus lira100 kurus
- printf( \n Total kurus is d kurus\n,
toplam_kurus) - // first part of the printf statement is a
control string
24Variables and Assignment
- OUTPUT
- Money is 13 lira 56 kurus
- Total kurus is 1356 kurus
25The use of define
- / Lines starting with are called preprocessing
directives - Preprocessor first changes all occurences of
identifier PI to 3.14 / - includeltstdio.hgt
- define PI 3.14
- void main(void)
-
- printf(PI equals f\n,PI)
- // f is used for floating numbers
-
26If Statements
C CODE void main() int x int y
if( xgt0) xy1 else xy-1
printf(d, x)
- SCENARIO/ MISSION
- you have 2 integers x,y
- if x is greater than 0 then
- do x y 1
- else
- do x y-1
- print the value of x
27if statements
MISSION Write a function called mymaximum that
gets 2 integer values (as input parameters) and
returns back the greater one
- int mymaximum( int a , int b)
- if(agtb)
- return a
- else
- return b
28Functions and if statements
- include ltstdio.hgt
- int mymaximum(int a, int b) // FUNCTION
PROTOTYPE - void main(void)
- int max, x, y 7 // DECLARE 3 VARIABLES,
INITIALIZE Y - printf("please give the value for x ")
- scanf("d",x) // GET THE VALUE OF VARIABLE X
- max mymaximum(x,y) // CALL TO THE MYMAXIMUM
FUNCTION - printf("Maximum d",max) // PRINT THE OUTPUT
-
- int mymaximum( int a , int b) // DEFINE
SUB-FUNCTION - if(agtb) return a
- else return b