Académique Documents
Professionnel Documents
Culture Documents
and Programming
Semester 1
Week 1, 2015
References
Prescribed text:
Deitel H., Deitel P., "C: How to Program 7th Edition", Prentice-Hall,
2004.
Other references:
J.R.Hanly and E.B. Koffman, C program design for Engineers, 2nd
edition , Addison Wesley, 2001.
K.Kruse, C.L.Tondo and B. Leung, Data structure and program design
in C, 2nd edition 1997.
Johnsonbaugh and Kalin, M.,Application Programming in ANSI C,
MacMillan, 1993
Bronson, G.J C++ for Engineers and Scientist , PWS publishing, 1999.
D.A. Patterson and J.L.Hennessy, Computer Organisation: the
Hardware/Software Interface, Morgan Kaufmann, 2004 edition or later.
J.E. Ridley, Introduction to programmable logic controllers : the
Mitsubishi FX, Arnold , London, 1997.
W. Bolton, Mechatronics: Electronic Control Systems in Mechanical
and Electrical Engineering, 2nd edition, Addison Wesley Longman
Ltd, 1999
Introduction
Disclaimer:
The pages, sources and information, whether printed or linked are for educational purpose only and produced
using freely available information sources in the world wide web and books cited in the unit handout. Also course
material from Deitel & Deitel C: How to program in preparing the lecture notes is highly acknowledged.
What is a Computer?
Computer
Device capable of performing computations
and making logical decisions
Computers process data under the control of
sets of instructions called computer programs
Hardware
Various devices comprising a computer
Keyboard, mouse, display, disk storage,
memory, CD-ROM, and processing units
Software
Programs that run on a computer
What is a Computer?
Computer Organisation
Six logical units in every computer:
1. Input unit
Obtains information from input devices (keyboard,
mouse, gamepad, joystick, webcam etc)
2. Output unit
Outputs information (to screen, to printer, to
control other devices [such as robot arms])
3. Memory unit
Rapid access, low capacity, stores input
information (usually more expensive than unit 6)
INPUT
Device
(Keyboard)
(Mouse)
(Disc Drive)
Memory Location
Numbers
0
1
2
3
4
5
6
7
8
Memory - RAM
OUTPUT
Device
(Screen)
(Disc Drive)
(Serial Port)
(Printer Port)
Games Port)
CPU Model
ALU
Control Logic
R1
R2
R3
..
Arithmetic
Logic
Unit
CPU
Internal
Registers
Intel Core 2
iPhone 3Gs ARM
processor
Operating Systems
Operating systems is a system software. It
is a collection of programs that must be
available to any computer system in order
to operate it.
Examples:
Microsoft Windows
3.1, 2000, XP, Vista, 7, CE, etc
Apple Mac
Linux / Unix, Chrome OS
Specialized: RTLinux, QNX, etc
Operating systems
Manage transitions between jobs
Increased throughput
Amount of work computers process
Multiprogramming
Computer resources are shared by many jobs or tasks
Timesharing
Computer runs a small portion of one users job then
moves on to service the next user
Personal Computing,
Distributed Computing, and
Client/Server Computing
Personal computers
Economical enough for individual
Distributed computing
Computing distributed over networks
Client/server computing
Sharing of information across computer
networks between file servers and clients
(personal computers)
BASEPAY
OVERPAY
GROSSPAY
Source code
10100110 01110110
00100110 00000000
11111010 11111010
01001110 10100110
11100110 10010110
11001110 00101110
10100110 01001110
11111010 01100110
01001110 10000110
etc...
Executable code
Why C?
Structured language (usually)
Standard library exists, allowing portability
See D&D 4/e Appendix B (web links in D&D 4/e)
History of C
Standardization
Many slight variations of C existed, and were
incompatible
ANSI (American National Standard Institute) created
in 1989, updated in 1999
Program:
talking to computer (compiler)
can be regarded as a formal expression of an
algorithm
Basic Structure of a C
Program
Example: Hello World
Algorithm:
C Program:
#include <stdio.h>
int main()
{
printf(Hello World!);
return 0;
}
Editor
Disk
Preprocessor
Disk
Compiler
Disk
Linker
Disk
Program is created in
the editor and stored
on disk.
Preprocessor program
processes the code.
Compiler creates
object code and stores
it on disk.
Linker links the object
code with the libraries
Primary Memory
Loader
Disk
6. Execute
Primary Memory
CPU
..
..
..
1 Kg bag
of rice
First half of
the
chessboard:
100 tons of
rice
1 Kg bag
of rice
First half of
the
chessboard:
100 tons of
rice
Second half
of the
chessboard:
400 billion
tons of rice
(1000 years
of current
global
production)
1 Kg bag
of rice
In 1947, Bardeen,
Brattain and
Shockley attached
two gold contacts to
a crystal of
germanium
2010: SPARC T3
1,000,000,000 Transistors
This notebook
P4 Intel Prescott
2,000,000,000,000 transistors2004
(125,000,00 transistors)
2010 SPARC T3
(1,000,000,000 transistors)
2013 Xeon Phi
(5,000,000,000 transistors)
This notebook
> 2 trillion transistors
?
How many on the last square?
Moores Law
Moores Law
Hardware Trends
Interpretations of Moores Law: Every
year or two the following roughly double
Amount of memory in which to execute
programs
Amount of secondary storage (such as disk
storage)
Used to hold programs and data over the longer
term
Processor speeds
The speeds at which computers execute their
programs
A Simple C Program:
Printing a Line of Text
/* Fig. 2.1: fig02_01.c
A first program in C */
#include <stdio.h>
/* function main begins program execution */
int main()
{
printf( "Welcome to C!\n" );
return 0; /* indicate that program ended successfully */
} /* end function main */
A Simple C Program:
Printing a Line of Text
/* Fig. 2.1: fig02_01.c
A first program in C */
#include <stdio.h>
/* function main begins program execution */
int main()
{
printf( "Welcome to C!\n" );
return 0; /* indicate that program ended successfully */
} /* end function main */
A Simple C Program:
Printing a Line of Text
/* Fig. 2.1: fig02_01.c
A first program in C */
#include <stdio.h>
/* function main begins program execution */
int main()
{
printf( "Welcome to C!\n" );
return 0; /* indicate that program ended successfully */
} /* end function main */
A Simple C Program:
Printing a Line of Text
/* Fig. 2.1: fig02_01.c
A first program in C */
#include <stdio.h>
/* function main begins program execution */
int main()
{
printf( "Welcome to C!\n" );
return 0; /* indicate that program ended successfully */
} /* end function main */
A Simple C Program:
Printing a Line of Text
/* Fig. 2.1: fig02_01.c
A first program in C */
#include <stdio.h>
/* function main begins program execution */
int main()
{
printf( "Welcome to C!\n" );
return 0; /* indicate that program ended successfully */
} /* end function main */
Another comment /* */
A Simple C Program:
Printing a Line of Text
/* Fig. 2.1: fig02_01.c
A first program in C */
#include <stdio.h>
/* function main begins program execution */
int main()
{
printf( "Welcome to C!\n" );
return 0; /* indicate that program ended successfully */
} /* end function main */
A Simple C Program:
Printing a Line of Text
/* Fig. 2.1: fig02_01.c
A first program in C */
#include <stdio.h>
/* function main begins program execution */
int main()
{
printf( "Welcome to C!\n" );
return 0; /* indicate that program ended successfully */
} /* end function main */
A Simple C Program:
Printing a Line of Text
/* Fig. 2.1: fig02_01.c
A first program in C */
#include <stdio.h>
/* function main begins program execution */
int main()
{
printf( "Welcome to C!\n" );
return 0; /* indicate that program ended successfully */
} /* end function main */
Welcome to C!
A Simple C Program:
Printing a Line of Text
/* Fig. 2.1: fig02_01.c
A first program in C */
#include <stdio.h>
/* function main begins program execution */
int main()
{
printf( "Welcome to C!\n" );
return 0; /* indicate that program ended successfully */
} /* end function main */
Welcome to C!
A Simple C Program:
Printing a Line of Text
/* Fig. 2.1: fig02_01.c
A first program in C */
#include <stdio.h>
/* function main begins program execution */
int main()
{
printf( "Welcome to C!\n" );
return 0; /* indicate that program ended successfully */
} /* end function main */
Welcome to C!
A Simple C Program:
Printing a Line of Text
Escape Sequence
Description
\n
\t
\a
\\
\"
Fig. 2.2
A Simple C Program:
Printing a Line of Text
Linker
When a function is called, the linker locates it
in the library
Inserts it into object program
If function name is misspelled, the linker will
produce an error because it will not be able to
find the function in the library
/* prompt */
/* read an integer
/* prompt */
/* read an integer
/* assign total to
/* print sum */
ended successfully
*/
*/
sum */
*/
/* prompt */
/* read an integer
/* prompt */
/* read an integer
/* assign total to
/* print sum */
ended successfully
*/
*/
sum */
*/
integer1
/* prompt */
/* read an integer
/* prompt */
/* read an integer
/* assign total to
/* print sum */
ended successfully
*/
*/
sum */
*/
integer1
/* function main
int main()
{
int integer1;
int integer2;
int sum;
integer2
/* prompt */
/* read an integer
/* prompt */
/* read an integer
/* assign total to
/* print sum */
ended successfully
*/
*/
sum */
*/
integer1
/* function main
int main()
{
int integer1;
int integer2;
int sum;
integer2
sum
/* prompt */
/* read an integer
/* prompt */
/* read an integer
/* assign total to
/* print sum */
ended successfully
*/
*/
sum */
*/
integer1
/* function main
int main()
{
int integer1;
int integer2;
int sum;
integer2
sum
/* prompt */
/* read an integer
/* prompt */
/* read an integer
/* assign total to
/* print sum */
ended successfully
*/
*/
sum */
*/
integer1
/* function main
int main()
{
int integer1;
int integer2;
int sum;
integer2
sum
/* prompt */
/* read an integer
/* prompt */
/* read an integer
/* assign total to
/* print sum */
ended successfully
*/
*/
sum */
*/
integer1
/* function main
int main()
{
int integer1;
int integer2;
int sum;
integer2
sum
/* prompt */
/* read an integer
/* prompt */
/* read an integer
/* assign total to
/* print sum */
ended successfully
*/
*/
sum */
*/
integer1
/* function main
int main()
{
int integer1;
int integer2;
int sum;
integer2
sum
/* prompt */
/* read an integer
/* prompt */
/* read an integer
/* assign total to
/* print sum */
ended successfully
*/
*/
sum */
*/
integer1
/* function main
int main()
{
int integer1;
int integer2;
int sum;
integer2
sum
/* prompt */
/* read an integer
/* prompt */
/* read an integer
/* assign total to
/* print sum */
ended successfully
*/
*/
sum */
*/
integer1
/* function main
int main()
{
int integer1;
int integer2;
int sum;
integer2
sum
/* prompt */
/* read an integer
/* prompt */
/* read an integer
/* assign total to
/* print sum */
ended successfully
*/
*/
sum */
*/
Memory Concepts
Variables
Variable names correspond to locations in the
computer's memory
Every variable has a name, a type, a size and a
value
Whenever a new value is placed into a variable
(through scanf, for example), it replaces (and
destroys) the previous value
Reading variables from memory does not change
them
Data Type
Values and variables can have many types
Not all values are of the same types
For example: 7 + cat makes no sense
Data Type
Built-in types:
char, int, float, double
Type modifiers: long, short, const
eg. long int, const float
Character Representation
Characters (letters) are stored as a small
integer (8 bits; 0-255)
Each character has a unique integer
equivalent specified by its position in the
ASCII table (pronounced as-key)
American Standard Code for Information
Interchange
see Appendix D of D&D
Character Representation
ASCII values range from 0 to 127
value 0: special character \0
(a.k.a. NUL character)
value 127: special character <DEL>
other special characters:
\n \t \ \\ etc.
various extended sets from 128 to 255
Arithmetic Operators
Arithmetic calculations
Use * for multiplication and / for division
Integer division truncates remainder
7/5
evaluates to 1
evaluates to 2
Operator precedence
Some arithmetic operators act before others
(i.e., multiplication before addition)
Use parenthesis when needed
Arithmetic Operators
Arithmetic operators:
C operation
Addition
f +7
f+7
Subtraction
pc
p-c
Multiplication
bm
b*m
Division
x/ y
x/y
Modulus
r mod s
r%s
y = 2 * 5 * 5 + 3 * 5 + 7;
2 * 5 is 10
St ep 2.
y = 10 * 5 + 3 * 5 + 7;
10 * 5 is 50
St ep 3.
y = 50 + 3 * 5 + 7;
3 * 5 is 15
St ep 4.
y = 50 + 15 + 7;
50 + 15 is 65
St ep 5.
y = 65 + 7;
(Last a dd ition)
65 + 7 is 72
St ep 6.
y = 72;
C equality or
Example of C
relational operator condition
Meaning of C condition
==
x == y
x is equal to y
!=
x != y
x is not equal to y
>
>
x > y
x is greater than y
<
<
x < y
x is less than y
>=
>=
x >= y
<=
<=
x <= y
Equality Operators
Relational Operators
3
4
Deit el &Deital
#include <stdio.h>
5
6
int main()
10
*/
11
12
p rintf( "En ter two inte gers, and I will tel l you \n " );
13
14
15
16
17
18
19
i f ( nu m1 == num2 ) {
prin tf( "%d is equal to %d \n" , num1, num2 );
} /* end if */
20
21
22
23
i f ( num1 != nu m2 ) {
prin tf( "%d is not equal to %d \n", num1, num 2 );
} /* end if */
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
return 0;
42
43 } /* end function main */
3
4
Deit el &Deital
#include <stdio.h>
5
6
int main()
10
*/
11
12
p rintf( "En ter two inte gers, and I will tel l you \n " );
13
14
15
16
17
18
19
i f ( nu m1 == num2 ) {
prin tf( "%d is equal to %d \n" , num1, num2 );
} /* end if */
20
21
22
23
i f ( num1 != nu m2 ) {
prin tf( "%d is not equal to %d \n", num1, num 2 );
} /* end if */
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
return 0;
42
43 } /* end function main */
num1
3
4
Deit el &Deital
#include <stdio.h>
5
6
int main()
10
*/
num1
11
12
p rintf( "En ter two inte gers, and I will tel l you \n " );
13
14
15
16
17
18
19
i f ( nu m1 == num2 ) {
prin tf( "%d is equal to %d \n" , num1, num2 );
} /* end if */
20
21
22
23
i f ( num1 != nu m2 ) {
prin tf( "%d is not equal to %d \n", num1, num 2 );
} /* end if */
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
return 0;
42
43 } /* end function main */
num2
3
4
Deit el &Deital
#include <stdio.h>
5
6
int main()
10
*/
num1
11
12
p rintf( "En ter two inte gers, and I will tel l you \n " );
13
14
15
num2
16
17
18
19
i f ( nu m1 == num2 ) {
prin tf( "%d is equal to %d \n" , num1, num2 );
} /* end if */
20
21
22
23
i f ( num1 != nu m2 ) {
prin tf( "%d is not equal to %d \n", num1, num 2 );
} /* end if */
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
return 0;
42
43 } /* end function main */
3
4
Deit el &Deital
#include <stdio.h>
5
6
int main()
10
*/
num1
11
12
p rintf( "En ter two inte gers, and I will tel l you \n " );
13
14
15
num2
16
17
18
19
i f ( nu m1 == num2 ) {
prin tf( "%d is equal to %d \n" , num1, num2 );
} /* end if */
20
21
22
23
i f ( num1 != nu m2 ) {
prin tf( "%d is not equal to %d \n", num1, num 2 );
} /* end if */
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
return 0;
42
43 } /* end function main */
3
4
Deit el &Deital
#include <stdio.h>
5
6
int main()
10
*/
num1
num2
11
12
p rintf( "En ter two inte gers, and I will tel l you \n " );
13
14
15
16
17
18
19
i f ( nu m1 == num2 ) {
prin tf( "%d is equal to %d \n" , num1, num2 );
} /* end if */
20
21
22
23
i f ( num1 != nu m2 ) {
prin tf( "%d is not equal to %d \n", num1, num 2 );
} /* end if */
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
return 0;
42
43 } /* end function main */
3
4
Deit el &Deital
#include <stdio.h>
5
6
int main()
10
*/
num1
num2
11
12
p rintf( "En ter two inte gers, and I will tel l you \n " );
13
14
15
16
17
18
19
i f ( nu m1 == num2 ) {
prin tf( "%d is equal to %d \n" , num1, num2 );
} /* end if */
20
21
22
23
i f ( num1 != nu m2 ) {
prin tf( "%d is not equal to %d \n", num1, num 2 );
} /* end if */
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
return 0;
42
43 } /* end function main */
3
4
Deit el &Deital
#include <stdio.h>
5
6
int main()
10
*/
num1
num2
11
12
p rintf( "En ter two inte gers, and I will tel l you \n " );
13
14
15
16
17
18
19
i f ( nu m1 == num2 ) {
prin tf( "%d is equal to %d \n" , num1, num2 );
} /* end if */
20
21
22
23
i f ( num1 != nu m2 ) {
prin tf( "%d is not equal to %d \n", num1, num 2 );
} /* end if */
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
return 0;
42
43 } /* end function main */
3
4
Deit el &Deital
#include <stdio.h>
5
6
int main()
10
*/
num1
num2
11
12
p rintf( "En ter two inte gers, and I will tel l you \n " );
13
14
15
16
17
18
19
i f ( nu m1 == num2 ) {
prin tf( "%d is equal to %d \n" , num1, num2 );
} /* end if */
20
21
22
23
i f ( num1 != nu m2 ) {
prin tf( "%d is not equal to %d \n", num1, num 2 );
} /* end if */
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
return 0;
42
43 } /* end function main */
3
4
Deit el &Deital
#include <stdio.h>
5
6
int main()
10
*/
num1
num2
11
12
p rintf( "En ter two inte gers, and I will tel l you \n " );
13
14
15
16
17
18
19
i f ( nu m1 == num2 ) {
prin tf( "%d is equal to %d \n" , num1, num2 );
} /* end if */
20
21
22
23
i f ( num1 != nu m2 ) {
prin tf( "%d is not equal to %d \n", num1, num 2 );
} /* end if */
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
return 0;
42
43 } /* end function main */
3
4
Deit el &Deital
#include <stdio.h>
5
6
int main()
10
*/
num1
num2
11
12
p rintf( "En ter two inte gers, and I will tel l you \n " );
13
14
15
16
17
18
19
i f ( nu m1 == num2 ) {
prin tf( "%d is equal to %d \n" , num1, num2 );
} /* end if */
20
21
22
23
i f ( num1 != nu m2 ) {
prin tf( "%d is not equal to %d \n", num1, num 2 );
} /* end if */
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
return 0;
42
43 } /* end function main */
3
4
Deit el &Deital
#include <stdio.h>
5
6
int main()
10
*/
num1
num2
11
12
p rintf( "En ter two inte gers, and I will tel l you \n " );
13
14
15
16
17
18
19
i f ( nu m1 == num2 ) {
prin tf( "%d is equal to %d \n" , num1, num2 );
} /* end if */
20
21
22
23
i f ( num1 != nu m2 ) {
prin tf( "%d is not equal to %d \n", num1, num 2 );
} /* end if */
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
return 0;
42
43 } /* end function main */
Keywords
Keywords
Special words reserved for C programming
Cannot be used as identifiers or variable
names
For example, dont do this: int int;
Keywords
Keywords
auto
double
int
struct
break
else
long
switch
case
enum
register
typedef
char
extern
return
union
const
float
short
unsigned
continue
for
signed
void
default
goto
sizeof
volatile
do
if
static
while
Cs reserved keywords.
Logical Operators
&& ( logical AND )
Returns true if both conditions are true
|| ( logical OR )
Returns true if either of its conditions are true
Logical Operators
expression1
nonzero
nonzero
nonzero
nonzero
expression2
expression1 || expression2
nonzero
nonzero
nonzero
nonzero
! expression
nonzero
Compound Assignment
Operators
Compound assignment operators abbreviate
assignment expressions
c = c + 3;
can be rewritten as
variable operator= expression;
-=
*=
/=
%=
4
5
3
9
is
is
is
is
same
same
same
same
as
as
as
as
(d
(e
(f
(g
=
=
=
=
d
e
f
g
*
/
%
4)
5)
3)
9)
Compound Assignment
Operators
Assume: int c = 3, d = 5, e = 4, f = 6, g = 12;
Compound assignment
operator
Sample expression
Explanation
Assigns
+=
c += 7
c = c + 7
10 to c
-=
d -= 4
d = d - 4
1 to d
*=
e *= 5
e = e * 5
20 to e
/=
f /= 3
f = f / 3
2 to f
%=
g %= 9
g = g % 9
3 to g
Preincrement
Operator is used before the variable (++c or --c)
Variable is changed before the expression it is in is
evaluated
Postincrement
Operator is used after the variable (C++ or C--)
Expression executes before the variable is changed
Prints 6
printf( "%d", c++ );
Prints 5
Sample expression
Explanation
++
++a
++
a++
--
--b
--
b--
int main() {
int score;
int max_score;
int passmark;
}
#include <stdio.h>
4
5
int main()
int c;
/* define variable */
9
10
/* demonstrate postincrement */
11
c = 5;
/* assign 5 to c */
12
printf( "%d\n", c );
/* print 5 */
13
14
15
16
/* demonstrate preincrement */
17
c = 5;
/* assign 5 to c */
18
printf( "%d\n", c );
/* print 5 */
19
20
printf( "%d\n", c );
21
22
/* print 6 */
fig03_13.c
23
24 } /* end function main */
5
5
6
5
6
6
Conditional operator ( ? : )
C provides a ternary operator to obtain one of
two values depending on a condition.
It consists of two symbols ? and : and must be
together in the following way:
expr1 ? expr2 : expr3
i, j, k;
1;
2;
i>2 ? (i+1)
: (j+1);
/* k is 3 */
Associativity
Type
right to left
unary
left to right
multiplicative
left to right
additive
left to right
relational
left to right
equality
&&
left to right
logical AND
||
left to right
logical OR
?:
right to left
conditional
right to left
assignment
left to right
comma
++
--
<
<=
==
!=
+=
>
-=
(type)
>=
*=
/=
%=
Description
Example
sqrt( x )
square root of x
exp( x )
exponential function ex
log( x )
natural logarithm of x
(base e)
exp(
exp(
log(
log(
log10( x )
fabs( x )
absolute value of x
ceil( x )
floor( x )
pow( x, y )
fmod( x, y )
remainder of x/y as a
floating point number
pow( 2, 7 ) is 128.0
pow( 9, .5 ) is 3.0
fmod( 13.657, 2.333 ) is
1.992
sin( x )
trigonometric sine of x
(x in radians)
cos( x )
trigonometric cosine of x
(x in radians)
tan( x )
trigonometric tangent of x
(x in radians)
1.0 ) is
2.0 ) is
2.718282
7.389056
2.718282
7.389056
) is 1.0
) is 2.0
Formatted Input/Output
All input/output operations in C are
performed by input/output functions
Most common input/output functions are
supplied by C standard library through the
preprocessor directive
#include <stdio.h>
(in <stdio.h>)
Format
printf( format-control-string, other-arguments );
Format control string: describes output format
Other-arguments: correspond to each conversion
specification in format-control-string
Each format specification begins with a percent sign(%), ends
with conversion specifier
Printing Integers
Conversion Specifier
Description
o
u
x
or X
or l (letter l)
Printing Integers
Integer
Whole number (no decimal point): 25, 0, -9
Positive, negative, or zero
Minus sign will print only if needed
10
11
12
13
14
15
16
17
18
19
/* i same as d in printf */
fig09_02.c
20
21 } /* end main */
455
455
455
-455
32000
2000000000
707
455
4294966841
1c7
1C7
Printing Floating-Point
Numbers
Floating Point Numbers
Have a decimal point (33.5)
Exponential notation (computer's version of scientific
notation)
150.3 is 1.503 x 10 in scientific
150.3 is 1.503E+02 in exponential (E stands for exponent)
use e or E
Printing Floating-Point
Numbers
Conversion specifier
Description
e or E
g or G
3
4
5
#include <stdio.h>
6
7
int main()
10
11
12
13
14
15
16
17
18
19 } /* end main */
1.234568e+006
1.234568e+006
-1.234568e+006
1.234568E+006
1234567.890000
1.234568e+006
1.234568E+006
s
Requires a pointer to char as an argument
Prints characters until NULL ('\0') encountered
Cannot print a char argument
Remember
Single quotes for character constants ('z')
Double quotes for strings "z" (which actually
contains two characters, 'z' and '\0')
#include <stdio.h>
4
5
int main()
10
11
12
13
14
15
16
17
18 } /* end main */
A
This is a string
This is a string
This is also a string
Floating point
Number of digits to appear after decimal (e and f)
For g maximum number of significant digits
Strings
Maximum number of characters to be written from string
Format
Use a dot (.) then precision number after %
%.3f
Example:
printf( "%*.*f", 7, 2, 98.736 );
#include <stdio.h>
4
5
int main()
printf( "%4d\n", 1 );
printf( "%4d\n", 12 );
10
11
12
13
printf( "%4d\n", -1 );
14
15
16
17
18
19
20
21 } /* end main */
1
12
123
1234
12345
-1
-12
-123
-1234
-12345
3
4
#include <stdio.h>
5
6
int main()
int i = 873;
/* initialize int i */
double f = 123.94536;
/* initialize double f */
10
11
12
13
printf( "\t%.4d\n\t%.9d\n\n", i, i );
14
15
16
printf( "\t%.3f\n\t%.3e\n\t%.3g\n\n", f, f, f );
17
18
19
printf( "\t%.11s\n", s );
20
21
22
23 } /* end main */
Description
\'
\"
\?
\\
\a
\b
\f
\n
\r
\t
\v
Escape sequences.
scanf
Input formatting
Capabilities
Input all types of data
Input specific characters
Skip specific characters
Format
scanf(format_control_string,other_arguments);
Format-control-string
Describes formats of inputs
Other-arguments
Pointers (&) to variables where input will be stored
Description
Integers
d
x or X
h or l
Place before any of the integer conversion specifiers to indicate that a short
or long integer is to be input.
Conversion specifiers for scanf.
Description
Floating-point
numbers
e, E, f, g or G
l or L
Characters and
strings
C
Read a string. The corresponding argument is a pointer to an array of type char that is large enough to hold the string and a
terminating null ('\0') characterwhich is automatically
added.
Scan set
[scan characters]
Miscellaneous
P
/* Reading integers */
#include <stdio.h>
4
5
int main()
int a; /* define a */
int b; /* define b */
int c; /* define c */
10
int d; /* define d */
11
int e; /* define e */
12
int f; /* define f */
13
int g; /* define g */
14
15
16
17
18
19
20
21
22
23 } /* end main */
char x;
/* define x */
9
10
11
12
13
14
15
16
17
18
19 } /* end main */
#include <stdio.h>
4
5
int main()
9
10
11
12
13
14
15
16
17 } /* end main */
Program
Output
#include <stdio.h>
4
5
int main()
8
9
10
11
12
13
14
15
16 } /* end main */
#include <stdio.h>
4
5
int main()
int x; /* define x */
int y; /* define y */
9
10
11
12
13
14
15
16
17 } /* end main */
Control Structures in C
Sequence structures
Built into C. Programs executed sequentially
by default
Selection Structure
C has three types: if, ifelse, and switch
Repetition structures:
C has three types: while, dowhile and for
Pseudocode
Pseudocode
Informal language that helps us develop
algorithms
Similar to everyday English
Not actually executed on computers
Helps us think out a program before writing
it fully in C
Easy to convert into a corresponding C program
Consists only of executable statements
If condition true
Print statement executed and program goes on to
next statement
If false, print statement is ignored and the program
goes onto the next statement
Indenting makes programs easier to read
structure
A decision can be made on
any expression.
zero - false
true
grade >= 60
nonzero - true
print Passed
Example:
3 - 4 is true
false
ifelse
Specifies an action to be performed both when the
condition is true and when it is false
Psuedocode:
If students grade is greater than or equal to 60
Print Passed
else
Print Failed
false
true
grade >= 60
print Failed
print Passed
>= 60 ) {
"Passed.\n" );
"Failed.\n" );
"You must take this course again.\n" );
case a action(s)
break
case b action(s)
break
case z action(s)
break
false
true
case b
false
.
.
.
true
case z
false
default action(s)
Discussion: Typecasting
The cast operator is a unary operator that
allows explicit conversion
float avg = (float) total / count;
Summary
Reading
Deitel & Deitel:
Chapter 2, 2.1 2.5
Chapter 9, 9.1 9.5, 9.11
Chapter 3.1 3.6
-Chapter 4.7
Hanly
Chapter 2.5, 2.6 & 4