1 / 32

KK10103 – Computer Programming

KK10103 – Computer Programming. Chapter 2 : Overview of C By Suraya Alias. 2.0 The Hello World Program. /*The classic HelloWorld */ #include < stdio.h > int main(void) { printf (“Hello World!!"); return 0; }. 2.1 C Language Element. /*The classic HelloWorld */

neveah
Download Presentation

KK10103 – Computer Programming

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. KK10103 – Computer Programming Chapter 2 : Overview of C By Suraya Alias

  2. 2.0 The Hello World Program /*The classic HelloWorld */ #include <stdio.h> int main(void) { printf(“Hello World!!"); return 0; }

  3. 2.1 C Language Element • /*The classic HelloWorld */ • These are comments, starts with /* ends with */, // is used to comment out per line. • #include <stdio.h> • Lines begin with # is the preprocessing directives which communicates with the processor. • #include <stdio.h> means to include standard header file that has definitions such as printf and scanf. • The other preprocessor directive is #define, such as #define HOUR 60 , which means that the constant macro HOUR has the value of 60. • Syntax : #define NAME value

  4. 2.1 C Language Element • int main (void) • The main function is where the execution begins. • The braces { } enclose the main function body, which contains declaration and executable statements. • The keyword int indicates that the main function returns an integer value 0 after the execution ends. • The keyword void means the main function takes no argument • The function body has two parts : declaration and executable statements. • Every declarations and statements ends with semicolon ; in C programming

  5. 2.1 C Language Element • Reserved words • a word that has special meaning in C • Such as int, void, double and return • Standard Identifiers • also has special meaning such as printf and scanf • User Defined Identifiers • user can define their own identifiers such as HOUR, KM_PER_MILE • An identifier must consist only letters, digit and underscore • An identifier cannot begin with a digit • A C reserved word cannot be used as an identifier • Lowercase and uppercase identifiers does make a different in C. The names Rate and rate and RATE is considered as different identifiers.

  6. 2.2 Variable Declaration and Data Types • Variables • a memory cell used to store value and can be changed as the programs executes • Variable Declaration • statement that tells the compiler the variable name and the value stored in the variable. • The Variable declarations starts with an identifier followed by the variable name such as : int count; double x, y, x; char w; • Data types • a set of values and operations that can be performed on those values • Data type int – to represent integers in C, such as 77 • Data type double – a real number that is separated by decimal point, such as 1.5674 and 0.09 • Data type char – represents an individual character value such as a letter, digit or special symbol. Such as ‘A’, ‘7’, ‘:’

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

  8. Figure 2.2 Memory(a) Before and (b) After Execution of a Program 2.3 Executable Statements

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

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

  11. Input / Output Operations • The printf function • Used for printing formatted output and prompting message • printf(“Please enter a number>”) • printf(“That equals %f kilometers.\n ”, kms); • That equals 16.090000 kilometers • %f is the placeholder which is replaced by the value from kms • A placeholder always begins with the symbol % • The scanf function • Used to read formatted input • scanf(“%lf”, &miles); • The format “%lf” is the placeholder that tells scanf what kind of data to copy to variable miles • The name of each variable is preceded by & character

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

  13. Placeholder in Format Strings • Multiple placeholder • printf (“Hi %c%c%c – you will be %d years old today \n”, letter_1, letter_2, letter_3, age); • Will display as • Hi BOB – you will be 32 years old today

  14. Figure 2.6 Scanning Data Line Bob

  15. Figure 2.7 General Form of a C Program

  16. 2.5 Arithmetic Expression • Rules for evaluating Expression • Parentheses rules • Solve or evaluate expression in bracket first • Operator Precedence Rules • Unary + - • * / % • Binary + - • Associativity Rules • For same level, left to right

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

  18. Figure 2.9 Step-by-Step Expression Evaluation

  19. Figure 2.10 Evaluation Tree and Evaluation forv = (p2 - p1) / (t2 - t1);

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

  21. Figure 2.12 Supermarket Coin Value Program

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

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

  24. 2.6 Formatting Numbers in Program Output • Formatting values of Type int • By using field width • printf(“Results: %3d meters = %4d ft. %2d in. \n”, meters, feet, inches); • Results : 21 meters= 68 ft. 11 in. • Formatting values of Type double • To display the value of x to an accuracy of 2 decimal place we can use the placeholder %6.2f • Example : x is -25.554 will be displayed as -25.55

  25. 2.7 Interactive Mode, Batch Mode and Data Files • Interactive mode • A mode where user responds to prompt by entering data • Batch mode • Programs scans data from data file

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

  27. Program controlled Input and output files Using the FILE * command FILE *inp, /* pointer to input file */ *outp; /* pointer to output file */ /* Open the input and output files. */ inp = fopen("D:\\distance.txt", "r"); outp = fopen("D:\\distanceout.txt", "w"); /* Get and echo the distance in miles. */ fscanf(inp, "%lf", &miles); fprintf(outp, "The distance in miles is %.2f.\n", miles); /* Display the distance in kilometers. */ fprintf(outp, "That equals %.2f kilometers.\n", kms); /* Close files. */ fclose(inp); fclose(outp);

  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