Title: Wavelet transform And Its Applications to Image Processing
1Learning the C language 3. The Nuts and Bolts
of C
2What you have learned last week?
- Built (compiling and linking) a simple C
program under the Visual Studio environment. - Source code, object code, and executable code
- Encountered syntax errors.
- Compile-time errors are useful.
- Run-time errors
- Debugging process
- Debugging all kinds of errors
3What you have learned last week?
- Executed the program in the console mode.
- Many command-line applications are known as
console applications. - Installed Visual Studio at your own PC.
- Reading
43.1 Revisit HelloWorld
5A simple program
include ltiostreamgt int main() stdcout ltlt
"Hello World!" ltlt stdendl return 0
6Elements of a C program
- A main() function
- A program consists of many functions.
- It is expected to return an integer value.
- Inputs and outputs
- Standard output stream (stdcout)
- Standard input stream (stdcin)
- Insert a new-line character (stdendl)
- Standard library
- It is a collection of classes and functions, such
as iostream. - Data type character strings and the new-line
character - Operators ltlt is an insertion operator.
- Statements
7Preprocessor and Program Codes
include ltiostreamgt int main() stdcout ltlt
"Hello World!" ltlt stdendl return 0
preprocessor
The actual program
- Preprocessor
- Instruct the compiler on how to compile the
program - Will not generate machine codes
- Start with a pound () symbol
- Actual program
- Every C program must have the main() function
- It is the beginning point of every C program
Run before compiling
8Preprocessor
include ltiostreamgt
- When compiling a file, we need to obtain the
definitions of some terms in the program codes. - These definitions are recorded in some header
files. - These files are shipped with the compiler or
other resources. - include tells the compiler where to find the
header files and insert this file to that
location of the program - e.g. include ltiostreamgt tells the compiler it
should get the file iostream through the default
path. - e.g. include "myfile" tells the compiler it
should get the file myfile in the current folder.
9Program Codes
Think from the point of view of the compiler.
- The basic element of a program is function.
- A function is composed of
1. Return Type
2. Function name
int main() stdcout ltlt "Hello World!" ltlt
stdendl return 0
3. Input parameters
4. Program codes enclosed by the opening and
closing braces
Note The meaning of stdcout is checked in
iostream
10- The main() function is the beginning point of a
program. - When executing a program, the operating system
will first call the main() function of this
program. - If the above main() function executes
successfully, it should return an integer 0 to
the operating system.
Call main()
main()
Return 0
Means everything fine on executing main()as it is
the last statement.
11Program Codes
Send the string Hello World! to stdcout the
standard output, defined in iostream
int main() stdcout ltlt "Hello World!" ltlt
stdendl return 0
Return an integer 0 to the operating system
- In console mode, the standard output is just the
console, or the Command prompt window. - In C, character string is represented by a
sequence of characters enclosed by . - stdendl means newline (or Enter), also defined
in iostream.
12Namespaces
- stdcout and stdendl means that we are
referring to the cout and endl of the std
namespace - The std namespace is defined in iostream.
- Namespace A new feature of C
- Designed to help programmers develop new software
components without generating naming conflicts. - Naming conflict A name in a program that may be
used for different purposes by different people. - cout and endl are not a part of C, people can
use these two names for any purpose not
necessarily referring to the standard output and
newline.
Folders and files concept in XP
13We can have our own cout by putting it in a
namespace defined by ourselves
include ltiostreamgt namespace myns int
cout0 //Integer variable //No semi-colon int
main() stdcout ltlt mynscout ltlt
stdendl return 0
This cout refers to the number 0
This cout refers to the standard output
- In fact, another definition of cout can be found
in iostream. - The result of this program is a number 0 shown on
the standard output.
14Thats why using cout without the namespace is an
error, because the system does not know which
cout you are referring to.
include ltiostreamgt namespace myns int
cout0 int main() stdcout ltlt cout ltlt
stdendl return 0
?
15- It may be a bit cumbersome to write the namespace
every time. - A short form is to use the using statement.
All names that are not a part of C will belong
to the namespace std, unless otherwise stated.
include ltiostreamgt using namespace std int
main() cout ltlt "Hello World!" ltlt endl return
0
No need to put std in front of cout and endl
16We can also print integers, floating- point
numbers or even combination of string and
integers in standard output
include ltiostreamgt using namespace std int
main() cout ltlt "Hello there.\n" cout ltlt
"Here is 5 "ltlt 5 ltlt "\n" cout ltlt "endl writes
a new line to the screen." cout
ltlt endl cout ltlt "Here is a very big
number\t" ltlt 70000 ltlt endl cout ltlt "Here is
the sum of 8 and 5\t" ltlt 85 ltlt endl cout ltlt
"Here's a fraction\t\t" ltlt (float) 5/8 ltlt
endl cout ltlt "And a very very big
number\t" cout ltlt (double) 70007000
ltlt endl cout ltlt "Replace Frank with your
name...\n" cout ltlt Frank is a C
programmer!\n" return 0
\n - Another way to show newline
escape sequence
\t Add a tab character
Another line
Ex. 3.1a
17Result
18Comments
- A program needs to be well commented to explain
the important points of the program. - Adding comments in the program will not affect
the program execution but only improve
readability. - Comments can be added in two ways
include ltiostreamgt using namespace std int
main() / Text between these two marks are
comments. / cout ltlt "Hello World!\n" return
0 // Text after that are also comments
19Exercise 3.1a
a. Build the program in p. 16. Note the output on
the console. b. Add one statement to the program
which will show your name and height (in cm) in a
single sentence. The name should be shown as a
character string. The height should be shown as
an integer. c. Use the comment symbols / and /
to comment the statements from line 4 to line 8
(inclusive). Is there any change to the results
output?
20More on Functions
- Although a single main() function is enough for
any C program, its bad to do everything by a
single function. - C allows nesting of functions to facilitate
"divide and conquer" of jobs. - The main() can call other functions to help it
complete a task. - When a function is called, the program branches
off from the normal program flow. - When the function returns, the program goes back
to where it left before.
21Mr. A wants to decorate his house
2
Call his friend B to help mowing
1
Call his friend C to help painting
Return him the mowed lawn
Return him the painted house
3
Call a function is just similar to asking
somebody to help!
22include ltiostreamgt using namespace
std //function DemonstrationFunction() //show a
useful message void DemonstrationFunction() cou
t ltlt "In Demonstration Function\n" cout ltlt
"Print one more line\n" //function main -
prints out a message, then //calls
DeomonstrationFunction, then shows //the second
message. int main() cout ltlt "In
main\n" DemonstrationFunction() cout ltlt "Back
in main\n" return 0
23include ltiostreamgt using namespace
std //function DemonstrationFunction() // show a
useful message void DemonstrationFunction() cou
t ltlt "In Demonstration Function\n" cout ltlt
"Print one more line\n" //function main -
prints out a message, then //calls
DeomonstrationFunction, then shows //the second
message. int main() cout ltlt "In
main\n" DemonstrationFunction() cout ltlt "Back
in main\n" return 0
The execution sequence is like that
24Passing Parameters to Function
- To let the called function really help the
main(), sometimes parameters are passed from
main() to the called function. - After finishing the computation, the function
should pass back the results to main(). - It can be achieved by the return statement.
function(a,b)
Branch to function(a,b)
main()
return c
25Mr. A wants to decorate his house
Call his friend B to help mowing and give him a
mowing machine
2
1
Call his friend C to help painting and give him
the paint brush
Return him the mowed lawn
Return him the painted house
3
If you want your friend to help, you'd better
give him the tool!
26include ltiostreamgt using namespace std int Add
(int x, int y) cout ltlt "In Add(),received
"ltltxltlt" and "ltltyltlt"\n" return(xy) int
main() cout ltlt "I'm in main()!\n" int
a,b,c cout ltlt "Enter two numbers " cin gtgt
a cin gtgt b cout ltlt "\nCalling Add()\n" c
Add(a,b) cout ltlt "\nBack in main().\n" cout
ltlt "c was set to " ltlt c cout ltlt
"\nExiting...\n\n" return 0
Input parameters need to declare type - the same
as those in the calling function
Add() will return an integer xy back to main()
Add() is called with two parameters
c holds the return value of Add()
27Exercise 3.1b
a. Build the program in the last slide. Note the
output on the console. b. Modify main() to
calculate the square of c. Add one more function
called Square() to achieve this. The Square()
function will take the square of the parameter
that is passed to it. It will return the result
in the form of an integer back to the calling
function.
28Reading
29Acknowledgement
- The slides are based on the set developed by Dr.
Frank Leung (EIE).
30Some references
- http//www.cplusplus.com/
- http//www.cplusplus.com/doc/tutorial/
- http//www.cplusplus.com/reference/