2000 Prentice Hall, Inc. All Rights Reserved
2000 Prentice Hall, Inc. All Rights Reserved
Programming
Outline
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
• 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!
• Comments
– Text surrounded by /* and */ is ignored by computer
– Used to describe program
• #include <stdio.h>
– Preprocessor directive - tells computer to load contents of a certain file
– <stdio.h> allows standard input/output operations
• 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 misspelled, linker will spot error because it
cannot find function in library
• 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) previous value
– Reading variables from memory does not change them
• A visual representation
integer1 45
• Arithmetic operators:
C operation Arithmetic Algebraic C expression
operator expression
Addition + f+7 f + 7
Subtraction - p–c p - c
Multiplication * bm b * m
Division / x/y x / y
Modulus % r mod s r % s
• if control structure
– 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
– Control always resumes after the if structure
• Keywords
– Special words reserved for C
– Cannot be used as identifiers or variable names
Relational operators
Keywords
32 num1, num2 );
3.1 Exit main
33