Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to C Programming Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.

Similar presentations

Presentation on theme: "Intro to C Programming Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University."— Presentation transcript:

1 Intro to C Programming Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University

2 TRU-COMP2130 C Programming2 Course Objectives The better knowledge of computer systems, the better programing. Computer SystemC Programming Language Computer architecture CPU (Central Processing Unit) IA32 assembly language Introduction to C language Compiling, linking, loading, executing Physical main memory MMU (Memory Management Unit) Virtual memory space Memory hierarchy Cache Dynamic memory management Better coding – locality Reliable and efficient programming for power programmers (to avoid strange errors, to optimize codes, to avoid security holes, …)

3 TRU-COMP2130 C Programming3 Course Contents Introduction to computer systems: B&O 1 Introduction to C programming: K&R 1 – 4 Data representations: B&O 2.1 – 2.4 C: advanced topics: K&R 5.1 – 5.9, 5.11, 6 – 8 Introduction to IA32 (Intel Architecture 32): B&O 3.1 – 3.8, 3.13 Compiling, linking, loading, and executing: B&O 7 (except 7.12) Dynamic memory management – Heap: B&O – 9.9.2, – 9.9.5, 9.11 Code optimization: B&O 5.1 – 5.6, 5.13 Memory hierarchy, locality, caching: B&O 5.12, 6.1 – 6.3, – 6.4.2, 6.5, – 6.6.3, 6.7 Virtual memory (if time permits): B&O 9.4 – 9.5

4 TRU-COMP2130 C Programming4 Unit Learning Objectives Use cc (or gcc ) to compile C programs. Write a C program with the common (very similar) syntaxes with Java. Use printf() library function to print messages. Use define statements for constants. Use getchar() and putchar() for I/O. Use of integer values as Boolean values. Use a char array as a string. Use bit operators to manipulate bits in a given integer. Understand the scope of external identifiers. Distinguish signed integer types and unsigned integer types. Use register variables for indexing arrays. Use conditional inclusion statements with #if, #elif, …. …

5 TRU-COMP2130 C Programming5 Unit Contents Introduction Types, Operators, and Expressions Control Flow Functions and Program Structure

6 TRU-COMP2130 C Programming6 1. Introduction Only the topics that are different from Java will be discussed.

7 TRU-COMP2130 C Programming7 Getting Started C program files should end with.c. hello.c #include // similar to import statements in Java // stdio.h includes the information about the standard library main() // similar to main method in Java { printf (hello, world\n); // printf() defined in stdio.h } How to compile? $ gcc hello.c or $ gcc hello.c-o hello If you do not have anything wrong, you will see a.out in the same working directory. How to run? $./a.out

8 TRU-COMP2130 C Programming8 printf (hello, world\n); … is a character string. This is also called a string constant. printf() is a library function to print a string to the terminal. Very similar primitive data types char, short, int, long, float, double, … Same control structures if, if-else, for, while, do-while, switch Same operators (we will discuss some new ones later.) +, -, *, /, %, =, ==, !=, &&, ||, … Can you write a program to convert Fahrenheit temperature to Celsius temperature? The conversion formula: C = 5 / 9 × (F – 32)

9 TRU-COMP2130 C Programming9 #include... /* print Fahrenheit-Celsius table for fahr = 0, 20, 40,..., 300, using a loop */ main() { int fahr, celsius; int lower, upper, step; lower = 0;/* lower limit of temperature scale */ upper = 300;// upper limit step = 20;// step size fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9;// integer value? printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; } }

10 TRU-COMP2130 C Programming10 printf("%d\t%d\n", fahr, celsius); %d specifies an integer argument (d: decimal). Output Can we print better? Like printf("%3d\t%6d\n", fahr, celsius);

11 TRU-COMP2130 C Programming Is there any problem in the above output? The Celsius temperatures in the previous out are not accurate. For example 0 o F is o C. Then? We can use float data type for fahr and celsius instead of int. float fahr, celsius; Then how to print real numbers using printf() ? celsius = 5 * (fahr-32) / 9;// okay? printf("%3.0f\t%6.1f\n", fahr, celsius); %f specifies a floating point argument (f: floating point). 6.1 means 1 decimal out of 6 digits.

12 TRU-COMP2130 C Programming12 Some easy errors: printf("%d\t%6.1f\n", fahr, celsius); ??? printf("%3.0f\t%6.1f\n", fahr); ??? printf("%6.1f\n", fahr, celsius); ???

