1 / 36

Introducing C

Introducing C. Lecture 2. Lecture overview. Operator : = Functions: main (), printf () Putting together a simple C program Creating integer-valued variables, assigning them values, and displaying those values onscreen The newline character ‘<br>’

lita
Download Presentation

Introducing C

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. Introducing C Lecture 2

  2. Lecture overview Operator: = Functions: main(), printf() Putting together a simple C program Creating integer-valued variables, assigning them values, and displaying those values onscreen The newline character ‘\n’ How to include comments in your programs, create programs containing more than one function, and find program errors. Debugging

  3. Program is created using the Editor and stored on Disk. Editor (Source) Phase 1 : Disk Preprocessor program processes the code. Preprocessor Phase 2 : Disk Compiler creates object code and stores it on Disk. Compiler Phase 3 : Disk Linker Phase 4 : Disk Linker links object code with libraries, creates a.out and stores it on Disk C Development Environment

  4. Binary code generation Source file Preprocessed Source File Object File Executable File .c .exe (Windows) Preprocessing Compile Link Preprocessor Compiler Linker Header File Library .h .lib (Windows)

  5. Primary Memory Loader Phase 5 : Loader puts Program in Memory : . Primary Memory C P U (execute) Phase 6 : CPU takes each instruction and executes it, storing new data values as the program executes. : . Execution processes

  6. A Simple Example of C I am a simple computer. My favorite number is 1 because it is first.

  7. #include Directives and Header Files The effect of #include <stdio.h> is the same as if you had typed the entire contents of the stdio.h file into your file at the point where the #include line appears.

  8. The main() Function A C program begins execution with the function called main(). The intis the main() function's return type. That means that the kind of value main() can return is an integer.

  9. The main() Function • Example of main function declaration intmain(void) { return 0; } void main(void) { } main(void) { } main( ) { } • 'main' is a C keyword. We must not use it for any other variable

  10. Comments comment The parts of the program enclosed in the /* */ symbols are comments. Using comments makes it easier for someone (including yourself) to understand your program. // Here is a comment confined to one line. intrigue; // Such comments can go here, too.

  11. Braces, Bodies, and Blocks there must be just as many opening braces as closing braces In general, all C functions use braces to mark the beginning as well as the end of the body of a function. Their presence is mandatory!Only braces ({ }) work for this purpose, notparentheses (( )) and not brackets ([ ]).

  12. Declarations • The word intis a C keyword identifying one of the basic C data types • The word doors in this example is an identifier—that is, a name you select for a variable, a function, or some other entity. • Each statement in C needs to be terminated with semicolon (;) • This line declares two things. • Somewhere in the function, you have a variable called doors. • The intproclaims doors as an integer—that is, a number without a decimal point or fractional part.

  13. Data types • C deals with several kinds (or types) of data: • integers, • characters, • and floating point, etc. Declaring a variable to be an integer or a character type makes it possible for the computer to store, fetch, and interpret the data properly.

  14. Four Good Reasons to Declare Variables One RADIUS1 = 20.4; CIRCUM = 6.28 * RADIUSl; letter l • Putting all the variables in one place makes it easier for a reader to grasp what the program is about. • Thinking about which variables to declare encourages you to do some planning before plunging into writing a program. • What information does the program need to get started? • What exactly do I want the program to produce as output? • What is the best way to represent the data? • Declaring variables helps prevent one of programming's more subtle and hard-to-find bugs—that of the misspelled variable name.

  15. Assignment Assign the value 1 to the variable num: num= 1; Earlier intnum;line set aside space in computer memory for the variable num, and the assignment line stores a value in that location.

  16. The printf() Function Arguments printf("I am a simple "); printf("computer.\n"); printf("My favorite number is %d because it is first.\n", num); The parentheses () signify that printf is a function name. The material enclosed in the parentheses is information passed from the main() function to the printf() function.

  17. The printf() Function The \n symbol means to start a new line the same function as pressing the Enter key of a typical keyboard. • printf("computer.\n");

  18. Return Statement return0; This return statement is the final statement of the program. The intin intmain(void) means that the main() function is supposed to return an integer.

  19. The Structure of a Simple Program preprocessor instructions function name with arguments declaration statement assignment statement function statements

  20. Tips on Making Your Programs Readable • The following is correct, but ugly, code: • Good code: • A readable program is much easier to understand, and that makes it easier to correct or modify. • Choose meaningful variable names; • Use comments; • Use blank lines to separate one conceptual section of a function from another.

  21. Multiple functions I will summon the butler function. You rang, sir? Yes. Bring me some tea and writeable CD-ROMS.

  22. C Tokens • In a passage of text, individual words and punctuation marks are called tokens. C has six types of tokens.

  23. Keywords • Every C word is classified as either a keyword or an identifier. • All keywords have fixed meanings

  24. Rules of identifiers • Identifiers refer to the names of variables, functions and arrays. These are user-defined names. • First character must be an alphabet (or underscore) • Must consist of only letters, digits or underscore. • Only first 31 characters are significant. • Cannot use a keyword. • Must not contain white space. • You should use meaningful names for variables • (such ascar_countinstead of x3 if your program counts sheep.). • The characters at your disposal are lowercase letters, uppercase letters, digits, and the underscore (_). The first character must be a letter or an underscore.

  25. Rules of identifiers

  26. Constants • Constants in C refer to fixed values that do not change during the execution of a program.

  27. Three types of errors: Syntax • Syntax Errors - aviolation of the C grammar rules, detected during program translation (compilation). • You make a syntax error when you don’t follow C’s rules. It is similar to a grammatical error in English. • Example: Sing to likes he He likes to sing

  28. Three types of errors: Run-time • Run-time error - an attempt to perform an invalid operation, detected during program execution. • Occurs when the program directs the computer to perform an illegal operation, such as dividing a number by zero. • The computer will stop executing the program, and displays a diagnostic message indicates the line where the error was detected

  29. Three types of errors: Semantic • Semantic Errors and Logic errors are errors in meaning and design • Consider “The flying box thinks greenly.” The syntax is fine, but sentence does not mean anything. • In C the program following syntactically correct but wrong algorithm. • Very difficult to detect - it does not cause run-time error and does not display message errors. • The only sign of logic error – incorrect program output. • Can be detected by testing the program thoroughly, comparing its output to calculated results • To prevent – carefully desk checking the algorithm and written program before you actually type it

  30. Introducing Debugging Find errors!

  31. Introducing Debugging Should be { Missing closing */ Missing ; Should be }

  32. Introducing Debugging • Is the program syntactically correct? • What about semantics? • Will program operate as you expect?

  33. Introducing Debugging • Is the program syntactically correct? • What about semantics? • Will program operate as you expect? n = 5, n squared = 25, n cubed = 625

  34. Introducing Debugging Output

  35. Introducing Debugging • By tracing the program step-by-step manually (on the paper), keeping track of variable you monitor the program state. • Add extra printf() statements throughout to monitor the values of selected variables at key points in the program. • Use debugger, that is a program that runs another program step-by-step and examine the value of the program’s variable.

  36. QUIZ • Find errors

More Related