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

02 Computer and C Programming Language

The document outlines a course titled 'Programming for Engineers' focusing on the C programming language, taught by Dr.-Ing. Nguyen Van Binh. It covers essential topics such as computer organization, programming languages, and the C language's history, along with practical programming skills and software development methods. The course emphasizes hands-on learning and provides a list of recommended resources for further study.

Uploaded by

ngocthanh2821
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

02 Computer and C Programming Language

The document outlines a course titled 'Programming for Engineers' focusing on the C programming language, taught by Dr.-Ing. Nguyen Van Binh. It covers essential topics such as computer organization, programming languages, and the C language's history, along with practical programming skills and software development methods. The course emphasizes hands-on learning and provides a list of recommended resources for further study.

Uploaded by

ngocthanh2821
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Course: Programming for Engineers (C language)

Dr.-Ing. Nguyen Van Binh


Email: [email protected]
HCMC, Feb. 2025
Content

1. Introduction to Computer and C language


2. Making Decisions
3. Looping
4. Functions
5. Arrays
6. Pointers
7. Structures
8. Characters and Strings
9. File Processing
10.Dynamic Memory Allocation
4 February 2025 Programming for Engineers (C language) NV Binh 2
Programming for Engineers (C language)

Documents & References


 “C - How to Program”, Harvey M. Deitel, Paul
J. Deitel, Prentice Hall, 2007.
 “Programming in C”, 3rd edition, S. G.
Kochan, Sams Publishing, 2005.
 “Programming In C: A Practical Approach”,
2nd edition, Ajay Mittal, Pearson, 2008.
 “An Introductory to Programming with C”,
2nd edition, Diane Zak, Thomson Publishing,
2001.
 “Data Structures and Algorithms”, Asian
edition, Alfred V. Aho, John E. Hopcroft,
Jeffrey D. Ullman.
▪ https://blackboard.hcmiu.edu.vn/
4 February 2025 Programming for Engineers (C language) NV Binh 3
Programming for Engineers (C language)

 Roles of the course


 Programming is the core of Electrical/Computer Science Engineering.

 This course is considered as the kick-off course in Programming and


Computer Science using “enjoyable” C (procedure-oriented) programming
language.

 Aims of the course


 This course aims to teach students skills that will be relevant many years in
the future.

4 February 2025 Programming for Engineers (C language) NV Binh 4


Programming for Engineers (C language)

Software

 Windows: DevC++, Microsoft Visual Studio Code

 MacOS: Xcode, ...

 UNIX/Linux: NetBeans, CodeLite, Atom Code Editor...

 Eclipse is available for 3 types of OS

4 February 2025 Programming for Engineers (C language) NV Binh 5


Programming for Engineers (C language)

Some Tips
➢ Learn by doing. Always play with the code while learning
➢ Seek out more online resources. There’s a wealth of content
➢ Don’t just read the sample code. Tinker with it!
➢ Take breaks when debugging

• “The most important thing in computer programming is SYNTAX.”


• “Programming is NOT a spectator sport. To become good you
must practice - practice - practice.”

4 February 2025 Programming for Engineers (C language) NV Binh 6


1 Introduction to Computer and C language
Lecture Content
➢ Introduction to Computers
▪ A Few Uses of Computers
▪ Computer Organization
▪ Data Hierarchy
▪ Programming Languages
▪ Operating Systems
➢ Introduction to C Programming Language
▪ Simple C Program
▪ Memory Concepts
➢ Algorithms
▪ Pseudo-code
▪ Flowchart
4 February 2025 Programming for Engineers (C language) NV Binh 7
1 Introduction to Computer and C language
A Few Uses of Computers

• Office
• Email, Instant messaging, Video chat
• Electronic health records
• Medical imaging
• Cloud computing
• Robots
• Internet TV
• Game programming

4 February 2025 Programming for Engineers (C language) NV Binh 8


1 Introduction to Computer and C language
Computer Organization

• Input Unit

• Output Unit

• Memory Unit

