111CMSC 212 – S07 (lect 3)AnnouncementsLast Day for Schedule Adjustments– 2/6/07(next Tuesday)Program #1– due 2/13/07Quiz #1– Monday, February 5 in Discussion Section - UNIXEmail– please send email to course staff from UMD accounts– email from hotmail, yahoo, aol, etc. will often be auto deletedReading– Chapter 3 & 4– Chapter 7& 8 (Tuesday)2CMSC 212 – S07 (lect 3)Data Types in CInteger Family– char typically 8 bits– short typically 16 bits– int typically 32 or 64 bits– long typically 32 or 64 bits– long long typically 64 bits– All are signed by default, but can be made unsigned• unsigned int (typically, 0 to 4,294,967,295)– Literals• Decimal (255), Hex (0x255), signed (-255)• Character (‘a’, ‘\n’)– Don’t be stingy with size, when in doubt use a larger sizeFloating Point– float, double, long double– Literals (3.14159, 1E10, 25., 6.023e23)223CMSC 212 – S07 (lect 3)Data Types in C (cont.)String Literals– “a long dull string”, “\n”, “”Enumerated Types– Declaring an enumerated type:• enum operatorType { plusOperator, minusOperator };– Declaring a variable of an enumerated type:• enum operatorType currentOperator;– Using an enumerated type in an expression:• currentOperator = plusOperator;– Can control values of enumerated type:• enum operatorType { plusOperator=3, minusOperator =8};4CMSC 212 – S07 (lect 3)Variable Declarations<type> <variable1>, <variable2>, … ;– int a, b, c;Variables names may not be reserved words– Must start with A-Z,a-z,_– May contain numbers or “_”• aValid_Variable_Name• aValid_Variable_Name_2May initialize variables as part of declaration– int a = 4;Implicit Declarations (Book section 3.2.4)– Skip section book and don’t ever use335CMSC 212 – S07 (lect 3)Simple ArraysArray dimensions must be known at compile timeArray Declaration– int a[10]; /* array of 10 elements */Accessing array elements– Arrays start from 0 and go to n-1 elements– Compiler will let programmer access invalid elements• Can lead to many problems that are hard to find– Examples of array references• A[0]• A[i]• A[i+3]6CMSC 212 – S07 (lect 3)TypedefSometimes you want to name your own type– Makes program easier to read– Makes it easier to change types latertypedef double dollars;typedef struct{int a;float b;dollars c;} MySTy;int main(void){MySTy x, y;…}447CMSC 212 – S07 (lect 3)Variable ScopeFile Scope– Entire file– Not declared inside any functionBlock Scope– Before an executable statement in a block of code { .. }Repeating same variables in nested scopevoid main() {int a;{ int a; a = 3; }}– Should be avoided!!8CMSC 212 – S07 (lect 3)Storage and LinkageLinkage– Determines how multi-file visibility issues are resolved– extern - There is only one instance of this variable– static - This variable is private to this filefoo.c:static int a;bar.c:static int a;Storage Classes– Default is automatic: • one variable instance per function instance• lifetime limited to while function is active – running or in called subroutine– Static changes default• one variable instance• lifetime is the entire program559CMSC 212 – S07 (lect 3)Storage Class and Linkage Exampleextern int b;static int c;int func1(int e){static int longLived;int funcLifetime;…func1(42);…}10CMSC 212 – S07 (lect 3)StatementsAssignment is just an expression:– Legal statements:• x = 3;• y = 5 + (x = 3);• y + 3;• x == y;If Statementif (expression) {statements1} else {statements2}– () are required – Not necessary, but good style is to Always use { } around statements in an if– Expression is of type int• any non-zero means the true statement is evaluated6611CMSC 212 – S07 (lect 3)Examples of If Statementsif (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=0){printf(“this will never print\n”);}else{printf(“x is now 0\n”);}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”);}12CMSC 212 – S07 (lect 3)While Statementwhile (expression) {body}Similar to while in JavaExpression is (like if) an integer expression– Loop terminates when the expression is tested and determined to be 0 (false)Forced loop termination– break – exit one innermost loop level – break does not take a label like Java’s break canForced next iteration– continue – return to loop head and evaluate expression• Again, also applies only to inner most loop7713CMSC 212 – S07 (lect 3)Examples of While Loopswhile (x < y) {….}while (x < y && !error) {...}while (!done) {while (x < y) {…}}14CMSC 212 – S07 (lect 3)More Examples of While Loopwhile (!done) {...if (a == b) {done = 1;}…}while (1) {...if (a == b) {break;}…}8815CMSC 212 – S07 (lect 3)For Statementfor (expression1; expression2; expression3) …– Expression1• Run once before first iteration• Used to initialize values– Expression2 • Evaluated before each loop iteration• Controls loop execution, when it’s 0 loop stops– Expression3• Run after each iteration– Can omit any expression– Loop body may contain break or continue like while loops16CMSC 212 – S07 (lect 3)Examples of For Loopfor (i=0; i < 10; i++) {a[i] = 0;}for (;;) {}for (i=0, b=21; i < 10; i++) {a[i] = b + i;}9917CMSC 212 – S07 (lect 3)String Length Examples#include <stdio.h>#define MAX 20int slen1(char name[MAX]){int size=0, index = 0;while (name[index] != '\0'){size++;index++;}return size;}int slen2(char name[]){int size, index;for (size=0, index=0; name[index] != 0; size++, index++);return size;}int slen3(char name[]){int size = 0, index=0;do {size++;index++;}while (name[index]);return size; }int main(void){char myname[MAX] = "Jan Plane";printf("by slen1: length =%d\n",slen1(myname));printf("by slen2: length = %d\n",slen2(myname));printf("by slen3: length = %d\n",slen3(myname));return 0;}18CMSC 212 – S07 (lect 3)Do Statementdo {statement} while (expression);Like a while, but the body always runsRarely
View Full Document