Download presentation
Presentation is loading. Please wait.
Published byHope Wilkins Modified over 9 years ago
1
C Language Elements: Preprocessor Directives Commands that give instructions to the C preprocessor Modifies text of a C program before compilation Two most common: #include #define
2
C Language Elements Preprocessor Directives: –#define –#include Reserved Words: int void double Standard Identifiers: printf scanf User-defined Identifiers: –Must consist of letters, digits, underscores –Cannot begin with a digit –Cannot use a C reserved word
3
C Language Elements Variable Declarations Executable Statements Input/Output Operations & Functions –printf, scanf, fprintf, fscanf Three Input/Output streams: – stdin (standard input) – stdout (standard output) – stderr (standard error)
4
Expressions Arithmetic Operators + - * / % Precedence –Parenthesis Rule –Operator Precedence: unary +, - first *, /, % next binary +, - last –Associativity Rules: unary: right to left binary: left to right
5
Functions Functions divide code into meaningful parts: Separates factual (what) from procedural (how) Clarifies Algorithm steps Every Function: –Has a return type –Has a Name (following Identifier conventions) –May have Arguments (aka Parameter List) enclosed in parentheses ( )
6
Functions A function appears in a program three times: 1. Prototype Declaration: –binds the type and the name –provides arguments and types 2. Function Definition: –Defines the procedure the function will execute –Sets the return value 3. Function Call: –A reference to the function in main or another function that calls upon it to execute and return a value
7
Control Structures Statements are grouped into Control Structures (Blocks) Each block has exactly one entry and one exit point Simple Statements Sequential Statements Compound Statements Block Statements
8
ANSI C Statements Selection: if, switch (Decision) Iteration: while, for, do-while (Loops) Jump: break, continue, goto, return Label: case, default (used with switch) & label used with goto Expression: any valid expression Block: blocks of code
9
Relational and Equality Operators OperatorMeaningType <less thanrelational > greater thanrelational <=less than or equal torelational >=greater than or equal torelational ==equal toequality !=not equal toequality
10
Logical Operators && (logical and) || (logical or) ! (logical not - logical complement or negation)
11
Precedence (See Table 11.1 p. 536) function callshighest ! + - & (unary operators) * / % + - = > == != && || =lowest
12
Iteration for loop for (initialization; condition; increment) statement; while loop while (condition) statement; do while loop do { statement; } while(condition) post-fix, pre-fix increment
13
Modularity Advantages of Modularity A modular program is easier to write and debug. Functional components can be written and debugged separately A modular program is easier to maintain and change reusability –A modular program is easier to control and divide among different programmers
14
Modularity Pass by Value: –copies the value of an argument into the formal parameter of the function x = sqrt(value); Pass by Reference: –the address of an argument is copied into the parameter inside the function, the address is used to access the actual argument used in the call
15
Meaning of the * Symbol (3 uses) * binary operator for multiplication x * y; variable declaration: char *signp –signp is a pointer to a variable of type char "follow the pointer": unary indirection operator *signp = '+'; *fracp = magnitude - *wholep;
16
Scope The scope rules of a language are the rules that govern whether a piece of code knows about or has access to another piece of code or data. The scope of a name refers to the region of a program where a particular meaning of a name is "visible" or can be referenced
17
Scope Each function is a discrete block of code A function's code is private to that function and cannot be accessed by any statement in any other function… …only through a call to that function Code and data defined within one function cannot interact with the code or data defined in another function because the two functions have a different scope.
18
Scope Variables defined within a function are called local variables A local variable comes into existence when the function is entered and destroyed upon exiting the function Constant Macros (#define's) have a scope that begins at their definition, and continues to the end of the source file
19
Data Types: int vs. double types positive integers are represented by standard binary numbers: –e.g. 13 is 001101 in binary, 31 is 011111, 32 is 100000, etc. double (floating point format) are represented by: real number = mantissa X 2 exponent … and stored as: mantissa exponent
20
Automatic & Explicit Conversion We’ve already seen examples of Automatic conversion: int x=3, y=4; double a=3.4, b=2.7; x = a*b; /* a*b is 9.18, x gets the value 9 */ a = b*x; /* x is converted to double 3.0, a gets the value 8.1 */ Explicit conversion: a = (double)x / (double)y; /* a gets 0.75 */ a = (double) (x/y) /* what does a get?? */ x = (int)a + (int)b /* x gets 5 */
21
char type: printable ASCII codes 32 = (space) 33 = ! 34 = " 35 = # 36 = $ 37 = % 38 = & 39 = ' 40 = ( 41 = ) 42 = * 43 = + 44 =, 45 = - 46 =. 47 = / 48 = 0 49 = 1 50 = 2 51 = 3 52 = 4 53 = 5 54 = 6 55 = 7 56 = 8 57 = 9 58 = : 59 = ; 60 = < 61 = = 62 = > 63 = ? 64 = @ 65 = A 66 = B 67 = C 68 = D 69 = E 70 = F 71 = G 72 = H 73 = I 74 = J 75 = K 76 = L 77 = M 78 = N 79 = O 80 = P 81 = Q 82 = R 83 = S 84 = T 85 = U 86 = V 87 = W 88 = X 89 = Y 90 = Z 91 = [ 92 = \ 93 = ] 94 = ^ 95 = _ 96 = ` 97 = a 98 = b 99 = c 100 = d 101 = e 102 = f 103 = g 104 = h 105 = i 106 = j 107 = k 108 = l 109 = m 110 = n 111 = o 112 = p 113 = q 114 = r 115 = s 116 = t 117 = u 118 = v 119 = w 120 = x 121 = y 122 = z 123 = { 124 = | 125 = } 126 = ~
22
Comparisons of type char Comparisons between characters are based on comparisons of binary representations …So '0' < '1' < '2' < '3' < '4' < '5' etc. and 'a' < 'b' < 'c' < 'd' < 'e' < 'f' etc. and 'A' < 'B' < 'C' < 'D' < 'E' < 'F' etc. Is 'a' < 'A' ?
23
Conversions between int and char Explicit conversion between int and char: int x=65; char a='K'; printf("%c", (char) x); /* prints 'A' */ printf("%d", (int) a); /* prints 75 */ x += 6; a = (char) x; printf("%c", a); /* prints 'G' */
24
Single Dimension Arrays Size (memory allocation) of an array: total bytes = sizeof(basetype) * size of array Arrays are allocated as sequential locations in memory Arrays are indexed beginning with 0, up to the size_of_the_array - 1 Example: ‘primes’ array ( int primes[10] ): 2357111317192329 0123456789index value
25
Arrays & Pointers The name of the array is really just a pointer to an array: int primes[9]; /* the identifier "primes" is a pointer to the first element */ int *p; p = primes; /* same as p = &primes[0]; */
26
Two-Dimensional Arrays Two dimensional arrays are stored in a row-column matrix, where the first index is the row and the second is the column Using this paradigm, the rightmost index changes faster than the leftmost when accessing the elements in an array in the same order that they are stored in memory 0 1 2 3 012012 [0][0][0][1][0][2][0][3] [1][0][1][1][1][2][1][3] [2][0][2][1][2][2][2][3]
27
Sorting 74 45 83 16 45 83 74 16 45 83 74 16 45 74 83
28
Strings String Variables Strings may be declared as static or dynamic variables Static Declaration and Initialization Sample Declaration char String1[10]; Sample Combination Declaration & initialization char String1[10] = ”MIS 121"; /* must be <= 10 */
29
Strings Arrays of Strings An array of strings is a 2D array of char, where each row is a string Example: char months[12][10] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November","December"}; for (i=0;i<12;i++) printf("%s\n",months[i]);
30
Strings String Functions: strcpy strncpy strlen strcat strncat strcmp strncmp
31
Structures Structure: a collection of variables referenced under one name. Provides a convenient means of keeping related information together. A structure declaration forms a template that can be used to create structures. The variables that make up the structure are called members of the structure (also commonly referred to as elements or fields)
32
Definition: typedef struct { // This defines a structure template char Name[20] // that can then be used in other places to int Number; // declare actual variables. char Dept; // This in effect represents int Phone; // a new user defined data type. } EmployeeRecord_t; Declaration : EmployeeRecord_t Employee; // a variable instance // Here attributes such as name, class, type, // and size are bound to this variable. Use of structure: Employee.Phone = 8813620; if (Employee.Number > 17) Definition, Declaration, and Use of a structure Without pointers - Simplified with typedef
33
Using structs Accessing members with the '. ' or ' -> ' Operators Arrays of structs Passing structs or arrays of structs to functions Arrays of Pointers to structs
34
Encapsulation The mechanism that binds together code and data, and that keeps both safe from outside interference or misuse. An “Object” is a logical entity that encapsulates both data and the code that manipulates that data. Some of the code or data within an object may be “private” or inaccessible to anything outside the object. An object is a variable of a user-defined type.
35
Polymorphism “One Interface, Multiple Methods” Allows one interface to be used with a general class of actions. Real-world Example: A thermostat –The thermostat (interface) is the same, no matter what type of furnace you have (coal, gas, electric) It’s the compiler’s job to select the specific action (method) that applies to each situation –(We’ll look at a template class “stack” that works for any data type).
36
Inheritance Inheritance is the process by which one object can acquire the properties of another object. Supports the concept of classification: An object need only define those qualities that make it unique within its class. AppleFruitFood Golden Delicious is anis a
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.