04 C Programming Parta PDF
04 C Programming Parta PDF
C Programming (Part A)
Copyright © 2008 W. W. Norton & Company. All rights Reserved
Overview (King Ch. 1-7)
• Introducing C (Ch. 1)
• C Fundamentals (Ch. 2)
• Formatted Input/Output (Ch. 3)
• Expressions (Ch. 4)
• Selection Statements (Ch. 5)
• Loops (Ch. 6)
• Basic Types (Ch. 7)
2
Chapter 1
Introducing C
Origins of C
4
Standardization of C
• K&R C
− Described in Kernighan and Ritchie, The C Programming
Language (1978)
− De facto standard
• C89/C90
− ANSI standard X3.159-1989 (completed in 1988; formally
approved in December 1989)
− International standard ISO/IEC 9899:1990
• C99
− International standard ISO/IEC 9899:1999
− Incorporates changes from Amendment 1 (1995)
5
C-Based Languages
6
C Characteristics
• Properties of C
− Low-level, Small, Permissive (assumes you know what
you’re doing)
• Strengths of C
− Efficiency, Portability, Flexibility, Standard library, Integration
with UNIX
• Weaknesses of C
− Programs can be error-prone, difficult to understand, difficult
to modify
7
Effective Use of C
8
Chapter 2
C Fundamentals
Program: Printing a Pun
#include <stdio.h>
int main(void){
printf("To C, or not to C: that is the question.\n");
return 0;
}
10
The GCC Compiler
11
Directives
12
How the Preprocessor Works
13
Compiling and Linking
14
Functions
15
The Standard Library
16
The main Function
17
Statements
18
Printing Strings
• The statement
printf("To C, or not to C: that is the question.\n");
could be replaced by two calls of printf:
printf("To C, or not to C: ");
printf("that is the question.\n");
• The new-line character can appear more than once in a
string literal:
printf("Brevity is the soul of wit.\n --Shakespeare\n");
19
Comments
20
Variables and Assignment
21
Declarations
22
Declarations
23
Assignment
24
Initialization
25
Printing the Value of a Variable
26
Printing the Value of a Variable
27
Printing the Value of Many Variables
• There’s no limit to the number of variables that can be
printed by a single call of printf:
printf("Height: %d Length: %d\n", height, length);
28
Printing Expressions
29
Reading Input
30
Reading Input
31
Program: Converting from
Fahrenheit to Celsius
• The celsius.c program prompts the user to enter a
Fahrenheit temperature; it then prints the equivalent
Celsius temperature.
• Sample program output:
Enter Fahrenheit temperature: 212
Celsius equivalent: 100.0
• The program will allow temperatures that aren’t
integers.
32
celcius.c
34
Chapter 3
Formatted Input/Output
The printf Function
36
The printf Function
37
Escape Sequences
• The \n code that is used in format strings is called an
escape sequence.
• A string may contain any number of escape
sequences:
printf("Item\tUnit\tPurchase\n\tPrice\tDate\n");
• Executing this statement prints a two-line heading:
Item Unit Purchase
Price Date
• A partial list of escape sequences:
New line \n Backslash \\
Horizontal tab \t Double Quotation \”
38
The scanf Function
39
The scanf Function
40
How scanf Works
• As it searches for a number, scanf ignores white-space
characters (space, horizontal and vertical tab, form-feed, and
new-line).
• A call of scanf that reads four numbers:
scanf("%d%d%f%f", &i, &j, &x, &y);
• The numbers can be on one line or spread over several lines:
1
-20 .3
-4.0e3
• scanf sees a stream of characters (¤ represents new-line):
••1¤-20•••.3¤•••-4.0e3¤
ssrsrrrsssrrssssrrrrrr (s = skipped; r = read)
• scanf “peeks” at the final new-line without reading it.
41
Program: Adding Fractions
42
addfrac.c
#include <stdio.h>
int main(void)
{
int num1, denom1, num2, denom2, result_num, result_denom;
return 0;
}
43
Chapter 4
Expressions
Operators
45
Increment and Decrement Operators
• The increment and decrement operators are tricky:
− They can be used as prefix operators (++i and –-i) or
postfix operators (i++ and i--).
• Example 1:
i = 1;
printf("i is %d\n", ++i); /* prints "i is 2" */
printf("i is %d\n", i); /* prints "i is 2" */
• Example 2:
i = 1;
printf("i is %d\n", i++); /* prints "i is 1" */
printf("i is %d\n", i); /* prints "i is 2" */
46
Increment and Decrement Operators
• ++i means “increment i immediately,” while i++ means
“use the old value of i for now, but increment i later.”
• How much later? The C standard doesn’t specify a
precise time, but it’s safe to assume that i will be
incremented before the next statement is executed.
47
Operator Precedence
48
Expression Evaluation
49
Operator Associativity
50
Implementation-Defined Behavior
51
Order of Subexpression Evaluation
• Example:
i = 2;
j = i * i++;
• It’s natural to assume that j is assigned 4. However, j
could just as well be assigned 6 instead:
1. The second operand (the original value of i) is fetched, then
i is incremented.
2. The first operand (the new value of i) is fetched.
3. The new and old values of i are multiplied, yielding 6.
52
Undefined Behavior
53
Chapter 5
Selection Statements
Statements
55
Logical Expressions
56
Boolean Values in C89
57
Boolean Values in C99
58
The if Statement
• Example:
if (line_num == MAX_LINES)
line_num = 0;
59
Compound Statements
60
The else Clause
61
Cascaded if Statements
62
Example Cascaded if Statement
63
The switch Statement
64
The Role of the break Statement
65
The Role of the break Statement
66
Chapter 6
Loops
Iteration Statements
68
The while Statement
69
The do Statement
71
The for Statement
72
Infinite Loops
73
The Comma Operator
74
The break Statement
75
The break Statement
76
The break Statement
77
The continue Statement
78
The continue Statement
79
Chapter 7
Basic Type
Basic Types
81
Integer Type Specifiers
82
Integer Types
83
Integer Overflow
84
Integer Overflow
85
Floating Types
86
Floating Types
87
Character Sets
88
Character Types
ch = 'a'; /* lower-case a */
ch = 'A'; /* upper-case A */
ch = '0'; /* zero */
ch = ' '; /* space */
• Notice that character constants are enclosed in single
quotes, not double quotes.
89
Operations on Characters
90
ASCII Table (128 first characters)
91
Operations on Characters
i = 'a'; /* i is now 97 */
ch = 65; /* ch is now 'A' */
ch = ch + 1; /* ch is now 'B' */
ch++; /* ch is now 'C' */
92
Operations on Characters
93
Character-Handling Functions
94
Reading and Writing Characters
Using scanf and printf
• The %c conversion specification allows scanf and
printf to read and write single characters:
char ch;
scanf("%c", &ch); /* reads one character */
printf("%c", ch); /* writes one character */
95
Reading and Writing Characters
Using getchar and putchar
• For single-character input and output, getchar
and putchar are an alternative to scanf and
printf.
− putchar writes a character:
putchar(ch);
− getchar it reads one character, which it returns:
ch = getchar();
• Moving the call of getchar into the controlling
expression allows us to condense a loop that reads
many characters:
while ((ch = getchar()) != '\n')
;
96
Type Conversion
97
Type Conversion
98
The Usual Arithmetic Conversions
99
The Usual Arithmetic Conversions
100
Explicit Conversion: Casting
101
The sizeof Operator
102