1 / 44

CHAPTER 2

CHAPTER 2. FUNDAMENTALS OF C. LEARNING OUTCOMES. After studying this chapter, you should be able to: Describe what is data in C programs; Write variable declarations and initialization; Trace a program with multiple expressions; and Apply the fundamentals of C in a working program. .

kamali
Télécharger la présentation

CHAPTER 2

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 FUNDAMENTALS OF C

  2. LEARNING OUTCOMES After studying this chapter, you should be able to: • Describe what is data in C programs; • Write variable declarations and initialization; • Trace a program with multiple expressions; and • Apply the fundamentals of C in a working program.

  3. INTRODUCTION • As discussed in Chapter 1, as a programmer, you need to analyze what a programming problem requires first before you got to type out the programming codes. • In this chapter, you will first look at the basic structure of a C program. • Then, you will learn about the fundamentals of data types available in C followed by how to apply them in variables.

  4. STRUCTURE OF A C PROGRAM • Recall from Chapter 1, you have written a Hello.c program

  5. STRUCTURE OF A C PROGRAM • The structure includes: • preprocessor directives • global declarations • local definitions • statements Figure 2.1: Structure of a C Program

  6. STRUCTURE OF A C PROGRAM #include <stdio.h> #define PI 3.142 int a=5; int main() { int b=10; printf("Hello World"); system("pause"); return 0; } Calling standard functions from header files (.h) Declaring program elements outside main() program Defining program elements inside main() program Statements in main() program Will cause the black DOS window to be paused until you press any key (to avoid it immediately close not giving you the chance to read what is printed) { } indicates the body of main() program To mark the end of program

  7. DATA AND MEMORY CONCEPTS • Variable names such as num1, num2 and total actually correspond to locations in the computer’s memory. • Every variable has a name, a type and a value. • Example: scanf(“%d”, &num1); //read an integer from keyboard

  8. DATA AND MEMORY CONCEPTS • Whenever a value is placed in a memory location, the value replaces other value that the location previously had. int num1 = 20; scanf(“%d”, &num1); // assume user key in 42 • Hence the memory location for num1 will be:

  9. IDENTIFIERS • Identifiers are names that you give to data like variables, constants, functions, types and labels in your program. • Question: How many data can you spot from Example 2.1? • Answer: There are three units of data - area, width and length. • We know that we will be using the three data or better said them as identifiers/variables. Example 2.1 Mr. M needs to write a C program that will calculate the area of a rectangle. Each rectangle will have its own length and width. Mr. M will use the formula: Area = width * length

  10. VARIABLES DECLARATION • All data used in a computer must be stored in the computer’s memory. • Whenever a name of a data given, it is actually referring to where it is located in the computer’s memory. In C programming, it is called a variable. • Programmer must tell the computer the names of the variable and clearly define its data type. The term variable is used because the value stored in the variable is not fixed (can change).

  11. VARIABLES DECLARATION • A general form of writing a declaration statement is as follows: dataTypevariableName; • If a variable holds an integer value, and the name given is sum, the declaration statement will be like this: int sum; • You may also declare multiple variables in a single declaration. float mark1, mark2, mark3, sum, average;

  12. VARIABLES INITIALIZATION • Initialization lets the programmer to decide on the starting content of the storage location. • For example, the declaration statement: int total=20; declares that variable total is an integer type and sets the initial value of 20 into the variable. Initialization is the term used when the first time a value is stored in a variable, upon declaration.

  13. CONSTANTS • How about when you have to deal with fixed data values? For example, government determined tax rate. The tax rate will be used many times throughout a program. If the tax rate changes, programmer will be in a trouble to repair the program by changing all the values initialized in the program. This may cause problems or errors later on.

  14. CONSTANTS • The statement to define the constant is #define constantName value • It is a standard practice to use all capital letters to define a constant.

  15. BASIC GUIDELINES ON NAMING DATA • Actually, we do not really need to be very rigid in giving names to identifiers in our C program. • However, we still need to refer to some set of rules in providing names to identifiers.

  16. Table 2.1: Rules for Naming Identifiers

  17. EXERCISE 2.1

  18. DATA TYPES • Data type will determine what data can be stored in a certain storage location, number of bytes allocated to it and how the data operates. • Common Data Types in C: • Void (void) • Integer (int) • Floating points (float , double) • Character (char)

  19. Table 2.2: Common Data Types in C

  20. EXPRESSIONS IN C • Arithmetic • Precedence and Associativity • Implicit and Explicit Conversions • Unary Operators in Postfix & Prefix Expressions • Compound Expression

  21. ARITHMETIC IN C Table 2.3: Arithmetic Operators

  22. PRECEDENCE AND ASSOCIATIVITY • Precedence rule is used when we have different level of operators in C expressions. • The calculations of multiplication, division and modulo are evaluated first before addition and subtraction. • Associativity rule is used (evaluated either left-to-right or right-to-left) when we have multiple operators of the same precedence level.

  23. PRECEDENCE AND ASSOCIATIVITY • Observe the outputs:

  24. IMPLICIT AND EXPLICIT CONVERSIONS • We use implicit and explicit conversions when we have different data types in C expression. • One of the data types must be converted either to lower or higher level (promote or demote). Table 2.5: Conversion Rank of Data Types

  25. IMPLICIT AND EXPLICIT CONVERSIONS • To cast data from one data type to another, type the new data type in parentheses before the value to be converted.

  26. Unary Operators in Postfix & Prefix Expressions • If a variable m is incremented by 1, the increment operator ++ can be used: instead of writing it as m=m+1, we may write it as m++. m++ has the same effect as m=m+1

  27. POSTFIX EXPRESSIONS • In postfix expressions, the original value is being used and result will be modified later. Figure 2.4: Result of Postfix A++

  28. PREFIX EXPRESSIONS • In prefix expressions, the result will be modified immediately. Figure 2.5: Result of Prefix ++A

  29. Table 2.6: Postfix and Prefix Examples

  30. COMPOUND EXPRESSIONS • C provides several assignment operators for abbreviating assignment expressions. Table 2.7: Expansion of Compound Expressions

  31. EXERCISE 2.2

  32. MANAGING INPUT AND OUTPUT • The printf()statement is used to display an output. • Pass at least two items to printf(): • control message • data list (value to be displayed) • Items passed to a function are always placed within parentheses and are called arguments.

  33. MANAGING INPUT AND OUTPUT • Conversion control sequence: • Has a special meaning to tell the function what type of values is to be displayed and how to display them. • Also referred to as conversion specification or format specifiers. Table 2.8: List of Conversion Control Sequence

  34. MANAGING INPUT AND OUTPUT • Minimum field width can be specified to the data type that we are going to display.

  35. MANAGING INPUT AND OUTPUT • Precision specification • If you do not specify its value, by default the output for floating-point numbers will be printed with six decimal places. printf(“%f”, 1.5); Output: 1.500000 printf(“%.2f”, 1.5); Output: 1.50

  36. MANAGING INPUT AND OUTPUT • The scanf() statement • Allows the user to enter a value. • Then value then stored directly in the variable. • Requires a control string as the first argument. The control string will tell functions the type of data being input.

  37. MANAGING INPUT AND OUTPUT • A sample statement that uses scanf() function is: scanf(“%d”, &mark); • Note that, when using scanf(), the address operator ‘&’ must be used for all data type except string. Hence an input to a string will be handled as: scanf(“%s”, name);

  38. MANAGING INPUT AND OUTPUT

  39. COMMENTS • To describe your codes or to make it easier for other programmers in understanding your codes later. • You can put comments anywhere in your programs (make sure they are meaningful). • Use /* */ or // symbols.

  40. ESCAPE SEQUENCES • A combination of backslash (\) and a specific character will result in different effects to the program. • Normal actions by the character will be ignored once backslash is used.

  41. EXERCISE 2.3

  42. SUMMARY • At this point, we have learned the Fundamentals of C Programming. The following are discussed earlier in this chapter: • Basic data types and how to name variables and constants; • Arithmetic Operations in C; • Evaluations of C expressions; • Formatting Input & Output for a C program.

  43. PROGRAMMING PROBLEMS

More Related