1 / 32

Chapter 2: Overview of C

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.

Download Presentation

Chapter 2: Overview of C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2:Overview of C Problem Solving & Program Design in C Sixth Edition By Jeri R. Hanly & Elliot B. Koffman

  2. Figure 2.1 C Language Elements in Miles-to-Kilometers Conversion Program

  3. 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>

  4. 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;

  5. 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.

  6. 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.

  7. 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.

  8. 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;

  9. Figure 2.2 Memory(a) Before and (b) After Execution of a Program

  10. 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

  11. Figure 2.3 Effect of kms = KMS_PER_MILE * miles;

  12. Figure 2.4 Effect of sum = sum + item;

  13. Figure 2.5 Effect of scanf("%lf", &miles);

  14. 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.

  15. Placeholder • A symbol beginning with % in a format string that indicates where to display the output value.

  16. 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.

  17. 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.

  18. Figure 2.6 Scanning Data Line Bob

  19. Figure 2.7 General Form of a C Program

  20. 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: , ; { }

  21. Figure 2.8 Evaluation Tree for area = PI * radius * radius;

  22. Figure 2.9 Step-by-Step Expression Evaluation

  23. Figure 2.10 Evaluation Tree and Evaluation for v = (p2 - p1) / (t2 - t1);

  24. Figure 2.11 Evaluation Tree and Evaluation for z - (a + b / 2) + w * -y

  25. Figure 2.12 Supermarket Coin Value Program

  26. Figure 2.12 Supermarket Coin Value Program (cont’d)

  27. Figure 2.13 Batch Version of Miles-to-Kilometers Conversion Program

  28. Figure 2.14 Miles-to-Kilometers Conversion Program with Named Files

  29. Figure 2.15 Compiler Listing of a Program with Syntax Errors

  30. Figure 2.16 A Program with a Run-Time Error

  31. Figure 2.17 Revised Start of main Function for Supermarket Coin Value Program

  32. Figure 2.18 A Program That Produces Incorrect Results Due to & Omission

More Related