13 TRU-COMP2130 C Programming13 Summary of printf() format specifiers %dprint as decimal integer %6dprint as decimal integer, at least 6 characters wide %fprint as floating point %6fprint as floating point, at least 6 characters wide %.2fprint as floating point, 2 characters after decimal point %6.2fprint as floating point, at least 6 characters wide and 2 after decimal point Other format specifiers %ofor octal %xfor hexadecimal %cfor character %sfor character string %% itself

14 TRU-COMP2130 C Programming14 Symbolic Constants #define name replacement_list #include #define LOWER 0// similar to final variable in Java #define UPPER 300 #define STEP 20 main() { float fahr, celsius; for (fahr =... ; fahr <=... ; fahr = fahr +...) { celsius = 5 * (fahr-32) / 9; printf("%3.0f\t%6.1f\n", fahr, celsius); }

15 TRU-COMP2130 C Programming15 Symbolic Constants #define name replacement_list #include #define LOWER 0// similar to final variable in Java #define UPPER 300 #define STEP 20 main() { float fahr, celsius; for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP) { celsius = 5 * (fahr-32) / 9; printf("%3.0f\t%6.1f\n", fahr, celsius); }

16 TRU-COMP2130 C Programming16 Character Input and Output #include /* copy input to output; 1st version */ main() { int c; c = getchar();// in order to read a character while (c != EOF) {// EOF is defined in stdio.h. // EOF means End of File, ^D. putchar(c);// in order to print a character c = getchar(); } } Where did getchar(), EOF, and putchar() come from? Lets run this program to understand I/O better.

17 TRU-COMP2130 C Programming17 #include /* copy input to output; 2nd version */ main() { int c; while ((c = getchar()) != EOF)// okay? putchar(c); } What if we use the follow code instead? while (c = getchar() != EOF) The above code is equivalent to while (c = (getchar() != EOF)) This is because the precedence of != is higher than =.

18 TRU-COMP2130 C Programming18 Operator precedence rules () [] ->. ! ~ - ( unary ) + ( unary ) * & sizeof * / % + - > >= == != & ^ | && || = += -= *= /= %= &= |= ^= >=

19 TRU-COMP2130 C Programming19 Boolean values? Syntax error? while (c = getchar() != EOF) Lets assume getchar() returns A. Then getchar() != EOF becomes TRUE. The data type of c is int. Is there a boolean data type in C? No. 0 is FALSE and non-zero value is considered as TRUE in C. Hence getchar() != EOF becomes 1, and c has 1. while(c) -> while(1). The loop repeats.

20 TRU-COMP2130 C Programming20 Arrays #include /* count digits, white space, others */ main() { int c, i, nwhite, nother, ndigit[10]; // very similar to Java nwhite = nother = 0; // really necessary? for (i = 0; i < 10; ++i) ndigit[i] = 0; while ((c = getchar()) != EOF) if (c >= '0' && c <= '9') ndigit[c-'0']++; else if (c == ' ' || c == '\n' || c == '\t') ++nwhite; else nother++; printf("digits ="); for (i = 0; i < 10; i++) printf(" %d", ndigit[i]); printf(", white space = %d, other = %d\n", nwhite, nother); }

21 TRU-COMP2130 C Programming21 A character, e.g., 3, is an integer value. int c = 3; is valid. ASCII table

22 TRU-COMP2130 C Programming22 Functions #include int power(int m, int n); // function declaration // What if it is not declared before //main()? main() { int i; for (i = 0; i < 10; i++) printf("%d %d %d %d\n", i, power(2, i), power(-3, i), i); return 0; } /* power: raise base to n-th power; n >= 0 */ int power(int base, int n) { int i, p;...// How to implement?

23 TRU-COMP2130 C Programming23... for (i = 0; i < 10; i++) printf("%d %d %d %d\n", i, power(2,i), power(-3,i), i); return 0; } /* power: raise base to n-th power; n >= 0 */ int power(int base, int n) { int i, p; p = 1; for (i = 1; i <= n; i++) p = p * base; return p; }

24 TRU-COMP2130 C Programming24 Arguments /* power: raise base to n-th power; n >= 0; version 2 */ int power(int base, int n) { int p; for (p = 1; n > 0; n--) p = p * base; return p; } base, n and p are local variables that are used only in power(). When the function is called (or also called invoked), the values will be copied (oac passed) into base and n. Even if those variables are updated with other values, the new values will not be passed back to the caller function. How is this implemented in main memory? This type of function calling is called call by value.

