Computer Programming For Engineers: 1. Introduction To C Programming
Computer Programming For Engineers: 1. Introduction To C Programming
1. INTRODUCTION TO C PROGRAMMING
Student Learning Outcomes
Upon completion of this lesson, students will be able to:
2
Topics
1. Computer Programming Language
2. History of C
3. Problem solution and software development
4. Algorithm
5. Variables and assignment statements
6. Statements and expression
3
Topics
1. Computer Programming Language
2. History of C
3. Problem solution and software development
4. Algorithm
5. Variables and assignment statements
6. Statements and expression
4
1. Computer Programming Language
Three (3) types of programming languages
2. Assembly language
• Using meaningful symbols to represent machine
code.
• Usually defined by microprocessor’s manufacturer.
• Example: add hl,de
3. High-level languages
6
Topics
1. Computer Programming Language
2. History of C
3. Problem solution and software development
4. Algorithm
5. Variables and assignment statements
6. Statements and expression
7
2. History of C
C
Invented by Ritchie based on B, a simplified version of BCPL.
Used to develop Unix operating system and Unix commands
Most system software such as OS are written in C or C++
Replacement for assembly language for hardware interface.
By late 1970's C had evolved to “K & R C“
C Standards
1st C standard created in 1989 by ANSI, ratified by ISO in 1990.
It is called C89. Some call it C90.
2nd C standard was ratified in 1999, called C99.
Numerical extensions such as complex numbers, variable length
arrays, and IEEE floating-point arithmetic are major enhancement in
C99. C99 will be pointed out whenever features in C99 only are
presented.
8
Topics
1. Computer Programming Language
2. History of C
3. Problem solution and software development
4. Algorithm
5. Variables and assignment statements
6. Statements and expression
9
3. Problem solution and software
development
Problem solution (problem solving)
Problem-solving consists of using generic or ad hoc
methods, in an orderly manner, for finding solutions to any
given problems.
Software development
A process in the development of software product
10
3. Problem solution and software
development
Software development – 3 phases
i. Development and design
• Analyze the problem
Input, output, formula
• Develop solution (Algorithm)
Pseudocode, flowchart
• Code the solution (coding)
Syntax error, logic error, run-time error
• Test and correct the program
ii. Documentation
iii. Maintenance
11
3. Problem solution and software
development
Programming process
12
3. Problem solution and software
development
Programming shortcut?
13
3. Problem solution and software
development
Example:
Create a program to calculate and print out the total
numbers of two integers.
Solution:
Analyze the problem
Input: two integer (num1 and num2)
Output: total of two integers (total)
Formula: total = num1 + num2
14
Topics
1. Computer Programming Language
2. History of C
3. Problem solution and software development
4. Algorithm
5. Variables and assignment statements
6. Statements and expression
15
4. Algorithm
Algorithms as path through “Problem spaces”
19
4. Algorithm
4.2 Flowchart
20
Topics
1. Computer Programming Language
2. History of C
3. Problem solution and software development
4. Algorithm
5. Variables and assignment statements
6. Statements and expression
21
5. Variables and assignment statements
5.1 Operators
Operator - A symbol that represents a specific action
Assignment operator: ‘=‘
Arithmetic operators:
i. + Addition
ii. - Subtraction
iii. * Multiplication
iv. / Division
v. % Modulus
Relational operators:
i. < Less than comparison
ii. <= Less or equal comparison
iii. == Equal comparison
iv. > Greater than comparison
v. >= Greater or equal comparison
vi. != Not equal comparison
22
5. Variables and assignment statements
5.1 Operators
Logical operators:
i. ! Logical negation
ii. && Logical AND
iii. || Logical OR
23
5. Variables and assignment statements
5.1 Operators
Operations Associativity
::
() [] left to right
Precedence and Function_name() right to left
24
5. Variables and assignment statements
5.2 variables
A variable is a location in memory that can be referred
to by an identifier and in which a data value that can be
changed is stored
25
5. Variables and assignment statements
5.2 variables
• Variable names in C
May only consist of letters, digits, and underscores
May be as long as you like, but only the first 31
characters are significant
May not begin with a number
Case sensitive: A ≠ a
May not be a C reserved word (keyword)
26
5. Variables and assignment statements
5.2 variables
• Reserved words in c
auto break int long
case char register return
const continue short signed
default do sizeof static
double else struct switch
enum extern typedef union
float for unsigned void
goto if volatile while
27
5. Variables and assignment statements
5.2 variables
Naming convention
• C programmers generally agree on the following
conventions for naming variables.
Begin variable names with lowercase letters
Use meaningful identifiers
Separate “words” within identifiers with underscores or mixed
upper and lower case.
Examples: surfaceArea surface_Area surface_area
Be consistent!
28
Topics
1. Computer Programming Language
2. History of C
3. Problem solution and software development
4. Algorithm
5. Variables and assignment statements
6. Statements and expression
29
6. Statements and expression
Statements
A statement is a block of code that does something.
Example:
o An assignment statement assigns a value to a variable.
o A for statement performs a loop
30
6. Statements and expression
Expression
Any legal combination of symbols that represents a value.
Example:
o X > 10
o Y+5
31
6. Statements and expression
Statement vs Expression
Every C program comprises of Statements.
Program executes statement by statement.
Each statement generally, comprises of some expression.
32
33