Chapter 2 - Introduction To C Programming: Outline
Chapter 2 - Introduction To C Programming: Outline
Outline
2.1 2.2 2.3 2.4 2.5 2.6 Introduction A Simple C Program: Printing a Line of Text Another Simple C Program: Adding Two Integers Memory Concepts Arithmetic in C Decision Making: Equality and Relational Operators
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
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
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
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
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Description
Newline. Position the cursor at the beginning of the next line. Horizontal tab. Move the cursor to the next tab stop. Alert. Sound the system bell. Backslash. Insert a backslash character in a string. Double quote. Insert a double quote character in a string.
Fig. 2.2
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
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
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Fig. 2.3: fig02_03.c Printing on one line with two printf statements */ #include <stdio.h> /* function main begins program execution */ int main() { printf( "Welcome " ); printf( "to C!\n" ); return 0; /* indicate that program ended successfully */ } /* end function main */
Outline
fig02_03.c
Welcome to C!
Program Output
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 2 3 4 5 6 7 8 9 10 11 12
/* Fig. 2.4: fig02_04.c Printing multiple lines with a single printf */ #include <stdio.h> /* function main begins program execution */ int main() { printf( "Welcome\nto\nC!\n" ); return 0; /* indicate that program ended successfully */ } /* end function main */
10
Outline
fig02_04.c
Welcome to C!
Program Output
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* Fig. 2.5: fig02_05.c Addition program */ #include <stdio.h> /* function main int main() { int integer1; int integer2; int sum; begins program execution */
11
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 */ /* prompt */ /* read an integer */
printf( "Enter second integer\n" ); /* prompt */ scanf( "%d", &integer2 ); /* read an integer */ sum = integer1 + integer2; printf( "Sum is %d\n", sum ); return 0; /* assign total to sum */ /* print sum */
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12
Outline
Program Output
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
13
2.3 As before
Identifiers: consist of letters, digits (cannot begin with a digit) and underscores( _ ) Case sensitive
14
2.3
When executing the program the user responds to the scanf statement by typing in a number, then pressing the enter (return) key
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15
2.3
16
2.4 Variables
Memory Concepts
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
A visual representation
integer1 45
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
17
2.4
Memory Concepts
integer2
integer2 72 sum
72
117
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
18
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
19
Arithmetic operator
+ * / %
Operation(s)
Parentheses
*, /, or %
or -
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
20
2.6
Step 1.
Step 2.
y = 10 * 5 + 3 * 5 + 7; 10 * 5 is 50
Step 3.
y = 50 + 3 * 5 + 7; 3 * 5 is 15 y = 50 + 15 + 7; 50 + 15 is 65 y = 65 + 7; 65 + 7 is 72 y = 72;
Step 4.
Step 5.
(Last a dd ition)
Step 6.
72
y in )
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
21
2.6
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
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
22
2.6
Example of C condition
x == y x != y
Meaning of C condition
x x
Relational Operators
> < >= <= > < >= <= x > y x < y x >= y x <= y x x x x
is greater than y is less than y is greater than or equal to y is less than or equal to y
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* Fig. 2.13: fig02_13.c Using if statements, relational operators, and equality operators */ #include <stdio.h> /* function main begins program execution */ int main() { int num1; /* first number to be read from user */ int num2; /* second number to be read from user */ printf( "Enter two integers, and I will tell you\n" ); printf( "the relationships they satisfy: " ); scanf( "%d%d", &num1, &num2 ); /* read two integers */ if ( num1 == num2 ) { printf( "%d is equal to %d\n" num1, num2 ); , } /* end if */ if ( num1 != num2 ) { printf( "%d is not equal to %d\n" num1, num2 ); , } /* end if */
23
Outline
fig02_13.c (Part 1 of 2)
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
25 if ( num1 < num2 ) { 26 printf( "%d is less than %d\n" num1, num2 ); , 27 } /* end if */ 28 29 if ( num1 > num2 ) { 30 printf( "%d is greater than %d\n" num1, num2 ); , 31 } /* end if */ 32 33 if ( num1 <= num2 ) { 34 printf( "%d is less than or equal to %d\n" num1, num2 ); , 35 } /* end if */ 36 37 if ( num1 >= num2 ) { 38 printf( "%d is greater than or equal to %d\n" num1, num2 ); , 39 } /* end if */ 40 41 return 0; /* indicate that program ended successfully */ 42 43 } /* end function main */
24
Outline
fig02_13.c (Part 2 of 2)
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
Program Output
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Enter two integers, and I will tell you the relationships they satisfy: 22 12 22 is not equal to 12 22 is greater than 12 22 is greater than or equal to 12
25
Outline
Program Output (continued)
Enter two integers, and I will tell you the relationships they satisfy: 7 7 7 is equal to 7 7 is less than or equal to 7 7 is greater than or equal to 7
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
26
2.6
Operators
* + < == = / <= != > %
Fig. 2.14
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
27
2.6
Keywords
auto break case char const continue default do
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.