Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }

Similar presentations


Presentation on theme: "Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }"— Presentation transcript:

1 Chapter 2 : Overview of C By Suraya Alias

2 /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }

3  /*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

4  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  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  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 1-7

8

9

10

11  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 1-12

13 PlaceholderVariable typeFunction used %cCharprintf/scanf %dIntprintf/scanf %fDoublePrintf %lfDoublescanf 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 1-14

15 1-15

16 Arithmetic 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 1 Rules for evaluating Expression 1)Parentheses rules 1)Solve or evaluate expression in bracket first 2)Operator Precedence Rules 1)Unary + - 2)* / % 3)Binary + - 3)Associativity Rules 1)For same level, left to right

17

18 1-18

19 1-19

20 1-20

21 1-21

22 1-22

23 1-23

24  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  Interactive mode A mode where user responds to prompt by entering data  Batch mode Programs scans data from data file

26 1-26

27  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 1-28

29 1-29

30 1-30

31 1-31

32 1-32


Download ppt "Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }"

Similar presentations


Ads by Google