Introduction to C programming pt 1 Lecture 1
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
Computing for Engineers
Lecture 1
Introduction to C programming pt 1
1. Course Overview
1.1. Objectives
On successful completion of this module, the student will be able to:
Understand the concepts of computer programming languages
The benefits of using the C programming language for engineering applications
Design and test computer programs using the main techniques of the ANSI C programming language
An introduction to more advanced C programming techniques
1.2. Recommended books
Note: It is not necessary to buy any books for this module. However, for background reading, you may wish to refer to “Davies”. There are copies available for loan in the Main Library. Note that this book is not in print. Do not pay the high prices sometimes seen on Amazon and other web sites.
“Kernighan and Ritchie” is a reference book which you should refer to if you need more information about the ANSI C standard.
Book title/comment |
Author |
Publisher |
Beginning Programming with C for Dummies |
Dan Gookin / Sandra Geisler |
Free book which can be viewed via ResourceLists |
The Indispensable Guide to C with Engineering Applications |
Paul Davies |
Addison-Wesley ISBN 0-201-62438-9 |
The C Programming Language |
Brian Kernigan and Dennis Ritchie |
Prentice Hall |
1.3. Lecture schedule
10 hours lectures. A summary of the lecture content is as follows. Lecture 1 – Introduction to C programming I
Importance and applications of the C programming language
Other common programming languages
Output
Variables
Comments
Data types
Assignment and initialisation
Lecture 2 – Introduction to C programming II
Operators
Input
Preprocessor
Header files
Program style and readability
Libraries
Program compilation and linking
Lecture 3. Loops
Iteration – for loops
The while loop
The do … while loop
Comparison of for, while and do-while loops
Lecture 4. Conditions
Condition statements – the if and ifelse statements
The switch statement
Logical and relational operators
Lecture 5 – Arrays
Introduction to arrays
Declaring arrays
Initializing arrays
Multi-dimensional arrays
Character arrays – strings
Lecture 6 – Functions
Lecture 7 – Files
Lecture 8 – Hardware and embedded systems
Lecture 9 – Pointers and structures
Lecture 10 – Revision
Contacting the lecturer. Dr Pammu writes: Emailing [email protected]> is the preferred method. I will aim to respond to all emails on the same working day or by the following working day. I can also be contacted by phone on 0121-414-4312. I can only read and send emails during University working hours.
You are welcome to visit my office (Gisbert Kapp building). However please note my office is within an ITsecurity zone and it will not always be possible to meet with you immediately.
For longer queries or for additional one-to-one support: please email me and I will try and arrange an appointment for within 2-3 working days.
2. Introduction to Programming Languages
2.1. Early developments
The earliest computers were programmed by entering machine code. Machine code consists of numbers which are instructions and data which directly instruct processors on what to do.
Early computers were programmed using switches and by connecting wires. Later improvements included paper tape and punched card. However, whatever the method of entering data, machine code is an extremely slow, complex and error prone method of programming computers.
In the 1960s, this led to the development of high-level languages. These allowed programmers to write programs which more closely resembled the English language and mathematical algorithms. Early high-level languages included Basic, Algol and Cobol.
In 1972, the development of modern computer systems and networking software required new programming techniques. In particular, the new Unix operating system could not be developed using the existing high-level languages, and were far too complex to develop using machine code or assembler.
This led Dennis Ritchie and Brian Kernighan to develop the C programming language, which would enable them to develop the Unix operating system and modern computer applications.
Despite the fact C was introduced over 40 years ago, and that there have been numerous programming languages developed since then, C remains the single most commonly deployed language in Engineering applications.
We will now look at why this is the case.
2.2. ANSI C – the most important language for Engineers?
As of 2019, C is the most widely deployed programming language in Engineering applications. Here are the the most common reasons why:
C compilers exist for almost every single microprocessor and operating system ever invented.
C is a programming language which can be programmed very efficiently in its use of memory and processor power. This makes it ideally suited for low- power applications e.g. sensors, embedded systems, microcontrollers. It is very commonly used to develop the software for devices such as set-top boxes, network routers, autonomous controllers, etc.
The same efficient usage of computing resources also makes it ideally suited for high-end supercomputers, where a multiprocessor system may need millions of processes all running at the same time and communicating with each other.
Many operating systems (such as Linux, MacOS, Unix and earlier releases of Windows) are written in C. That means that applications which operate closely with these operating systems (such as security applications, or device drivers) are also developed in C.
Widely used in
o Control and communication systems
o hardware development / embedded systems
o industrial environments
o bioinformatics
C is used as an interface language by the vendors of many different types of equipment, software, devices and microprocessors.
C provides a good basis for learning C++, Java and C#, which share much of the same syntax. The basic principles of the language also provide a good basis for learning Matlab.
3. Our first C program
Let’s take a look at our first C program.
int main(void) { printf("Programming in C is easy. \n”); return 0; } |
A few points to note about this program.
The program starts with the line int main(void) which informs the computer where the program actually starts.
All programs must have exactly one "main" entry point.
void indicates that no arguments are passed to the program from the operating system. int indicates that the program returns an integer back to the operating system. This allows the program to indicate to the operating system that it has run normally and without any errors.
The “curly brackets” { and } signify the beginning and end segments of the program. These brackets are used in many places in C to mark the beginning and end of a section.
The statement #include
The statement printf(…) is actually a function in C which is used for printing variables and text to the screen. The text appearing in double quotes “” are printed without modification. In our example \n means print a new line. Thus our program simple prints the statement:
Programming in C is easy. |
and the cursor is set to the beginning of the next line.
The statement return 0; simply returns a value of zero back to the operating system. Operating systems can interpret return values of programs to indicate normal behaviour or some error condition.
A final point to note is that C statements are frequently terminated by a semi-colon
;
4. Variables
In C, variables are used to store values which can be changed during the course of execution of the program. A variable has a name such as value, sum, total, current_account_balance etc. Note that variable names are case sensitive so Sum and sum are different variable names.
Let’s look at a program incorporating a variable :
#include
int main(void) { int sum;
sum=200+25; printf("The sum of 200 and 25 is %d \n", sum);
return 0; } |
Sample program output :
The sum of 200 and 25 is 225 |
Points to note :
The basic format for declaring a variable is: data-type var1, var2, var3 ….. ;
C has 4 built-in data-types: character, integer, floating-point, double-precision floating point. These are indicated by the C key words char, int, float and double respectively.
In the printf statement, the modifier % is used to display variable values.
In this example, printf(…) contains 2 arguments, the text inside the “” and sum. When the program is executed, the text inside the “” is printed up to the % character.
It then looks at the next argument which is sum, displays its value. Therefore each “%d” is substituted by the value of a variable.
It then continues displaying the rest of the text inside the “”, in this example, a newline character indicated by \n.
The ‘d’ character after the % indicates that the next argument to be displayed is a decimal integer.
‘\t’ here indicates a tab.
Let’s look at a second example using variables:
#include
int main(void) { int value1, value2;
value1=10; value2=25; printf("%d\t%d \n", value1, value2);
return 0; } |
Sample program output
4.1. Variable names
Variable names must begin with a character or underscore and may be followed by any combination of characters a-z or A-Z, or the digits 0-9. The following is a list of valid variable names.
Total
exit_flag
sum1
_valid51
total_count_to_date
code6789
In choosing a variable name bear in mind the following:
Meaningful names for variables make programs:
self-documenting
easier to understand
easier to read
Note also that the length of a variable name doesn’t affect the amount of memory used by a program so don’t feel restricted to, say, 8 characters per variable name.
5. Comments
Comments in a C program increase the readability and understanding of a piece of code. Comments are text in the program inserted between /* and */. Thus a comment looks like :
/* This is a comment. */ |
2022-01-17