Download presentation
Presentation is loading. Please wait.
Published byMorris Gibbs Modified over 9 years ago
1
Lecture 2 Introducing C
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
C Development Environment Disk Phase 2 : Preprocessor program processes the code. Disk Compiler Phase 3 : Compiler creates object code and stores it on Disk. Preprocessor Disk Linker Phase 4 : Editor (Source) Phase 1 : Program is created using the Editor and stored on Disk. Disk Linker links object code with libraries, creates a.out and stores it on Disk
4
Binary code generation Source file Preprocessed Source File Object File Executable File Preprocessing Compile Link PreprocessorCompilerLinker Header File Library.exe (Windows).lib (Windows).h.c
5
Execution processes Loader Phase 5 : :.:. Primary Memory Loader puts Program in Memory C P U (execute) Phase 6 : :.:. Primary Memory CPU takes each instruction and executes it, storing new data values as the program executes.
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 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 int is 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 int main(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 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. int rigue; // Such comments can go here, too. comment
11
Braces, Bodies, and Blocks 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, not parentheses (( )) and not brackets ([ ]). there must be just as many opening braces as closing braces
12
Declarations This line declares two things. Somewhere in the function, you have a variable called doors. The int proclaims doors as an integer— that is, a number without a decimal point or fractional part. The word int is 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 (;)
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 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. RADIUS1 = 20.4; CIRCUM = 6.28 * RADIUSl; One letter l
15
Assignment Assign the value 1 to the variable num: num = 1; Earlier int num; 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 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. Arguments
17
The printf() Function printf("computer.\n"); The \n symbol means to start a new line the same function as pressing the Enter key of a typical keyboard.
18
Return Statement return 0; This return statement is the final statement of the program. The int in int main(void) means that the main() function is supposed to return an integer.
19
The Structure of a Simple Program preprocessor instructions declaration statement function name with arguments assignment statement function statements
20
Tips on Making Your Programs Readable A readable program is much easier to understand, and that makes it easier to correct or modify. 1. Choose meaningful variable names; 2. Use comments; 3. Use blank lines to separate one conceptual section of a function from another. The following is correct, but ugly, code:Good code:
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 autodogotosignedunsigned breakdoubleifsizeofvoid caseelseintstaticvolatile charenumlongstructwhile constexternregisterswitch continuefloatreturntypedef defaultforshortunion 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 as car_count instead 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
RulesExample Can contain a mix of characters and numbers. However it cannot start with a number H2o First character must be a letter or underscoreNumber1; _area Can be of mixed cases including underscore character XsquAre my_num Cannot contain any arithmetic operatorsR*S+T … or any other punctuation marks…#@x%!! Cannot be a C keyword/reserved wordstruct; printf; Cannot contain a spaceMy height … identifiers are case sensitiveTax != tax 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 - a violation 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
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 Three types of errors: Semantic
30
Introducing Debugging Find errors!
31
Missing closing */ Should be { Should be } Missing ; Introducing Debugging
32
Is the program syntactically correct? What about semantics? Will program operate as you expect? Introducing Debugging
33
Is the program syntactically correct? What about semantics? Will program operate as you expect? Introducing Debugging n = 5, n squared = 25, n cubed = 625
34
Introducing Debugging Output
35
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. Introducing Debugging
36
Find errors QUIZ
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.