25 Character Arrays #include #define MAXLINE 1000 int getline(char line[], int maxline); void copy(char to[], char from[]); main() { int len; /* current line length */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; // longest line saved max = 0; while ((len = getline(line, MAXLINE)) > 0) if (len > max) { max = len; copy(longest, line); } if (max > 0) printf("%s", longest); return 0; }... int getline(char s[], int lim) { int c, i; for (i=0; i

26 TRU-COMP2130 C Programming26 In main(), while ((len = getline(line, MAXLINE)) > 0) The data type of line[] is an array. getline() can change the contents in line [], and the change in line[] can be used in main(). Very similar to Java. This type of function calling is called call by reference. How is this implemented in main memory? line represents MAXLINE variables - line[0], line[1],..., line[MAXLINE-1]. How? Address should be passed. line contains the address of the first byte of line[0]. line[i] is the memory area pointed by the address line + sizeof(char) * i. addrvaluevar ……… defabcline ……… abcCline[0] abc+1Oline[1] ……… Main memory

27 TRU-COMP2130 C Programming27 In getline(), int getline(char s[], int lim) The data type of the first parameter is an array. The address stored in line in main() is passed into s. Therefore s[i] will become equal to line[i]. Any change in this variable will be preserved so that the caller can use the change. This type of function calling is called call by reference. s[i] = '\0'; After all the characters are stored in s[] till EOF or \n, \0 is stored to mark the end of the character string. That is, a string is a list of characters with the end of \0, and a char array is usually used to keep a string. This is very important.

28 TRU-COMP2130 C Programming28 In main(), if (max > 0) printf("%s", longest); %s is used to print a character string. The argument is a char array, not longest[], nor longest[0], and … What is the data type of longest ? char longest[MAXLINE]; What value is stored in longest ? The address of the first byte of longest[0] to represent all longest[0], longest[1], …. Can a char array be a character string? Yes. printf() with %s will try to print characters pointed by longest (, i.e., longest[0], longest[1], …,) until the character becomes \0.

29 TRU-COMP2130 C Programming29 char name[256], tmp[256]; name[0] = C; name[1] = O; name[2] = M; name[3] = P; name[4] = \0;// it is very important. printf(course number = %s\n, name); name[5] = ; name[6] = 2; printf(course number = %s\n, name); name[7] = 1; name[8] = 3; name[9] = 0; name[10] = \0; // it is very important. printf(course number = %s\n, name); What is the output?

30 External Variables and Scope #include #define MAXLINE 1000 int max; // similar to instance in Java char line[MAXLINE]; char longest[MAXLINE]; int getline(void); void copy(void); main() { int len; extern int max, extern char longest[]; max = 0; while ((len = getline()) > 0) if (len > max) { max = len; copy(); } if (max > 0) /* there was a line */ printf("%s", longest); return 0; } int getline(void) { int c, i; // extern char line[]; // no need to declare for (i = 0; i < MAXLINE – 1 && (c=getchar)) != EOF && c != '\n'; ++i) line[i] = c; if (c == '\n') { line[i] = c; ++i; } line[i] = '\0'; return i; } void copy(void) { int i; // extern char line[], longest[]; i = 0; while ((longest[i] = line[i]) != '\0') ++i; } In the same file

31 TRU-COMP2130 C Programming31 #include … … #define … … // declaration of global variables … // declaration (i.e., definitions) of functions … // implementation of functions { … } Any identifier declared early is visible in the same block or sub-blocks. Visible to every function in the file

32 TRU-COMP2130 C Programming32 2. Types, Operators, Expressions

33 TRU-COMP2130 C Programming33 Data Types and Sizes Primitive data types CJavaSize char, unsigned charbyte1B char in Java uses 2Bs. short, unsigned shortshort2Bs int, unsigned intint4Bs long, unsigned longlong8Bs // there is no unsigned in Java floatfloat4Bs doubledouble8Bs // boolean?TRUE: any non-zero value, boolean in Java FALSE: zero // string?...: with \0 at the end String in Java sizeof(data_type) or sizeof(variable) gives the number of bytes used.

34 TRU-COMP2130 C Programming34 Constants int 1234, long L double 123.4, 1e-3 float 123.4F printf() format specifiers %u unsigned integers %l long integers %lu unsigned long integers %e float, double