• Arithmetic and Logic Unit (ALU)

• Central Processing Unit (CPU)

• Secondary Storage Unit

4 February 2025 Programming for Engineers (C language) NV Binh 9


1 Introduction to Computer and C language
Data Hierarchy

• Database

• Files

• Records

• Fields

• Characters

• Bit
4 February 2025 Programming for Engineers (C language) NV Binh 10
1 Introduction to Computer and C language
Programming Languages
Various programming
languages

➢ Some understandable directly by computers


➢ Others require “translation” steps
4 February 2025 Programming for Engineers (C language) NV Binh 11
1 Introduction to Computer and C language
Programming Languages
Machine language
• Natural language of a particular computer
• Consists of strings of numbers (1s, 0s)
• Instruct computer to perform elementary operations one at
a time
• Machine dependant

4 February 2025 Programming for Engineers (C language) NV Binh 12


1 Introduction to Computer and C language
Programming Languages
Assembly Language
• English like abbreviations
• Translators programs called “Assemblers” to convert assembly
language programs to machine language
• E.g. add overtime to base pay and store result in gross pay

LOAD BASEPAY
ADD OVERPAY
STORE GROSSPAY

4 February 2025 Programming for Engineers (C language) NV Binh 13


1 Introduction to Computer and C language
Programming Languages High-level languages
• To speed up programming even further
• Single statements for accomplishing substantial tasks
• Translator programs called “Compilers” to convert
high-level programs into machine language
• E.g. add overtime to base pay and store result in gross
pay
grossPay = basePay + overtimePay

4 February 2025 Programming for Engineers (C language) NV Binh 14


1 Introduction to Computer and C language
History of C

• In 1972, Dennis Ritchie at Bell Labs (Canada) writes C


and in 1978 the publication of The C Programming
Language by Kernighan & Ritchie caused a revolution in
the computing world.

• In 1983, the American National Standards Institute


(ANSI) established a committee to provide a modern,
comprehensive definition of C. The resulting definition,
the ANSI standard, or “ANSI C”, was completed late 1988.

4 February 2025 Programming for Engineers (C language) NV Binh 15


1 Introduction to Computer and C language
Why use C?
➢ Mainly because it produces code that runs nearly as fast as code
written in assembly language.
➢ Stability and small size code
➢ Some examples of the use of C might be:
▪ Operating Systems (Windows, Mac OS, UNIX)
▪ Language Compilers / Assemblers
▪ Embedded Systems
▪ Text Editors / Language Interpreters
▪ Real-Time Systems
▪ Network Drivers
▪ Modern Programs
▪ Databases
▪ Communication Systems
4 February 2025 Programming for Engineers (C language) NV Binh 16
1 Introduction to Computer and C language
C Standard Library
• Two parts to learning the “C” world
– Learn C itself

– Take advantage of rich collection of existing functions


called C Standard Library

• Avoid reinventing the wheel

4 February 2025 Programming for Engineers (C language) NV Binh 17


1 Introduction to Computer and C language
Software Development Method
 Requirement Specification
– Problem Definition
 Analysis
– Refine, Generalize, Decompose the problem definition
 Design
– Develop Algorithm
 Implementation
– Write Code
 Verification and Testing
– Test and Debug the code
4 February 2025 Programming for Engineers (C language) NV Binh 18
1 Introduction to Computer and C language
Basics of C Environment

• C systems consist of 3 parts


– Environment
– Language
– C Standard Library

• Development environment has 6


phases
– Edit
– Pre-processor
– Compile
– Link
– Load
– Execute

4 February 2025 Programming for Engineers (C language) NV Binh 19


1 Introduction to Computer and C language
Basics of C Environment
Program edited
Phase 1 Editor Disk in Editor and
stored on disk

Preprocessor
Phase 2Preprocessor Disk program processes
the code

Creates
object code
Phase 3 Compiler Disk and stores on
disk

Links object code


