Chap 2
Chap 2
2
Introduction
to C Programming
OBJECTIVES
In this chapter you will learn:
To write simple computer programs in C.
To use simple input and output statements.
The fundamental data types.
Computer memory concepts.
To use arithmetic operators.
The precedence of arithmetic operators.
To write simple decision-making statements.
2.1
Introduction
2.2
2.3
2.4
Memory Concepts
2.5
Arithmetic in C
2.6
2.1 Introduction
C programming language
Structured and disciplined approach to program design
Structured programming
Introduced in chapters 3 and 4
Used throughout the remainder of the book
1
2
3
4
5
6
#include <stdio.h>
7 {
8
9
10
fig02_01.c
11
Outline
Welcome to C!
#include <stdio.h>
Preprocessor directive
- Tells computer to load contents of a certain file
<stdio.h> allows standard input/output operations
10
11
12
13
14
15
\t
\a
\\
\"
16
17
Right brace }
Indicates end of main has been reached
Linker
When a function is called, 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 function in the library
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
{
printf( "Welcome " );
printf( "to C!\n" );
22
Outline
fig02_03.c
1
2
3
4
#include <stdio.h>
5
6
7
8
9
10
23
Outline
fig02_04.c
printf( "Welcome\nto\nC!\n" );
return 0; /* indicate that program ended successfully */
11
12 } /* end function main */
Welcome
to
C!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
24
Outline
fig02_05.c
/* first number to be input by user */
/* second number to be input by user */
/* variable in which sum will be stored */
Definitions of variables
scanf obtains a value from the
user and assigns it to integer1
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Fig. 2.6 | Memory location showing the name and value of a variable.
48
49
50
2.5 Arithmetic
Arithmetic calculations
Use * for multiplication and / for division
Integer division truncates remainder
- 7 / 5 evaluates to 1
Operator precedence
Some arithmetic operators act before others (i.e., multiplication
before addition)
- Use parenthesis when needed
51
C opetration
Arithmetic Algebraic
C expression
operator
expression
Addition
f+7
f + 7
Subtraction
pc
p - c
Multiplication
bm
b * m
Division
Remainder
x y or
x
or x y x / y
y
r % s
r mod s
52
53
Parentheses
*
/
%
Multiplication
Division
Remainder
+
-
Addition
Subtraction
54
55
56
if control statement
Simple version in this section, more detail later
If a condition is true, then the body of the if statement executed
- 0 is false, non-zero is true
Keywords
Special words reserved for C
Cannot be used as identifiers or variable names
57
Standard algebraic
equality operator or
relational operator
C equality or
relational
operator
Example of
C 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
58
59
60
61
62
63
64
65
1
2
3
4
5
#include <stdio.h>
7
8
66
Outline
fig02_13.c
10
11
12
13
14
15
16
17
if ( num1 == num2 ) {
18
} /* end if */
23
24
25
} /* end if */
27
28
(1 of 3 )
19
20
21
22
26
*/
if ( num1 != num2 ) {
printf( "%d is not equal to %d\n", num1, num2 );
29
30
31
} /* end if */
32
33
34
67
35
36
37
} /* end if */
38
39
Outline
fig02_13.c
(2 of 3 )
40
41
return 0;
42
43 } /* end function main */
43 } /* end function main */
Enter two integers, and I will tell you
the relationships they satisfy: 3 7
3 is not equal to 7
3 is less than 7
3 is less than or equal to 7
(continued on next slide )
68
Outline
fig02_13.c
Enter two integers, and I will tell you
the relationships they satisfy:
7 is equal to 7
7 is less than or equal to 7
7 is greater than or equal to 7
(3 of 3 )
69
70
71
Operators
Associativity
()
left to right
<
<=
==
!=
left to right
left to right
>
>=
left to right
left to right
right to left
72
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