Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture3.

Similar presentations


Presentation on theme: "Lecture3."— Presentation transcript:

1 Lecture3

2 Lecture Outline Program Layout Executable Statements
Input/Output Statements Input/Output Operation scanf function printf function Assignment Statements Arithmetic Operations The Return Statement Comments in Programs A Complete C Program Mixed Data Types in Assignment Statements Expressions with multiple operators Rules for Expression Evaluation Writing mathematical Formulas in C Types of Errors

3 Program Layout provides an instruction to the preprocessor.
Preprocessor Directive int main(void) { Declaration Executable Statements } A C program line beginning with # that provides an instruction to the preprocessor. Examples: #include, #define. Function main indicates the start of the program. The part of a program that tells the compiler the names of memory cells and their data types. Program commands that are converted to machine code by the compiler.

4 Executable Statements
Getting information in and out of a program. Input/Output Statements (functions) Doing calculations. Assignment Statements

5 Input/Output Operations
Input Operation An instruction that copies data from an input device into memory. Output Operation An instruction that displays information stored in memory.

6 Input/Output Functions
Input Functions Perform input operations. Example: scanf. Output Functions Perform output operations. Example: printf.

7 scanf The scanf function gets data entered by the user and copies it into memory. Syntax scanf(format string, input list); Example scanf("%c%i", &firstinitial, &age); H32 placeholder

8 Placeholder A placeholder is a symbol beginning with % in a format string that indicates where to display the output value. Placeholder Variable Type Function Use %c char printf/scanf %i or %d int printf/scanf %f float printf/scanf

9 Examples scanf("%i",&workhours); scanf("%c",&letter);
scanf("%i",&student_ID); scanf("%f",&tot_score); scanf("%f",&temperature); scanf("%f",&working_hours); scanf("%i%c",&population,&firstinitial);

10 printf The printf function displays information on the screen. Syntax
printf(format string); printf(format string, print list); Note: a format string is always enclosed in " " placeholder newline escape sequence Example printf("That equals %fKilometers.\n",kms); function name format string print list

11 New Line Escape Sequence "\n"
Is the newline escape sequence symbol which is placed in a printf to skip to a new line.

12 Examples printf("Please enter the student's grades:");
printf("The class average is %f ",average); The class average is 95.5. printf("The total area is %f and the total cost is %i S.R.",tarea,tcost); The total area is 60.2 and the total cost is 4530 S.R. printf("The student received an %c grade in the course.",grade); The student received an A grade in the course.

13 Examples (Continue) printf("The grade is %c%c", grade, gradesymb);
The grade is A+ printf("I am the first line\n"); printf("\n I am the second line\n"); I am the first line I am the second line

14 Formatting Numbers in Program Output
Value Format Displayed Output %4i %5i %1i %4i %5i %2i

15 Formatting Numbers in Program Output
Value Format Displayed Output %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f

16 Formatting Numbers in Program Output
Value Format Displayed Output %5.2f %3.2f %4.2f %.3f %4.2f %.4f

17 Assignment Statements
It stores a value or a computational result in a variable. It is used to perform most arithmetic operations in a program. Variable=expression; Syntax Examples Total_Grades = Grade1 + Grade2 + Grade3; No_Students = 3; Average = Total_Grades / No_Students;

18 Arithmetic Expressions
Arithmetic Meaning Operator addition subtraction * multiplication / division % remainder

19 The % Operation (Remainder)
Examples 3 % 5 = 3 4 % 5 = 4 5 % 5 = 0 6 % 5 = 1 7 % 5 = 2 8 % 5 =3 5 % 3 = 2 5 % 4 = 1 15 % 5 = 0 15 % 6 = 3

20 C Language Elements #include <stdio.h>
#define KMS_PER_MILE 1.609 int main(void) { float miles, kms; printf("Enter the distance in miles: "); scanf("%f", &miles); kms = KMS_PER_MILE * miles; printf("That equals %.2f kilometers.\n", kms); return(0); }

21 The Return statement The return statement transfers control from a function back to the activator of the function. For function main, control is transferred back to the operating system The value of expression is returned as the result of the function execution. Syntax return expression; Example return(0);

22 Comments in programs Syntax /* comment text*/ Examples
/* This is a one line comment */ /* * This is a multiple line comment in which the stars not immediately * proceeded or followed by slashes have no special syntactic * significance */

23 A Complete C Program /* converts distances from miles to kilometers */
#include <stdio.h> /* printf, scanf definitions */ #define KMS_PER_MILE /* conversion constant */ int main(void) { float miles, /* distance in miles */ kms; /* equivalent distance in kilometers */ /* get the distance in miles */ printf("Enter the distance in miles: "); scanf("%f", &miles); /* convert the distance to kilometers */ kms = KMS_PER_MILE * miles; /* display the distance in kilometers */ printf("That equals %.2f kilometers.\n", kms); return(0); }

24 Mixed Data Types in an Assignment Statement
The variable to the left of the equals sign determines the data types of an expression. Example: if x is int and y is float then x = 7 * 0.5; 3 y = 7 * 0.5; 3.50 x = 100/5; 20 y = 100/5; 20.00

25 Expressions with multiple operators
Operators with multiple operators are common in C. Example: x = b * b - 4 * a * c These expressions can include: Unary operators: Take only one operand. Example: x = -y; p = +x * y Binary operators: Requires two operands. Example: x = y + 2;

26 Rules for Expression Evaluation
Rule # 1: Parentheses rule All expressions in parentheses must be evaluated separately. Nested parenthesized expressions must be evaluated from the inside out. Example: ( ( a + b ) / ( c + d ) ) * 2

27 Rules for Expression Evaluation
Rule # 2: Operator precedence rule Operators in the same expression are evaluated in the following order: unary + , - first * , / , % next binary + , - last Example: a + b * c

28 Rules for Expression Evaluation
Rule # 3: Associativity rule Right associativity: unary operator in the same sub-expression and at the same level (such as + and -) are evaluated right to left. Example: a + b - c Left associativity: Binary operator in the same sub-expression and at the same level (such as + and -) are evaluated left to right.

29 Rules for Expression Evaluation (Examples)
z - ( a + b / 2 ) + w * - y ( b * b ) - ( 4 * 4 ) * ( a * c ) A * - ( b + c )

30 Writing Mathematical Formulas in C
Always specify multiplication explicitly by using the operator * where needed. Example: b² - 4 a c = b * b - 4 * a * c Use parentheses when required to control the order of operator evaluation. Example: a + b = ( a + b ) / ( c + d ) c + d Two arithmetic operators can be written in succession if the second is a unary operator Example: a x - ( b + c ) = a * - ( b + c )

31 Writing Mathematical Formulas in C (Examples)
a + b - c = a + b - c 1 1 + a² = 1 / ( 1 + a * a) a x (5b + c) = a * ( 5 * b + c )

32 Types of Errors Syntax error: a violation of the C grammar rules. Detected during compilation. Logic error: an error caused by following an incorrect algorithm. Run-time Error: an attempt to perform an invalid operation, detected during program execution. Example: dividing a number by zero.


Download ppt "Lecture3."

Similar presentations


Ads by Google