Phase 4 Linker Disk with libraries and
stores on disk
4 February 2025 Programming for Engineers (C language) NV Binh 20
1 Introduction to Computer and C language
Basics of C Environment
Primary memory

Puts program in
Phase 5 Loader memory

Primary memory
Takes each
instruction
Phase 6 CPU and executes it
storing
new data values

4 February 2025 Programming for Engineers (C language) NV Binh 21


1 Introduction to Computer and C language
Simple C Program

1 /* A first C Program*/

2 #include <stdio.h>

3 int main()

4 {

5 printf(“Hello World \n”);

6 return 0;

7 }

4 February 2025 Programming for Engineers (C language) NV Binh 22


1 Introduction to Computer and C language
Simple C Program

 Line 1: /* The first C Program*/


▪ In the C Programming Language, you can place comments in your
source code that are not executed as part of the program.
▪ Comments provide clarity to the C source code allowing others to
better understand what the code was intended to accomplish and
greatly helping in debugging the code.
▪ Comments are especially important in large projects containing
hundreds or thousands of lines of source code or in projects in
which many contributors are working on the source code.
▪ You can also write comment as // The first C Program

4 February 2025 Programming for Engineers (C language) NV Binh 23


1 Introduction to Computer and C language
Simple C Program

 Line 2: #include <stdio.h>

▪ As part of compilation, the C compiler runs a program called


the C preprocessor. The preprocessor is able to add and
remove code from your source file.
▪ In this case, the directive #include tells the preprocessor to
include code from the file stdio.h
▪ This file contains declarations for functions that the program
needs to use. A declaration for the printf function is in this
file.

4 February 2025 Programming for Engineers (C language) NV Binh 24


1 Introduction to Computer and C language
Simple C Program

 Line 3: void main()


▪ This statement declares the main function.
▪ A C program can contain many functions but must always
have one main function.
▪ A function is a self-contained module of code that can
accomplish some task. Functions are examined later.
▪ The “void” specifies the return type of main. In this case,
nothing is returned to the operating system.
▪ You can also define main function as void main(void) or int
main() or int main(void). The last two cases must come with
a return 0; at the end.
4 February 2025 Programming for Engineers (C language) NV Binh 25
1 Introduction to Computer and C language
Simple C Program

 Line 4: {
▪ This opening bracket denotes the start of the program.

4 February 2025 Programming for Engineers (C language) NV Binh 26


1 Introduction to Computer and C language
Simple C Program

 Line 5: printf("Hello World\n");


▪ printf is a function from a standard C library that is used to
print strings to the standard output, normally your screen.
▪ The compiler links code from these standard libraries to the
code you have written to produce the final executable.
▪ The “\n” is a special format modifier that tells the printf to
put a line feed at the end of the line.
▪ If there were another printf in this program, its string would
print on the next line.

4 February 2025 Programming for Engineers (C language) NV Binh 27


1 Introduction to Computer and C language
Simple C Program

 Line 6: }
▪ This closing bracket denotes the end of the program.

4 February 2025 Programming for Engineers (C language) NV Binh 28


1 Introduction to Computer and C language
Escape Sequence

• \n new line
• \t horizontal tab (⋲ 6 spaces)
• \r carriage return
• \a alert
• \\ backslash
• \” double quote

4 February 2025 Programming for Engineers (C language) NV Binh 29


1 Introduction to Computer and C language
Memory concepts

➢ Every variable has a name, type and value

➢ Variable names correspond to locations in computer


memory

➢ New value overwrites the previous value –


“Destructive read-in”

➢ Value reading called “Non-destructive read-out”

4 February 2025 Programming for Engineers (C language) NV Binh 30


1 Introduction to Computer and C language
Arithmetic in C

C operation Algebraic C
Addition(+) f+7 f+7
Subtraction (-) p-c p-c
Multiplication (*) bm b*m
Division (/) x/y x/y
Modulus (%) r mod s r%s

4 February 2025 Programming for Engineers (C language) NV Binh 31


1 Introduction to Computer and C language
Precedence Order

➢ Highest to lowest

• ()

• *, /, %

