1 / 32

Fundamentals of C Programming: Lecture Outline and Examples

Learn the basics of C programming with topics like program layout, executable and input/output statements, arithmetic operations, and error types. Master scanf and printf functions, arithmetic expressions, and return statements in C programming.

imcleod
Download Presentation

Fundamentals of C Programming: Lecture Outline and Examples

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. 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 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. Input/Output Statements (functions) • Getting information in and out of a program. Assignment Statements • Doing calculations. Executable Statements

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

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

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

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

  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. 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 printf • The printf function displays information on the screen.

  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:"); Please enter the student's grades: printf("The class average is %f",average); The class average is95.5. printf("The total area is %f and the total cost is %i S.R.",tarea,tcost); The total area is60.2and the total cost is4530S.R. printf("The student received an %c grade in the course.",grade); The student received anAgrade in the course.

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

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

  15. .123 %6.2f 0.12 -9.536 %6.2f -9.54 Formatting Numbers in Program Output Value Format Displayed Output -99.42 %6.2f -99.42 -25.554 %6.2f -25.55 99.999 %6.2f 100.00 999.4 %6.2f 999.40

  16. 3.14159 %5.2f 3.14 Formatting Numbers in Program Output Value Format Displayed Output 3.14159 %3.2f 3.14 .1234 %4.2f 0.12 -.006 %.3f -0.006 -.006 %4.2f -0.01 -3.14159 %.4f -3.1416

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

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

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

  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. Syntax return expression; Example return(0); 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.

  22. 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 */ Comments in programs

  23. A Complete C Program /* converts distances from miles to kilometers */ #include <stdio.h> /* printf, scanf definitions */ #define KMS_PER_MILE 1.609 /* 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. 3 3.50 20 20.00 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; y = 7 * 0.5; x = 100/5; y = 100/5;

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

  26. 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 Rules for Expression Evaluation

  27. 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 Rules for Expression Evaluation

  28. 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. • Left associativity: Binary operator in the same sub-expression and at the same level (such as + and -) are evaluated left to right. • Example: a + b - c Rules for Expression Evaluation

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

  30. Use parentheses when required to control the order of operator evaluation. Example: a + b = ( a + b ) / ( c + d ) c + d 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 • Two arithmetic operators can be written in succession if the second is a unary operator Example: a x - ( b + c ) = a * - ( b + c )

  31. 1 1 + a² Writing Mathematical Formulas in C (Examples) Examples: a + b - c = a + b - c = 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.

More Related