Computer Programming in C
Computer Programming in C
Programming in C
By
Mr. Harry & Damen
Overview
This course is designed for Engineering students.
By the end of the course, students should be able to conceive,
create, construct and write basic computer programs in the C
programming language.
Book
C By Example by Noel Kalicharan
Operating System / Environment
Ubuntu Linux / Gnu Common Compiler (gcc)
Outline
Introduction
Algorithms
Basics of Functions and Operators
Control Flow Constructs
The C Preprocessor
Data types and storage classes
Character handling and strings
Pointers
Basic Structures and Linked Lists
File Input / Output
Binary Trees and Other Structures
Introduction
● What is computer programming?
● Why study computer programming as an Engineer?
● What are computer programs?
● How are computers programmed?
● What are programming languages?
Introduction
● How to write a program
● How to compile a program
● Running the program
● Debugging the program
How to write a program
● Keywords in C (reserved words)
● Basic Data types
● Variables and their values
● Operators
● Expressions and statements
● Functions and header files
● A word on program layout
How to write a program
● Keywords in C (reserved words)
auto break case char const continue
default do double else enum extern
float for goto if int long
register return short signed sizeof static
struct switch typedef union unsigned void
volatile while
All reserved words should be written in lower case.
How to write a program
● Basic Data types
● char – A single character enclosed by single quotes (apostrophes) e.g
'a', 'd', '9', '\n', '\''...
● int – integer (whole number) e.g 25, 99, 0, 34, 235... Octal constants
begin with leading 0 e.g 59 = 073. Hexadecimal constants begin with a
leading 0x (0X) e.g 0x3b or 0x3B
● double – doubleprecision floating point number. Written with a decimal
point (78.5, 58.9) with exponential e or E (0.58e89, 1.5E9)
● float – singleprecision floating point. Written by adding suffix f or F to a
double constant (75.8F, 0.5e2f)
How to write a program
● Variables and their values
Variables are denoted by Identifiers. An Identifier begins with a
letter or underscore(_). In ANSI C, only the first 31 characters of an
identifier's name are significant. Both upper & lower case letters
may be used. e.g maxStudentNumber, _score, ...
Variable names should not be the same as reserved words.
Variables have types and are declared (defined) as such
<data type> <variable name>; e.g int maxLength;
Analogy to variables in mathematics
How to write a program
●Qualifiers
Integers(int), characters(char) and doubles can be qualified in
declarations by one of a combination of the following reserved
words (qualifiers): short, long, signed, unsigned. e.g
short int numTeams;
long int gross;
unsigned int count;
short unsigned int numStudents;
Assuming that a char is stored in 8bits, an unsigned char can assume values from 0 to
255 while a signed char can assume values from 128 to 127.
Long double gives twice the precision as double.
How to write a program
●Operators and Operands
Arithmetic – They include addition(+), subtraction(),
multiplication(*), division(/), modulus(%).
Assignment – used to assign the value of an expression to a
variable. (=, +=, =, *=, /=, %=)
variable op= expression is equivalent to
variable = variable op expression. e.g c += 4 ≡ c = c + 4.
Relational – used for comparing quantities. They include equal
to(==), not equal to(!=), greater than(>), less than(<), less than or
equal to(<=), greater than or equal to(>=).
How to write a program
● Operators and Operands
● BoDMAS (Brackets of Division, Multiplication, Addition and Subtraction)
a = b = c = 12 /*Assignment operator associate from right to left a = (b = (c = 12))*/
a == b == c /*Compare operator associates as such (a == b) == c
better (a == b)&&(c == c)*/
a && b || c /* ! has a higher precedence than && which is higher than || */
How to write a program
● Expressions and statements
An expression consists of operands and operators.
e.g y = m*x + c, length > width, aSalary = mSalary*numMonth + tBonus, ...
A simple statement is an expression followed by a semicolon.
e.g y = m*x;
A compound statement begins with an open brace ({) followed by
any number of simple statements and ends with a close brace (})
e.g {count = 0; max = 5; return 0;}
How to write a program
●Functions and header files
Functions are building blocks from which larger programs are
constructed. (dividing large jobs into smaller once) Analogous to
functions in mathematics.
An integral part of any C programming system is the provision of a
library of functions (at least I/O functions)
Each class of functions has a Header file which contains the
function declaration, together with other variables and constants.
e.g ctype.h – declarations for character handling functions
stdio.h declarations for input / output handling functions.
How to write a program
● A word on program layout
In general, C does not require the program to be laid out in a particular way.
In the layout of your program text, the following should be considered:
●
Comments – /* Every thing between these symbols is considered as comments */
● Documentation – The habit of including author's name, program name, date,
modification history etc at the beginning of the program text as comments.
● Indentations – Use to group statement blocks.
● Flow and readability – Ensure that your program reads logically from left to
right, top to bottom.
The program text
/******************************************************/
/* Program title: Welcome */
/* Authors: Harry and Damen */
/* Date: 26 October 2010 */
/* */
/******************************************************/
#include <stdio.h> /* includes the standard I/O header file */
main() /* Calls the main function */
{
printf(“Welcome to CEF207\n”); /* \n – newline character */
}
How to compile a program
● The program text
● The Text editor
● The compiler
● The Object file
● The linker
● The loader
● The executable program
How to compile a program
harry@ubuntu:~$ cd Desktop
harry@ubuntu:~/Desktop$ cd CEF207
harry@ubuntu:~/Desktop/CEF207$ gcc c welcome.c
harry@ubuntu:~/Desktop/CEF207$ gcc o welcome welcome.o
harry@ubuntu:~/Desktop/CEF207$ ./welcome
Running the program
● Ways of running a program
● Requirements for running a program
● Environmental and hardware requirements
● The input of a program
● The output of a program
● Program termination
Debugging the program
A debugger program
●
The executable program
●
Patience to handle the frustration
●
Stepping through the program code while
●
observing its progress.
Identify errors and make corrections in the
●
program text.
Recompile the program text
●
Algorithms
● What are algorithms?
● Why are algorithms relevant to computer programming?
Definition of an Algorithm
Properties of an Algorithm
1. Precision. The steps are precisely stated.
2. Uniqueness. The result of executing each step is uniquely
determined by the inputs and the result of preceding steps.
3. Finiteness. The algorithm stops after finitely many instructions
have been executed.
4. Input. The algorithm receives input.
5. Output. The algorithm produces output.
6. Generality. The algorithm applies to a set of inputs.
Representation of Algorithms
● An algorithm can be represented in a natural
language such as English, French, Pidgin, etc.
● It can also be represented as a computer
program or even as a hardware design.
● There are two popular ways of show the
representation of algorithms: flow diagrams (flow
charts) and pseudocodes.
Flow Diagram Symbols
Flow Diagram Example
Pseudocode
Pseudocode Example
Basics of Functions and Operators
● What are functions?
● What are operators?
● How are they used in computer programming?
Control Flow Constructs
● What are control flow constructs?
● What are some examples of control flow constructs in computer
programming?
Control Flow Constructs
● The if...else statement
● The while statement
● The for statement
● The do..while statement
● The switch statement
● The continue statement
The C Preprocessor
● What is a processor?
● What is a preprocessor?
● What is the use of #include<stdio.h> in a C program?
Data types and storage classes
● What are data types?
● What are storage classes?
● Give examples of data types and storage classes.
Character handling and strings
● What are strings?
● How are characters and strings handled (declared, stored,
manipulated and represented) in a computer program?
Pointers
● What are pointers?
● Why are pointers necessary in computer programming?
Basic Data Structures and Linked Lists
● What are data structures?
● What are linked lists?
File Input / Output
● What is a File?
● How is data stored / read from a file?
Binary Trees and Other Data Structures
● What are Binary trees?
● What other Data structures exist?