CHTP5e - 02-Introduction To C
CHTP5e - 02-Introduction To C
2
Introduction
to C Programming
2
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.
3
2.1 Introduction
2.2 A Simple C Program: Printing a Line of Text
2.3 Another Simple C Program: Adding Two Integers
2.4 Memory Concepts
2.5 Arithmetic in C
2.6 Decision Making: Equality and Relational Operators
4
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
5
Welcome to C!
1 /* Fig. 2.4: fig02_04.c 22
Printing multiple lines with a single printf */
2
3 #include <stdio.h>
Outline
4
5 /* function main begins program execution */
6 int main( void ) Newline characters move the cursor to the next line fig02_04.c
7 {
8 printf( "Welcome\nto\nC!\n" );
9
10 return 0; /* indicate that program ended successfully */
11
12 } /* end function main */
Welcome
to
C!
1 /* Fig. 2.5: fig02_05.c 23
2
3
Addition program */
#include <stdio.h>
Outline
4
5 /* function main begins program execution */
6 int main( void )
fig02_05.c
7 {
8 int integer1; /* first number to be input by user */
9 int integer2; /* second number to be input by user */ Definitions of variables
10 int sum; /* variable in which sum will be stored */
11
12 printf( "Enter first integer\n" ); /* prompt */ scanf obtains a value from the user
13 scanf( "%d", &integer1 ); /* read an integer */ and assigns it to integer1
14
15 printf( "Enter second integer\n" ); /* prompt */
16 scanf( "%d", &integer2 ); /* read an integer */ scanf obtains a value from the user
17 and assigns it to integer2
18 sum = integer1 + integer2; /* assign total to sum */
19
20
Assigns a value to sum
printf( "Sum is %d\n", sum ); /* print sum */
21
22 return 0; /* indicate that program ended successfully */
23
24 } /* end function main */
Fig. 2.6 | Memory location showing the name and value of a variable.
47
2.5 Arithmetic
Arithmetic calculations
– Use * for multiplication and / for division
– Integer division truncates remainder
- 7 / 5 evaluates to 1
– Modulus operator(%) returns the remainder
- 7 % 5 evaluates to 2
Operator precedence
– Some arithmetic operators act before others (i.e., multiplication
before addition)
- Use parenthesis when needed
– Example: Find the average of three variables a, b and c
- Do not use: a + b + c / 3
- Use: (a + b + c ) / 3
50
Arithmetic Algebraic
C opetration C expression
operator expression
Addition + f+7 f + 7
Subtraction – p–c p - c
Multiplication * bm b * m
Division / x
x y or or x÷y x / y
y
Remainder % r mod s r % s
Equality operators
== x == y x is equal to y
Operators Associativity
() left to right
* / % left to right
+ - left to right
< <= > >= left to right
== != left to right
= right to left
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