Engineering H192 - Computer Programming

C Programming Basics
Lecture 5

Winter Quarter

The Ohio State University Gateway Engineering Education Coalition

Lect 5

P. 1

Engineering H192 - Computer Programming

ENG H192 Course Web Page
• A web page which contains the course syllabus, updated lecture notes and other useful information may be found at: http://feh.eng.ohio-state.edu • A copy of Professor Dick Busick’s flowchart spreadsheet is on the Web site
Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P. 2

Engineering H192 - Computer Programming

C Program Basics
• C vs. C++ – C is a subset of C++. All of features in C are contained in C++ – C++ adds more libraries with functions for object oriented programming – C++ also adds more keywords and some added features.

Winter Quarter

The Ohio State University Gateway Engineering Education Coalition

Lect 5

P. 3

pg. Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P. C++ adds 30 more keywords (see text. They are called reserved words or keywords. 563). 4 .Engineering H192 . The keyword list for C contains 32 words (see text. 44).Computer Programming Keywords in C and C++ Certain words have a special meaning to the C or C++ compiler. pg. We should not try to use these words as names of variables or function names in a program.

Engineering H192 .Computer Programming Some Keywords in C and C++ asm auto break case catch char class const continue default delete do Winter Quarter double else enum extern float for friend goto if inline int long new operator private protected public register return short signed sizeof static struct switch template this throw try typedef union unsigned virtual void volatile while Lect 5 P. 5 The Ohio State University Gateway Engineering Education Coalition .

6 .Computer Programming Program Structure in C • EACH complete C program is composed of: – – – – – Comment statements Pre-processor directives Declaration statements One or more functions Executable statements Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P.Engineering H192 .

7 .Engineering H192 .Computer Programming Program Structure in C • EACH complete C program is composed of: – Comment statements – Pre-processor directives – Comment statements – Declaration statements – Comment statements – One or more functions – Comment statements – Executable statements – Comment statements Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P.

Computer Programming Comment Statements • Formal Comments: /* Comment …. */ – Used for detailed description of functions or operations (for our benefit. not C): // Comment …. • Informal Comments (only in C++.. not compiler’s). Ends at the end of line – Used for quick comments like: int temp. 8 . // temporary variable for storing // the input value Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P. – Can take multiple lines in source file.Engineering H192 ..

Engineering H192 .h> Note Space #define -.Computer Programming Pre-Processor Directives #include -.7182818 #define pi 3. 9 .header files for library functions Example: #include <stdio.define constants and macros Examples: #define e 2.14159265359 Note Spaces Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P.

Computer Programming Declarations • Declarations tell the compiler what variable names will be used and what type of data each can handle (store).Engineering H192 . • Example declarations: int a. float r. q . y. Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P. b. double x. n . 10 . c . z . char m. p.

Engineering H192 . but only a limited number of digits of precision. • Floating point variables: float c.Computer Programming Data Types • Integer variables: int a. like 3. b . 11 . store only whole numbers like 3 or 7. like c or d.65. • Integer variables.33 or 7. like a or b. • Floating point variables. and only up to certain maximum values.14159. Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P. not 3. d . store rational numbers.

12 .Engineering H192 .or a "bit". is either a 0 or a 1 – Byte -.often a collection of 4 bytes together • On the SGI Unix system: – an "int" data type takes up 4 bytes (on some systems. an "int" is only 2 bytes) – a "float" data type takes up 4 bytes – a "double" data type take up 8 bytes – a "char" data type takes up 1 byte Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P.usually a collection of 8 bits together – Word -.Computer Programming Internal Storage Representation • Definitions: – Binary digit -.

Engineering H192 . The body of each user-written function is enclosed in braces. 13 . { } (or curly brackets) • The syntax of a function is: <function type> function_name (arg. list) { /* beginning of function */ } /* end of function */ Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P.Computer Programming Programs Have One or More Functions • Even the main program is a function.

Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P. Assigning Values temp = 5 . 14 . i. Calling Functions plotxy (x. • Calculations x = (5.e. / 2 + 6) * 7 . char a .Engineering H192 .. y) .Computer Programming Executable Statements • Simple Declaring variables int temp . temp is assigned the value of 5 • Complex.

Computer Programming Arithmetic Operators * / % multiply + add divide subtract remainder. expressions: num + 3. /* x will be equal to 3 */ • An expression can be used almost anywhere a variable of the same type can be used.. where: x = 13 % 5 .5. a * d .. Ex.Engineering H192 . Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P. . 15 .

Parentheses: () – Evaluate from the inside out 2. pg. (D&D text. Multiplication. 16 . Addition and Subtraction: + and NOTE: Multiple occurrences of operations with the same precedence evaluate from left to right. 38) Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P.Engineering H192 .Computer Programming Arithmetic Operators – Order of Evaluation 1. /. Division. and % 3. and Remainder: *.

9 + 8 % 7. x x x x x = = = = = 2 * 3 .(4 + 5) + 8 % 7.9 + 8 % 7. The Ohio State University Gateway Engineering Education Coalition Lect 5 P.Computer Programming Arithmetic Operators – Order of Evaluation For example: x = 2 * 3 .9 + 1. 17 Winter Quarter .Engineering H192 . -3 + 1. 6 . 6 . -2.

