Computer Programming I
C Programming
Lecture 1 – Introduction
2022 Spring, CSE2003/AIE2050
Sogang University
CSE2003/AIE2050 S'23 1
Team
• Instructor
• Prof. Joo Ho Lee (AS818, jhleecs@sogang.ac.kr)
• Teaching assistants (Class I)
• Lee, Soo-hyun (이수현), khtt1222@gmail.com
• Woo, Sun-gwon (우성원), scottwoo1122@gmail.com
• Yeen, Hueui-yeen (연희연), yeen214@gmail.com
CSE2003/AIE2050 S'23 2
Team
• Instructor
• Prof. Joo Ho Lee (AS818, jhleecs@sogang.ac.kr)
• Teaching assistants (Class II)
• Yoon, Jun (윤준영), jjun9288@gmail.com
• Her, Seunghoi (허승회), seunghoi0722@gmail.com
• Lee, Do-hun (이도훈), dlehgns1998@gmail.com
CSE2003/AIE2050 S'23 3
Organization
• 3 Credits
• Lectures held in Dasan Hall
• Class I: D105, Tuesday and Thursday 13:30-14:45
• Class II: D105, Tuesday and Thursday 15:00-16:15
• Exam
• Date and time will be announced on the cyber campus.
• Relevant to programming assignments and pen & paper assignments.
CSE2003/AIE2050 S'23 4
Exercise & Programming Assignments
• We offer MANY programming assignments during semester
• A programming assignment every week
• 10~12 programming assignments + some pen & paper assignments
• Handed out via Cyber Campus.
• Assignments are not graded, but we will give support and solutions.
• They are highly relevant to the exam.
CSE2003/AIE2050 S'23 5
Grading
• Exam (95 %)
• Midterm & final exams (45% and 50%, individually)
• Homework (0%)
• Programming and pen & paper assignments
• Relevant to exams
• Attendance (5%)
• Your attitude and attendance.
CSE2003/AIE2050 S'23 6
Cheating
• Penalty for cheating:
• Removal form course with failing grade (no exceptions)
• Permanent mark on your record
• Your instructors’ personal contempt
• Exam modality
• Offline, the place would be announced one week before.
CSE2003/AIE2050 S'23 7
References
• Essential C, Stanford C Materials
http://cslibrary.stanford.edu/101/EssentialC.pdf
• GoalKicker’s free C Porgramming Notes
https://goalkicker.com/CBook/
• (Korean) 열혈프로그래밍 C, 윤성우 저, Orange Media
• Don’t need to buy it unless you need
CSE2003/AIE2050 S'23 8
Definition of Word “Computer” before 1900
• Timelines
• Fibonacci (1200), Descartes (1620), Fermat (1630), Pascal (1650), Newton (1680),
Leibniz (1680), Euler (1750), Industrial Revolution (1760~1820), Gauss (1800),
Germain (1810), Galoise (1830)
• Computer (JOB): A person who completes mathematical calculations
• sitting, hour after hour, performing calculations by hand and recording them
in books: very painful job
• Significant role for navigation, science, engineering, and mathematics
• Slow and inaccurate due to human error
CSE2003/AIE2050 S'23 9
Pascaline: Pascal Calculator (1642)
• Mechanically operated
• One arithmetic operator can be computed
Pascal Calculator (1642) CSE2003/AIE2050 S'23 Leibniz Calculator (1670) 10
Difference Engine - Automating Computations (1822)
• Babbage wanted to build an
automated machine that replace
the “mental labor” which is math
calculation.
• Invented the “difference engine”
that calculates polynomial
Difference engine
functions mechanically in 1822.
CSE2003/AIE2050 S'23 11
Jacquard Machine (1804)
• A weaving machine that automates the patterning
• A programmable device
A series of punched card represents the Jacquard machine
pattern CSE2003/AIE2050 S'23 12
Babbage - Analytic Machine (1833)
• Propose the basic structure of computers (memory and CPU)
• CPU: perform a given instruction like variable load, store and arithmetic
operation.
• Memory: A space to store the result of the CPU
• Inspired by Jacquard machine
Computer
Programming code in 1833
const. 1 variable indices Instruction 1 load variables
const. 2 variable indices Instruction 2 Input
const. 3 variable indices Instruction 3
CPU Memory
const. 4 variable indices Instruction 4
. . . store results
. . .
Abstract CSE2003/AIE2050
overview of “Analytic
S'23
machine” 13
How computer works?
Result Buffer
Program
load 3 to the result buffer
Counter
store the result buffer to variable 0
CPU
load 4 to the result buffer
store the result buffer to variable 1
slot1 slot2
load variable 0 to CPU slot 0
load variable 1 to CPU slot 1
addition
Memory
store the result buffer to variable 0
Variable 0
instruction, variable, constant
Variable 1
Instruction series (program)
CSE2003/AIE2050 S'23 14
Programming
• Composing a series of instructions executable by a programmable
machine in order to perform a desirable computation task
• Programming language
• A notation about data and computation instruction steps
• A representation of algorithm, a computational method to solve the problem
CSE2003/AIE2050 S'23 15
C Programming Language
• High-level but low-level programming language
• Relatively lower than Python programming language
• Lower programming languages,
• Less interpretation needed
Human-intuitive
• More required details of computers
• Faster code (usually)
Machine-executable
CSE2003/AIE2050 S'23 16
Computer & Program
• Making a program → Giving sequential commands to a computer
• Computer will perform it line-by-line
Computer
Program
You
CSE2003/AIE2050 S'23 17
https://www.restaurantowner.com/public/Chef-Training-Managing-the-Recipes-and-the-Yield.cfm
Program Codes
• A sequence of computer instructions (Like a recipe)
• Specify how to perform a computation
• Computers run instructions in the program sequentially
• Computing tasks
• Mathematical: solving a systems of equations, finding the roots of a polynomial
• Symbolic: searching and replacing text in a document, and etc.
• Others.
CSE2003/AIE2050 S'23 18
Executable Program
• Computers cannot understand high-level programming language
• Compilation: program codes → executable program
• Translate high-level language (string) into machine language (binaries)
• Computer now can execute the compiled program.
• Of course, the workflow shouldn’t change during compilation!
CSE2003/AIE2050 S'23 19
Program “Hello World”
#include < stdio.h >
int main(void) {
printf("Hello World\n");
puts("Hello World\n");
return 0;
CSE2003/AIE2050 S'23 20
/* Begin with comments about file contents */
Insert #include statements and preprocessor definitions
Function prototypes and variable declarations
Define main() function {
Function body
}
Define other function {
Function body
}
Basic Structure of .c file
...
CSE2003/AIE2050 S'23 21
Comments
• Comments: /* multiple-line comment */, // One-line comment
• Can span multiple lines
/* first line
second line
third line */
• Ignored by compiler
• Use comments for additional information
/* hello.c – first program
created by your_name, 2023/02/05 */
CSE2003/AIE2050 S'23 22
The #include Macro
• Header files: constants, functions, other declarations
• #include <stdio.h> - read the contents of the header file
stdio.h
• stdio.h: standard I/O functions for console, files
#include <stdio.h> /* basic I/O facilities */
• printf, puts, scanf and other I/O functions available
CSE2003/AIE2050 S'23 23
Program 2: Add Two Numbers
#include <stdio.h>
int add(int, int);
int main() {
int a, b, sum;
a = 10;
b = 5;
sum = add(a, b);
printf("%d+%d = %d\n", a, b, sum);
return 0;
}
int add(int a, int b) {
return a + b;
}
CSE2003/AIE2050 S'23 24
Variables in Program?
• Very different from variables in mathematics
• Variable in math: some value that satisfy conditions
• y=2, x = 2x-y-1, so x is 1.5
• Variable in program: Assignable. A small object that has a piece of
memory space to store value.
• x=0; y=2; x = 2x-y-1; /* x is -3 after executing commands */
• Operator = is assignment operator, compute the result of the right hand and assign it
to the variable on the left hand.
CSE2003/AIE2050 S'23 25
Declaring Variables
• Must declare variables before use
• Variable declaration:
int n;
float phi;
• int – integer data type
• float – floating-point data type
• Other types (later)
CSE2003/AIE2050 S'23 26
Initializing Variables
• Uninitialized, variable assumes a default value
• Variables initialized via assignment operator:
n=3
• Can initialized at declaration:
float phi = 1.6180339887;
• Can declare/initialize multiple variables at once:
int a,b,c = 0, d = 4;
CSE2003/AIE2050 S'23 27
Arithmetic Expressions
• Suppose a and b are variables
• a+b,a-b,a*b, a/b, a%b: binary arithmetic
• A simple statement:
b=a+5*a/(b-2);
• Numeric literals like 2 or 5 valid in expression
• Semicolon ends statement
• x += y is equal to x = x + (y) available for -, *, /, %
CSE2003/AIE2050 S'23 28
Order of Operations:
Precedence Operator Evaluation direction
High +, - (sign) Right-to-left
*, /, % Left-to-right
+, - Left-to-right
Low =, +=, -=, *=, /=, %= Right-to-left
• Use parentheses to change order of evaluation
A+B*-C → A+(B*(-C))
(A+B)*-C → (A+B)*(-C)
CSE2003/AIE2050 S'23 29
Assume x=3.0 and y=4.0. Evaluate the statement
float z = x+4*x/(y-2);
1. Evaluate expression in parentheses
float z = x+4*x/(y-2); → float z = x+4*x/2.0;
2. Evaluate multiplies and divides, from left-to-right
float z = x+4*x/2.0; → float z = x+12.0/2.0; → float z = x+6.0;
3. Evaluate addition
float z = x+6.0; → float z = 9.0;
4. Perform initialization with assignment
z=9.0
How do I insert parentheses to get z = 7.5?
float z = (x+4*x)/(y-2);
Order of Operations
CSE2003/AIE2050 S'23 30
Function Prototypes
• Functions must be declared before use
• Declaration called function prototype
• Function prototypes:
int isEven(int); or int isEven(int n);
• Prototypes for many common functions in header files for C Standard
Library
CSE2003/AIE2050 S'23 31
Function Prototypes
Return_type function_name (arg1, arg2 …);
• Arguments: input of function, local variables
• Return value: single value returned to caller when function exits
• void – signifies no return value/arguments
int rand(void);
CSE2003/AIE2050 S'23 32
The main() Function
• main(): entry point for C program
• Simplest version: no inputs, outputs 0 when successful, and nonzero
to signal some error
int main(void);
• Two-argument form of main(): access command-line arguments
int main(int argc, char **argv);
• More on the char **argv notation later this week…
CSE2003/AIE2050 S'23 33
Our main() Function
/* The main() function */
int main(void) /* entry point */
{
/* write message to console */
puts(“hello Worlds”);
return 0; /* exit ( 0 => success) */
}
• puts(): output text to console window (standard-out, stdout) and end the line
• String literal: written surrounded by double quotes
• return 0;
exits the function, returning value 0 to caller
CSE2003/AIE2050 S'23 34
Function Definitions
Function declaration
{
declare variables;
Program statements;
}
• Must match prototype (if there is one)
• Variable names don’t have to match
• No semicolon at end
• Curly braces define a block – region of code
• Variables declared in a block exist only in the block
• Variable declarations before any other statements
CSE2003/AIE2050 S'23 35
Console I/O
• Stdout, stdin: console output and input stream
(ex. screen input and output in command prompt)
• puts(string): print string to stdout
• putchar(char): print character to stdout
• char = getchar(): return character from stdin
• string = gets(string): read line from stdin into string
• Many others – later this week
CSE2003/AIE2050 S'23 36
Preprocessor Macros
• Preprocessor macros begin with # character
#include <stdio.h>
• #define msg “Hello World”
Defines msg as “Hello World” throughout source file
• Many constants specified this way
CSE2003/AIE2050 S'23 37
Reference
• https://en.wikipedia.org/wiki/Pascal%27s_calculator
• https://en.wikipedia.org/wiki/Stepped_reckoner
• https://en.wikipedia.org/wiki/Analytical_Engine
• https://en.wikipedia.org/wiki/Difference_engine
• https://www.youtube.com/watch?v=K6NgMNvK52A
CSE2003/AIE2050 S'23 39