• +, -

•=
4 February 2025 Programming for Engineers (C language) NV Binh 32
1 Introduction to Computer and C language
Precedence Order
Algebra:
z = pr%q+w/x-y

C:
z = p * r % q + w / x – y;

Precedence: 6 1 2 4 3 5

4 February 2025 Programming for Engineers (C language) NV Binh 33


1 Introduction to Computer and C language
Precedence Order
Algebra:
a(b+c)+ c(d+e)

C:
a * ( b + c ) + c * ( d + e );

Precedence: 3 1 5 4 2

4 February 2025 Programming for Engineers (C language) NV Binh 34


1 Introduction to Computer and C language
Decision Making

➢ Equality operators
• == equality
• != difference

➢ Relational operators
•< less than
•> greater than
• <= less than or equal to
• >= greater than or equal to

4 February 2025 Programming for Engineers (C language) NV Binh 35


1 Introduction to Computer and C language
Decision Making

• Checking falsity or truth of a statement

• Equality operators have lower precedence than


relational operators

• Relational operators have same precedence

• Both associate from left to right

4 February 2025 Programming for Engineers (C language) NV Binh 36


1 Introduction to Computer and C language
Assignment Operators
• = assign C = A + B will assign the value of A + B to C
• += assign with add C += A is equivalent to C = C + A
• -= assign with subtract C -= A is equivalent to C = C - A
• *= assign with multiply C *= A is equivalent to C = C * A
• /= assign with divide C /= A is equivalent to C = C / A
• %= assign with remainder C %= A is equivalent to C = C % A
• >>= assign with right-shift C >>= 2 is same as C = C >> 2
• <<= assign with left-shift C <<= 2 is same as C = C << 2
• &= assign with bitwise AND C &= 2 is same as C = C & 2
• ^= assign with bitwise XOR C ^= 2 is same as C = C ^ 2
• |= assign with bitwise OR C |= 2 is same as C = C | 2
4 February 2025 Programming for Engineers (C language) NV Binh 37
1 Introduction to Computer and C language
Increment/Decrement Operators
• ++ ++i pre-increment
▪ i = 1;
▪ j = ++i;
▪ (i is 2, j is 2)

• ++ i++ post-increment
▪ i = 1;
▪ j = i++;
▪ (i is 2, j is 1)

• -- --i pre-decrement
• -- i-- post-decrement
4 February 2025 Programming for Engineers (C language) NV Binh 38
1 Introduction to Computer and C language
Example

4 February 2025 Programming for Engineers (C language) NV Binh 39


1 Introduction to Computer and C language
Example

4 February 2025 Programming for Engineers (C language) NV Binh 40


1 Introduction to Computer and C language
Example
#include <stdio.h>
main()
{
int a = 21;
int c ;
c = a;
printf("Line 1 - = Operator Example, Value of c = %d\n", c );
c += a;
printf("Line 2 - += Operator Example, Value of c = %d\n", c );
c -= a;
printf("Line 3 - -= Operator Example, Value of c = %d\n", c );
c *= a;
printf("Line 4 - *= Operator Example, Value of c = %d\n", c );
c /= a;
printf("Line 5 - /= Operator Example, Value of c = %d\n", c );
c = 200;

4 February 2025 Programming for Engineers (C language) NV Binh 41


1 Introduction to Computer and C language
Example

c %= a;
printf("Line 6 - %= Operator Example, Value of c = %d\n", c );
c <<= 2;
printf("Line 7 - <<= Operator Example, Value of c = %d\n", c );
c >>= 2;
printf("Line 8 - >>= Operator Example, Value of c = %d\n", c );
c &= 2;
printf("Line 9 - &= Operator Example, Value of c = %d\n", c );
c ^= 2;
printf("Line 10 - ^= Operator Example, Value of c = %d\n", c );
c |= 2;
printf("Line 11 - |= Operator Example, Value of c = %d\n", c );
}

4 February 2025 Programming for Engineers (C language) NV Binh 42

You might also like