35 TRU-COMP2130 C Programming35 Operators Arithmetic operators = assignment + addition ++ increment by one; sometimes confusing - subtraction -- decrement by one; sometimes confusing * multiplication / division; integer division % modulo Relational operators > >= < <=

36 TRU-COMP2130 C Programming36 Boolean operators == != && ANDnot bitwise AND || ORnot bitwise OR Reference operators– will be discussed later.

37 TRU-COMP2130 C Programming37 Bitwise Operators | bitwise OR & bitwise AND ^ XOR ~ 1s complement << shift left >> arithmetic shift right (the left most bit will be copied.) Examples Exercise 2-6 setbits(x, p, n, y) Exercise 2-7 invert(x, p, n) Exercise 2-8 rightrot(x, n) IPv4 address conversion Check if a target bit in an integer is 1. Get the first three bytes in an integer. …

38 TRU-COMP2130 C Programming38 3. Control Flow Statement …; Block, or compound statement { … } The same control structures as Java if, … for, while, do-while switch break, continue You must know them all already.

39 TRU-COMP2130 C Programming39 Goto and Labels for (i = 0; i < n; i++) for (j = 0; j < m; j++) if (a[i] == b[j]) goto found; /* didn't find any common element */... found: /* got one: a[i] == b[j] */... Better not use goto statements Can you convert the above code so that you would not use goto ?

40 TRU-COMP2130 C Programming40 4. Functions and Program Structure Function in C is the same as method in Java. return-type function-name(argument declarations) Various parts may be absent.

41 TRU-COMP2130 C Programming41 External Variables If a large number of variables must be shared among functions, external variables (or also called global variables) are more convenient and efficient than long argument lists. External variables are declared outside of any function, usually with initial values. Automatic variables (local variables and parameters) are internal to a function; they come into existence when the function is entered, and disappear when it is left. External variables, on the other hand, are permanent, so they can retain values from one function invocation to the next. Thus if two functions must share some data, yet neither calls the other, it is often most convenient if the shared data is kept in external variables rather than being passed in and out via arguments.

42 TRU-COMP2130 C Programming42 Scope Rules The scope of a name is the part of the program within which the name can be used. The scope of an external variable or a function lasts from the point at which it is declared to the end of the file being compiled. main() {... } int sp = 0; double val[MAXVAL]; void push(double f) {... } double pop(void) {... } Is sp visible in push() ? Can sp and pop() be used in main() ?

43 TRU-COMP2130 C Programming43 Can we make external variables declared in file1 be used in file2? in file1: extern int sp; extern double val[]; void push(double f) {... } double pop(void) {... } in file2: int sp = 0; double val[MAXVAL];

44 TRU-COMP2130 C Programming44 Static Variables The static declaration, applied to an external variable or function, limits the scope of that object to the rest of the source file being compiled. External static thus provides a way to hide names from other files. static char buf[BUFSIZE]; // only in this file static int bufp = 0; // only in this file int getch(void) {... } void ungetch(int c) {... } Static in Java has a bit different meaning.

45 TRU-COMP2130 C Programming45 Register Variables A register declaration advises the compiler that the variable in question will be heavily used. The idea is that register variables are to be placed in machine registers, which may result in smaller and faster programs. But compilers are free to ignore the advice. register int x; register char c; f(register unsigned m, register long n) { register int i;... } Usually for index variables used in loop structures

46 TRU-COMP2130 C Programming46 Initialization Very similar to Java In the absence of explicit initialization, external and static variables are guaranteed to be initialized to zero; but automatic and register variables have undefined (i.e., garbage) initial values.

47 TRU-COMP2130 C Programming47 The C Preprocessor File inclusion #include filename // from the working dir or #include // from the standard dir Macro substitution #define name replacement_text #define max(A, B) ((A) > (B) ? (A) : (B)) Conditional inclusion #if !defined(HDR) // or #ifndef HDR #define HDR /* contents of hdr.h go here */ #endif

48 TRU-COMP2130 C Programming48 #if SYSTEM == SYSV #define HDR "sysv.h" #elif SYSTEM == BSD #define HDR "bsd.h" #elif SYSTEM == MSDOS #define HDR "msdos.h" #else #define HDR "default.h" #endif #include HDR

Download ppt "Intro to C Programming Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University."

Similar presentations

Ads by Google