To C Programming
To C Programming
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 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
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
Welcome to C!
Welcome
to
C!
Fig. 2.6 | Memory location showing the name and value of a variable.
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
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