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

SYSC2006_L01_Imperative_programming_intro_C

sysc 2006 lecture 1

Uploaded by

diaansiddiky
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

SYSC2006_L01_Imperative_programming_intro_C

sysc 2006 lecture 1

Uploaded by

diaansiddiky
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

1

Copyright © 2024
Department of Systems and Computer Engineering, Carleton University

SYSC 2006: Foundations of


Imperative Programming
Lecture 1:
Imperative Programming Concepts
Introduction to C
2
Objectives

❑Define programming, program


❑Differentiate between the different programming paradigms
and name programming languages under those paradigms
❑Understand the fundamental concepts of imperative
programming languages
❑Become familiar with basic C syntax and variables
❑Write basic programs in C
3
What is Programming?
➢ Providing a set of instructions to instruct the computer to
perform tasks.
➢ The action or process of writing computer programs.
➢ Process of taking an algorithm and encoding it into a notation,
a programming language, so that it can be executed by a
computer.
➢ The art and science of translating a set of ideas into a
program.
4
What is a Program?

A list of instructions that a computer can follow.


5
Programming Paradigm
Approach to solve a problem using some programming
languages.

➢ Imperative Programming

➢ Declarative Programming
6
Declarative Programming Paradigm
Programmer declares the properties of the desired result, but
!
not how to compute them. What to do instead of how to do it.
s
o u rse
➢ Functional programming: based on mathematical function
(e.g., Scheme, Lisp)
S C c
SY
➢ Logic programming: uses logic circuits to control how facts
r
h e
and rules about the problems within the system are
t
O
represented or expressed (e.g., Prolog)
7
Imperative Programming Paradigm
Computation specified in terms of:
1. Program’s state: contents of its variables at any point in its execution
2. Statements that change the state.
Programmers instruct the computer on how to change its state.
Imperative Programming Language
➢ Has constructs that control the order in which operations are
performed
➢ Allows side effects: State can be read and modified at different points
in the program's execution
8
Imperative Programming Paradigm
➢ Object-oriented Programming: operations are grouped with the
state they operate on; i.e., objects encapsulate data (state) and
operations (behavior) 4 !
2 00
➢ Typically, an object's state is modified only by the code that is
C
part of the object
SYS
➢ Most (but not all) OO languages are imperative languages
➢ Typical Object Programming Languages: C++, Java, Python, …

0 6 !
S C 20
➢ Procedural Programming: statements are grouped into
procedures/subroutines/functions.
SY
9

Procedural Programming
10
Procedural Programming SYSC 2006!
➢ Statements are grouped into procedures/ subroutines/
functions
➢ Structured constructs (if, while, etc.) that control the
execution of blocks of code within procedures
➢ Typical Procedural Programming Languages: C, Fortran,
COBOL, Python, ...
NOTE: In the literature, "procedural programming" and "imperative
programming" are used synonymously.
Check the name
of the course!
11
Characteristics
➢ Program state
➢ Maintained using variables
➢ Changed using assignment statements
➢ By default, statements are executed sequentially from a
known starting point
➢ Loops: executes a sequence of statements multiple times
➢ Early exit from a loop body; e.g., break statement
➢ Skip execution of remainder of loop body, continue with
next iteration; e.g., continue statement
12
Characteristics
➢ Conditional branch
➢ executes a sequence of statements only if a specified
condition is true
➢ Unconditional branching
➢ jumps (infamous goto statement)
➢ subprograms/functions/procedures
➢ Other control-flow constructs - beyond the scope of this
course
➢ Exceptions, generators, coroutines, etc.
13
Why C for SYSC 2006?
ECOR1041/42 – Python

➢ Good introductory language to start programming

➢ Not good for teaching more advanced imperative


programming, such as memory management.
14
Why C for SYSC 2006?
SYSC 2006 – C Programming Language
➢ One of the oldest
➢ Created in the early 1970s by Dennis Ritchie while he was
working in Bell Laboratories
➢ General-purpose programming language
➢ Very helpful if you want to learn C++ or Java

Note: The course is not titled "Advanced C Programming“.


C is a tool to explore the fundamental concepts of imperative
programming.
15
Why C for SYSC 2006?
Advantages of C to teach imperative programming
➢ Small language (compared to Java or C++)
➢ Good for learning how to manage programs' memory correctly
➢ Programmers must write code that handles many low-level
issues: e.g., allocating, sharing, and deallocating blocks of
memory
➢ Many newer languages (e.g., Python, Java, Go): the
compiler or interpreter and run-time libraries take care of
many memory management issues "behind the scenes"
16
Why C for SYSC 2006?

➢ Imperative programming concepts apply to many other


modern programming languages

➢ After you understand the concepts, learning new


programming languages is usually straightforward
17
SYSC 2006 C Resources

C Tutor
Visualize C program execution, including memory management
URL: https://pythontutor.com/c.html
18