18 . Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P. x = -42. x = 6 / (2 + 1) .(3 + 8) * 4.3 + 8 * 4.Computer Programming Arithmetic Operators – Order of Evaluation Another example: x = 6 / 2 + 1 .Engineering H192 . x = 33.

Computer Programming Mixed Mode Arithmetic • When performing arithmetic operations.Engineering H192 .the result is floating point • Integer operations produce integer results (remember how you first learned to to division?) Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P. 19 . if both operands are integer – Mixed. the "mode" will one of: – Floating point. if one operand in integer and the other is floating point -. if both operands are floating point – Integer.

x=x+5. x=x/5. x=x*5. Meaning: x=5. x –= 5 .Computer Programming Assignment Operators Operator: = += –= /= *= %= Example: x=5. x *= 5 .Engineering H192 . x /= 5 . x += 5 . x= x%5. x %= 5. 20 . Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P. x=x–5.

/* This adds b to a.Computer Programming Assignment Operators Example of assignment operators: int a = 4. */ /* What is value of c now? */ Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P.Engineering H192 . c = 36 . 21 . a = ? c /= a + b . a += b . b = 2.

a += b .Engineering H192 . 22 . so a = 4 + 2 or a = 6 ] c /= a + b . c = 36 . /* What is value of c now? */ Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P.Computer Programming Assignment Operators Example of assignment operators: int a = 4. b = 2. /* This adds b to a. a = ? */ [ Answer: a = a + b.

Engineering H192 . a += b . /* What is value of c now? */ [ Answer: c = c / (a + b). b = 2.Computer Programming Assignment Operators Example of assignment operators: int a = 4. and a = 6 now. so c = 36 / (6 + 2). a = ? */ [ Answer: a = a + b. /* This adds b to a. 23 . so a = 4 + 2 or a = 6 ] c /= a + b . c = 36 . so c = 36 / 8 or c = 4 ] Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P.

count = count . count-. count = count + 1 . --count ..Engineering H192 . 24 .Computer Programming Increment/Decrement Operators Operator: count++ .1 .1 . Meaning: count = count + 1 . ++count . When? After use Before use After use Before use Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P. count = count .

/* What are the values of a.++a . 25 .Engineering H192 . c now? */ */ Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P.Computer Programming Increment/Decrement Operators Examples of increment and decrement operators: int a = 4. b. b. c. c now? c = b-. /* What are the values of a. b = 2. c = ++a + b-...

c = ++a + b-.. c = 7) c = b-.++a . /* What are the values of a. b = 2.. b. b. b = 1. 26 . c.Computer Programming Increment/Decrement Operators Examples of increment and decrement operators: int a = 4. /* What are the values of a.Engineering H192 . c now? */ Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P. c now? */ (Answers: a = 5.

. /* What are the values of a. c now? */ (Answers: a = 5.Engineering H192 . b.++a . c. c = -5) Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P. b = 0. c = 7) c = b-. b. b = 1. c now? */ (Answers: a = 6. /* What are the values of a. b = 2.. c = ++a + b-.Computer Programming Increment/Decrement Operators Examples of increment and decrement operators: int a = 4. 27 .

28 .Engineering H192 .Computer Programming Relational Operators Operator: < > <= >= == != Meaning: Less Than Greater Than Less Than or Equal To Greater Than or Equal To Exactly Equal To Not Equal To Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P.

29 . the value of 1 stands for true and 0 stands for false.Computer Programming Relational Operators • Used for asking questions like: Is x bigger than 10? • In C. But C will recognize any non zero value as true.Engineering H192 . • NOTE: "==" is NOT same as "=" Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P.

Engineering H192 .. but 5>6 && 7>4 is not true (i.e. 30 . false) || (or) Ex: 5>6 || 7>4 is true 5<6 || 7<4 is also true Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P.Computer Programming Logical Operators ! (not) Ex: a != b is true if a and b are not equal && (and) Ex: 5<6 && 7>4 is true.

b) which raises a to the b power.cpp The Ohio State University Gateway Engineering Education Coalition Lect 5 P. 31 Winter Quarter . You must put a #include <math.h> in your source code and must also use the -lm switch in your compile command when on the SGI UNIX system.Engineering H192 . You can use the math function pow (a.out myprog.Computer Programming Exponentiation Operations Exponentiation is not written as x**2 or x^2 C does not have an exponentiation operator. Ex: >g++ -o myprog.

0.Computer Programming /*****************************************/ /* Name: Brutus Buckeye */ /* Seat No. } Winter Quarter Skeleton Program The Ohio State University Gateway Engineering Education Coalition Lect 5 P.Engineering H192 . Instr: W.h> #include <stdlib.h> int main ( ) { statements .h> #include <math. 32 . Hayes */ /* Program progname/assignment */ /*****************************************/ #include <stdio.

Engineering H192 . 33 .cpp – Save to name for program – Edit program Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P.cpp” file as a basis for your assignments • Going to look much like Skeleton program on previous slide • USAGE for an program – Open skelton.Computer Programming Shell for programs • Create a “skelton.

34 .Computer Programming Today’s Assignment • Today’s Assignment is G04 • G03 has a C program that calculates values and assigns them to variables. • Then.Engineering H192 . copy the program into your account and run it to get the correct answers. Compare your hand calculations to the computer’s calculations. • What needs to be added to the code so that you know what the computer calculated? Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 5 P. • You are to do the calculations by hand (quiz and midterm material!!) and print the answers on the problem sheet.

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.