KK10103 – Computer Programming

32
KK10103 – Computer Programming Chapter 2 : Overview of C By Suraya Alias

description

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 */ - PowerPoint PPT Presentation

Transcript of KK10103 – Computer Programming

KK10103 Computer Programming

KK10103 Computer ProgrammingChapter 2 : Overview of C

By Suraya Alias2.0 The Hello World Program/*The classic HelloWorld */

#include

int main(void){ printf(Hello World!!"); return 0;}2.1 C Language Element/*The classic HelloWorld */These are comments, starts with /* ends with */, // is used to comment out per line.

#include Lines begin with # is the preprocessing directives which communicates with the processor.#include 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

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 argumentThe function body has two parts : declaration and executable statements.Every declarations and statements ends with semicolon ; in C programming2.1 C Language ElementReserved wordsa word that has special meaning in CSuch as int, void, double and returnStandard Identifiers also has special meaning such as printf and scanfUser Defined Identifiersuser can define their own identifiers such as HOUR, KM_PER_MILEAn identifier must consist only letters, digit and underscoreAn identifier cannot begin with a digitA C reserved word cannot be used as an identifierLowercase and uppercase identifiers does make a different in C. The names Rate and rate and RATE is considered as different identifiers.

2.1 C Language Element2.2 Variable Declaration and Data Types Variables a memory cell used to store value and can be changed as the programs executesVariable 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 valuesData type int to represent integers in C, such as 77Data type double a real number that is separated by decimal point, such as 1.5674 and 0.09Data type char represents an individual character value such as a letter, digit or special symbol. Such as A, 7, :1-7

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

Figure 2.2 Memory(a) Before and (b) After Execution of a Program2.3 Executable Statements

Figure 2.3 Effect of kms = KMS_PER_MILE * miles;

Figure 2.4 Effect of sum = sum + item;Input / Output OperationsThe printf functionUsed for printing formatted output and prompting messageprintf(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 functionUsed to read formatted inputscanf(%lf, &miles);The format %lf is the placeholder that tells scanf what kind of data to copy to variable milesThe name of each variable is preceded by & character

1-12

Figure 2.5 Effect of scanf("%lf", &miles);Placeholder in Format StringsPlaceholderVariable typeFunction used%cCharprintf/scanf%dIntprintf/scanf%fDoublePrintf%lfDoublescanfMultiple placeholder

printf (Hi %c%c%c you will be %d years old today \n, letter_1, letter_2, letter_3, age); Will display asHi BOB you will be 32 years old today

1-14

Figure 2.6 Scanning Data Line Bob1-15

Figure 2.7 General Form of a C Program2.5 Arithmetic ExpressionArithmetic OperatorMeaningExamples+Addition5 + 2 is 7 , 5.0 + 2.0=7.0-Subtraction5-2 is 3, 5.0-3.0 is 2.0*Multiplication5*2 is 10, 5.0*2.0 is 10.0/Division5/2 is 2, 5.0/2.0 is 2.5%remainder5%2 is 1Rules for evaluating Expression

Parentheses rulesSolve or evaluate expression in bracket firstOperator Precedence RulesUnary + - * / % Binary + -Associativity RulesFor same level, left to right

Figure 2.8 Evaluation Tree for area = PI * radius * radius;1-18

Figure 2.9 Step-by-Step Expression Evaluation1-19

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

1-20

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

1-21Figure 2.12 Supermarket Coin Value Program

1-22Figure 2.12 Supermarket Coin Value Program (contd)

1-23

Figure 2.13 Batch Version of Miles-to-Kilometers Conversion Program2.6 Formatting Numbers in Program Output Formatting values of Type intBy using field widthprintf(Results: %3d meters = %4d ft. %2d in. \n, meters, feet, inches);Results : 21 meters= 68 ft. 11 in.

Formatting values of Type doubleTo display the value of x to an accuracy of 2 decimal place we can use the placeholder %6.2fExample : x is -25.554 will be displayed as -25.55 2.7 Interactive Mode, Batch Mode and Data FilesInteractive mode A mode where user responds to prompt by entering data

Batch modePrograms scans data from data file 1-26

Figure 2.13 Batch Version of Miles-to-Kilometers Conversion Program26Program controlled Input and output filesUsing 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);

1-28

Figure 2.14 Miles-to-Kilometers Conversion Program with Named Files1-29

Figure 2.15 Compiler Listing of a Program with Syntax Errors1-30

Figure 2.16 A Program with a Run-Time Error1-31Figure 2.17 Revised Start of main Function for Supermarket Coin Value Program

1-32

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