FROM PYTHON TO C
My first C Program
19
"Hello, world!" in Python
if __name__ == '__main__':
print("Hello, world!") Every C program
will have a main
Or, to be more “fair” to C: function that is
the starting point
def main():
print("Hello, world!")
of the program.

if __name__ == '__main__':
main()
20
"Hello, world!" in C Contains declarations that
must be included in order to
#include <stdio.h> call I/O functions (printf)
#include <stdlib.h>
Contains declarations of
int main(void){ EXIT_SUCCESS and
printf("Hello, world!\n"); EXIT_FAILURE macros
return EXIT_SUCCESS; (and much more)
}
Similar to Python function definition.
Mandatory return and parameter types.
Each C program has a main function.
Program starting point
21
"Hello, world!" in C
printf sends formatted
#include <stdio.h> output to the standard
#include <stdlib.h> output stream (often the
console)
int main(void){
printf("Hello, world!\n");
return EXIT_SUCCESS;
} instructions/statements
end with a semicolon (;)
The value returned by main is
the program's exit status
22
Practice Exercise:
Fahrenheit to Celsius
Print a table of Fahrenheit temperatures and their Celsius
equivalents
0 -17.8
20 -6.7 5 Min to solve it in
40 4.4 Python
... ...
280 137.8
300 148.9
23
Practice Exercise:
Python Solution
Fahrenheit to Celsius
def main():
# Set table’s lower & upper limits, and step size.
lower = 0
upper = 300
step = 20
fahr = lower
#Loop to calculate and print all values
while fahr <= upper:
celsius = 5 / 9 * (fahr – 32)
print(fahr, round(celsius, 1))
fahr = fahr + step
if __name__ == '__main__':
main()
24
Practice Exercise:
C Solution
Fahrenheit to Celsius
#include <stdio.h>
#include <stdlib.h>

int main(void){
//Declare the variables we need

int lower, upper, step;


float fahr, celsius;
25
Practice Exercise:
C Solution
Fahrenheit to Celsius
#include <stdio.h>
#include <stdlib.h>
int main(void){
int lower, upper, step;
float fahr, celsius;

/* Initialize the variables.


Set table’s lower & upper limits, and step size.
*/

lower = 0;
upper = 300;
step = 20;
26
Practice Exercise:
Fahrenheit to Celsius C Solution
#include <stdio.h>
#include <stdlib.h>
int main(void){
int lower, upper, step;
float fahr, celsius;
lower = 0;
upper = 300;
step = 20;

//Loop to calculate and print all values


fahr = lower;
while (fahr <= upper) {
celsius = 5.0 / 9.0 * (fahr – 32.0);
printf("%4.0f %6.1f\n", fahr, celsius);
fahr = fahr + step;
}
return EXIT_SUCCESS;
}
27
C Program summary
➢ #include directive causes the C compiler to read the
contents of the specified header file (by convention, filename
ends in .h)
➢ stdio.h: information about the standard I/O library; e.g.,
printf function
➢ stdlib.h: declarations of EXIT_SUCCESS and
EXIT_FAILURE macros (and much more)
➢ Statements are terminated by semicolon (;)
28
C Program summary
➢ C program consists of one or more functions
➢ Execution always begins in main
➢ Function header: int main(void)
➢ Program can also have arguments instead of void.
➢ Statements in the function body are enclosed in braces: {},
and indented
➢ Indentation is not required by the compiler.
➢ It is good coding practice and style (and worth marks in
this course ☺)!
29
C Program summary
➢ Comments in C
➢ Lines enclosed by /* and */ are comments that can span
multiple lines
➢ // starts a single-line comment
➢ Variables must be declared (names and types) before being
used in expressions
30
C Program summary
➢ Assignment statement
➢ Expression on the right-hand side of = is evaluated and
stored in the variable named on the left-hand side
➢ Conversion from int to float is done automatically. Some
conversions are automatically done; others need to be
specified.
➢ Be careful with conversions: Why is Celsius
➢ celsius = 5 / 9 * (fahr – 32); always zero?
31
No need to memorize all
C Program summary specifications.
Program outputs: printf Use the help of the IDE.
printf("%4.0f %6.1f\n", fahr, celsius);
➢ printf's first argument is a format string that specifies how the
values of the subsequent arguments are output
➢ %4.0f - floating-point value (fahr) is output in a field at
least 4 characters wide, with no decimal point and no
fractional digits
➢ %6.1f - floating point value (celsius) is output in a field
at least 6 characters wide, with 1 digit after the decimal
point
32
Recap of Learning Outcomes

❑Define programming, program


❑Differentiate between the different programming paradigms
and name programming languages under those paradigms
❑Understand the fundamental concepts of imperative
programming languages
❑Become familiar with basic C syntax and variables
❑Write basic programs in C
33
Copyright © 2024
Department of Systems and Computer Engineering, Carleton University

SYSC 2006: Foundations of


Imperative Programming
Lecture 1:
Imperative Programming Concepts
Introduction to C

You might also like