320 likes | 452 Vues
This overview elaborates on fundamental elements of the C programming language involved in problem solving and program design, drawn from "Problem Solving and Program Design in C" by Hanly and Koffman. It explains key concepts such as preprocessors, directives, libraries, identifiers, variables, and data types. Additionally, it highlights input/output operations using functions like `scanf` and `printf`, alongside variable assignment and declaration, illustrating their roles in efficient program design and coding practices in C.
E N D
Chapter 2:Overview of C Problem Solving & Program Design in C Sixth Edition By Jeri R. Hanly & Elliot B. Koffman
Figure 2.1 C Language Elements in Miles-to-Kilometers Conversion Program
Preprocessor and Library • Preprocessor: A system program that modifies a C program before the compilation. • Preprocessor Directive: A C program line that provides an instruction to the preprocessor • Library: A collection of useful functions and symbols that may be accessed by a program. #include <stdio.h>
Constant Macro A name that is replaced by a particular constant value before the program is sent to the compiler. #define KMS_PER_MILE 1.609 Each time the compiler sees KMS_PER_MILE, it will replace it by 1.609. For example: kms = KMS_PER_MILE * miles; would read: kms = 1.609 * miles;
Commenting • Those are for the users to remember or understand easier. They are ignored by the preprocessor and the compiler. They provide supplementary information. /* This is a comment */ // This is a comment, as well.
Identifiers • Reserved Words Words with special meaning in C; e.g. double. They cannot be used for any other purpose. • Identifiers • Standard Still special words but they can be changed by the programmer; e.g. printf • User-Defined Words to name the memory cell; e.g. KMS_PER_MILE Remark: Names are case sensitive. i.e. area and Area would be two different identifiers.
Restrictions • Identifiers must consist of only letters, digits and underscores. • Cannot begin with a digit. • Reserved words cannot be used. • Some compilers take the first 31 characters; so the rest is not important. e.g. my book, 16door, double, ali’sbook ….are invalid. Recommendation: Use capital letters in constant macros and give meaningful names.
Variables and Data Types • Variable: A name associated with a memory cell whose value can change. • Data Type: A set of values and operations that can be performed on those values. int for an integer number (-32767 to 32767) double for a real number char for an individual character value Example of a variable declaration: double kms;
Figure 2.2 Memory(a) Before and (b) After Execution of a Program
Assignment Statements = is read as “gets”, “becomes”, “assigns”, “takes the value of”. variable = expression; x = y + z; x = x + 1; Previous value is overwritten by the new value
Input/Output Operations and Functions • Data can be stored in the memory in two ways: • By assignment • By copying data from an input device into a variable. For example: scanf function • An instruction that displays information stored in the memory is an output operation; e.g. to output data to the screen printf function is used.
Placeholder • A symbol beginning with % in a format string that indicates where to display the output value.
printf() printf(“Your point is %f out of 100 \n”, grd); Printf: Function name grd: Print list i.e. variables or expressions whose values are displayed. Whole in () is the function argument. \n: Terminate the line. %f: placeholder that indicated where to display the output value.
scanf() scanf(format string, input list); scanf(“%lf”, &miles) means we will have 30.5 given to “miles” if number entered is 30.5. e.g. scanf(“%c%c%c”, &letter_1, &letter_2, &letter_3); Will take first letter copied to letter_1, second to letter_2 and third one to letter_3 in the entered order.
Function Body • Two parts: • Declarations Tell the compiler what memory cells are needed in the function • Executable Statements First translated into machine language then executed Punctuation: , ; { }
Figure 2.10 Evaluation Tree and Evaluation for v = (p2 - p1) / (t2 - t1);
Figure 2.11 Evaluation Tree and Evaluation for z - (a + b / 2) + w * -y
Figure 2.13 Batch Version of Miles-to-Kilometers Conversion Program
Figure 2.14 Miles-to-Kilometers Conversion Program with Named Files
Figure 2.15 Compiler Listing of a Program with Syntax Errors
Figure 2.17 Revised Start of main Function for Supermarket Coin Value Program
Figure 2.18 A Program That Produces Incorrect Results Due to & Omission