Docsity

Notes on Storage Class and Linkage Example | CMSC 212, Assignments of Computer Science

Partial preview of the text

Download Notes on Storage Class and Linkage Example | CMSC 212 and more Assignments Computer Science in PDF only on Docsity!

Announcements

z

Program #1A– due Thursday

z

Reading– Chapter 3 & 7 (skip 7.6)– Chapter 10 (Thursday, skip 10.2)

Storage Class and Linkage Example

extern int b;static int c;int func1(int e){

static int longLived;int funcLifetime;func2(42);

Examples of If Statements

if (x > 43) {

printf(“X is bigger than 43\n”); } if (!x) {

printf

(“x is zero”);

} if (x == y) {

x = 42; } else {

x = y; }

if (x > 43) {

printf(“X is bigger than 43\n”); } else if (x > 10) {

printf(“X greater than 10\n”); } else {

printf(“X is 10 or less\n”); }

While Statement

while (expression) {

body

} z

Similar to while in Java

z

Expression is (like if) an integer expression– Loop terminates when the expression is 0

z

Forced loop termination– break – exit

one

innermost loop level

  • break does

not

take a label like Java

z

Forced next iteration– continue – return to loop head and evaluate expression

  • Also applies only to inner most loop

More Examples of While Loop

while (!done) {

... if (a == b) {

done = 1;

while (1) {

... if (a == b) {

break;

For Statement

z

for (expression1; expression2; expression3) …– Expression

  • Run once before first iteration• Used to initialize values
    • Expression
      • Evaluated before each loop iteration• Controls loop execution, when it’s 0 loop stops
        • Expression
          • Run after each iteration
            • Can omit any expression– Loop body may contain break or continue like while loops

Do Statement

do {

statement

} while (expression); z

Like a while, but the body always runs

z

Rarely used

Switch Statement

switch (expression) {

case constant-expression:

statement;break; ….default:

statement;break;

} z

Execution starts at the constant-expression equal tothe expression– Without the break, will continue to next statement!!!

z

Default constant expression matches anything else

Functions

z

type name (parameters) block– type: return type for the function– name: name of the function– parameters: a comma separated list of types and names– block: the code to run

z

return statement: return expression;– terminates a function at that point– expression is the value returned by the function

Function Example

int find(int data[10], int value){

for (i=0; i < 10; i++) {

if (data[i] == value) {

return i; } }

}

Recursive Functions

z

Functions that call themselves– can be indirect: a calls b calls a

z

Example:void binary_to_ascii(unsigned int value){

unsigned int quotient;quotient = value / 10;if (quotient != 0) {

binary_to_ascii(quotient);

} printf("%d", value % 10 + '0');

Abstract Data Types

z

Key Idea: Hide implementation from users– lets implementation evolve without changing use– makes it easier to understand code

  • simply know what a function should do not how

z

Static keyword helps with this– hides global variables from other modules– hides functions that should not be called from other modules