Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 2130 Intro Computer Systems Thompson Rivers University

Similar presentations


Presentation on theme: "COMP 2130 Intro Computer Systems Thompson Rivers University"— Presentation transcript:

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

2 Course Contents Part I – Review Part II – C Programming Language
Mumber Systems Boolean Logic Digital Logic Part II – C Programming Language Part III – Introduction to Computer Systems Not the replacement of IPv6; Inter-transition mechanism; But if IPv6 would fail TRU-COMP2130 C Programming

3 Unit Learning Objectives
Use a char array as a string. Use printf() function to print messages. Use pointers and references. Use pointers for dynamic memory management. Use open(), read(), write(), close(), and FILE* functions to manipulate files. Use user-defined data structures. Use bitwise operators. ... TRU-COMP2130 C Programming

4 Unit Contents References Basic C
Basic C Primitive Data Types, Operators, and printf() ... TRU-COMP2130 C Programming

5 Why C? Why C? Pros and cons How is C different from Java?
Most system programs are written in C, not even C++, for fast execution. The kernels of most operating systems are written in C. Pros and cons Fast execution \ Easy to handle memory - > Good for system programming Bit operation / But complex concepts of pointer, type conversion and memory allocation How is C different from Java? TRU-COMP2130 C Programming

6 How is C different from Java?
C Java Procedural No class Common data: global variables Abstract data type: struct, union Object oriented Class Common data: instance variables Abstract data type: class Micro approach Individual utility libraries Macro approach Utilities include language itself Reference type variable No reference, but objects include the concept Call by value; call by reference Call by value; call by reference for objects Compiling One file at a time, and linking Cross-reference TRU-COMP2130 C Programming

7 Basic C # include <stdio.h> // import statements in Java # include <stdlib.h> # define PI // final instance variables in Java // global variables // instance variables in Java int level = 0; // functions // methods in Java void prt(char[] s) { printf(“%s”, s); printf(“This is a level-%d programming course.”, level); } // main() // main method in Java int main(int argc, char* argv[]) level = 2; prt(“Welcome to C Programming\n”); TRU-COMP2130 C Programming

8 Look at the programming style!
// program1-1.c // Definitions including header files and functions #include <stdio.h> // Declarations of global variables // Functions int main() { // Declarations of local variables double radius, circumference; /* declare an input and output items */ // codes radius = 2.0; /* set a value for the radius */ circumference = 2.0 * * radius; /* calculate the circumference */ printf("The circumference of the circle is %f\n", circumference); // What is printf(...)? // What is %f? // printf("...", ...); // return statements return 0; } Look at the programming style! TRU-COMP2130 C Programming

9 How to compile? How to execute? How to find manuals?
$ gcc program1-1.c It will create a.out. $ gcc –o prgm program1-1.c It will create prgm. How to execute? $ ./a.out $ ./... How to find manuals? $ man –s 3 printf $ man –s 3 scanf $ man –s 3 rand ... TRU-COMP2130 C Programming

10 Data Types, Operators and printf()
Primitive data types char unsigned char byte in Java short unsigned short short char in Java uses 2Bs. int unsigned int int long unsigned long long no unsigned in Java float float double double // boolean? TRUE: any non-zero value, boolean in Java FALSE: zero // string? “...” String sizeof(data_type) or sizeof(variable) gives the number of bytes used. TRU-COMP2130 C Programming

11 Arithmetic operations
= assignment + addition ++ increment by one - subtraction -- decrement by one * multiplication / division % modulo Boolean operations && AND not bitwise AND || OR not bitwise OR Bitwise operations – will be discussed later Reference operations – will be discussed later TRU-COMP2130 C Programming

12 printf() formats in C System.out.print() in Java
printf(“...”, ...); // format, and variables // the number of variables must be equal to the % formaters and // must have the same data types %s string, i.e., char[] having an element of ‘\0’ as the end specifier %c character; char type %d; %i integer; char, short, int, long type %3d 3 places for the integer %u integer (unsigned) – decimal; char, short, int, long type %x; %X integer (unsigned) – hexadecimal; char, short, int, long type %f float, double type %5.2f 5 places for mantissa, 2 places for fraction %e; %E exponential form; float, double type \n, \t, \\, \”, %% newline, tab, \, ”, % int t = 10; printf(“Hi %c %d %s\n”, ‘a’, t, “there!”); TRU-COMP2130 C Programming

13 Examples program2-1.c program2-2.c program2-3.c program2-4.c
TRU-COMP2130 C Programming

14 math.h Some MATH related functions # include <math.h>
double sqrt(double) double pow(double, double) double fabs(double) Link with –lm -lm means libm.a, that contains math utilities, is used $ gcc program3-5.c –lm Examples program3-5.c program3-6.c TRU-COMP2130 C Programming

15 Reading User Inputs Some input functions that read data from keyboard
First, a reference operator and the related format specifier &any_variable the addresss of the variable in the main memory int addr = 10; char c = ‘A’; double x = 34.59; printf(“%d, %p\n”, addr, &addr); // %p for address printf(“%c, %p\n”, c, &c); printf(“%f, %p\n”, x, &x); printf(“%d, %d\n”, sizeof(addr), sizeof(&addr)); Examples program2-13.c TRU-COMP2130 C Programming

16 scanf(“...”, ...) “format”, address of a variable
int size; scanf(“%d”, &size); // why &size ??? char c; scanf(“%c”, &c); // why &c ??? Examples program3-9.c program3-10.c program3-11.c program3-12.c program3-18.c program3-19.c char getchar(); get a character char c = getchar(); program3-10.c // interesting example of character inputs TRU-COMP2130 C Programming

17 Control Structures if How to use Boolean values or expressions?
if-else if-else-if switch-case Examples program4-1.c program4-2.c program4-3.c program4-4.c program4-5.c program4-6.c switch-case program4-7.c program4-8.c program4-9.c TRU-COMP2130 C Programming

18 for while do-while Examples program5-1.c ... program5-20.c
TRU-COMP2130 C Programming

19 Functions What is a function? Why do you need to use functions?
Pass by value When a varible is passed to a function, the content of the variable is passed, not the variable itself. Examples program6-1.c function prototypes; call by value program6-2.c scope program6-3.c program6-4.c return value from a function how to return multiple values? program6-5.c program6-6.c use of multiple functions program6-7.c program6-8.c use of time() and random numbers program6-9.c function prototypes as global variables program6-10.c handling characters program6-11.c handling strings – atoi(), atof() TRU-COMP2130 C Programming

20 Scope Rules Global variables and local variables
Global variables declared outside any function are visible in all functions and shared. Local variables declared inside a function (or a block using ‘{‘ and ‘}’) are visible only in the function. Examples program7-1.c local and global scope program7-2.c how to keep a value in the next invocation? program7-3.c static local variables Is it a good idea? Let’s make a function to swap two values. Case 1: the swap function in the same file with main() Case 2: the swap function in another file $ gcc file_1.c file_2.c TRU-COMP2130 C Programming

21 Pointers Pointers are not supported by Java Reference operations
A pointer type variable can hold the address of a varible of the same data type. Declaration of pointer type variables int* test; char *string; ... // any data type How to obtain the address of a variable? ... = &num; TRU-COMP2130 C Programming

22 Examples TRU-COMP2130 int *test; int number = 20; test = &number;
printf(“%d, %d, %p, %p\n”, number, *test, test, &test); // the content pointed by test // the content of test *test = 30; // into the memory pointed by test printf(“Enter an integer: ”); scanf(“%d”, &number); // the address of number printf(“%d, %d, %d, %p, %p, %p\n”, number, *test, *(&number), test, &test, &number); // the content pointed by test // the content pointed // by the address of number // the address of test // the address of number TRU-COMP2130 C Programming

23 Examples program7-4.c program7-5.c program7-11.c recursion
TRU-COMP2130 C Programming

24 Pass by reference Function prototypes Function declarations
void newval(float*); Function declarations void newval(float* xnum) { *xnum = 5; ... } Example of function invocation float* x; float y; newval(x); newval(&y); Examples program7-6.c program7-7.c program7-8.c return of two values by using references program7-10.c swap() TRU-COMP2130 C Programming

25 Arrays Declaration and initialization Passing array variables
program8-2.c program8-3.c program8-7.c 2-D array program8-8.c Passing array variables program8-4.c the size of an array is defined in function spec program8-5.c the size of an array is passed as a parameter program8-6.c program8-9.c program8-11.c liear search program8-14.c bubble sorting; array passing as reference program8-12.c binary search TRU-COMP2130 C Programming

26 Array and reference Examples int number[10];
printf(“%p\n”, &(number[0])); printf(“%p\n”, &(number[5])); printf(“%p\n”, number); // related to reference printf(“%p\n”, number + 5); // the address of number[5] // not 5 * 4 newval(number); void newval(int num[]) { num[0] = 5; ... }, or void newval(int* num) { *num = 5; num[1] = 10; ... } Examples program8-1.c program8-10.c TRU-COMP2130 C Programming

27 Arrays and pointers are well related.
int number[10]; int *p, *q; *p = 10; // Is it wrong? p = number; q = &number[0]; number[0] = 2; number[1] = 9; number[2] = 5; printf(“%p, %p\n”, p, q); printf(“%d, %d, %d, %d\n”, *(p+1), p[1], *(q+1), q[1]); Examples program11-2.c increment of a pointer value program11-3.c array as a reference program11-4.c increment of a pointer value; array as a reference program11-5.c program11-7.c char* program11-8.c array of pointers program11-9.c Arrays and pointers are well related. TRU-COMP2130 C Programming

28 Do we really need to use pointers? Dynamic memory management
#include <stdlib.h> void *malloc(int size); // allocate size bytes // and return the addr void free(void *); // free the memory space Let’s make a dynamic queue. TRU-COMP2130 C Programming

29 String sprintf(tmp, “course name is %s.”, name); TRU-COMP2130
char name[256], tmp[256]; name[0] = ‘C’; name[1] = ‘O’; name[2] = ‘M’; name[3] = ‘P’; name[4] = ‘\0’; // it is very important. name[5] = ‘ ’; name[6] = ‘2’; name[7] = ‘1’; name[8] = ‘3’; name[9] = ‘0’; name[10] = ‘\0’; // it is very important. printf(“course number = %s\n”, name); printf(“%p\n”, name); printf(“course number = %s\n”, &(name[5])); scanf(“%s”, name); sprintf(tmp, “course name is %s.”, name); TRU-COMP2130 C Programming

30 #include <string.h.> Examples
program9-1.c gets(), puts() program9-2.c stringcopy() program9-3.c getchar() program9-4.c getline() program9-5.c strcpy(), strlen(), strcmp(), strcat() program9-6.c toupper() program9-7.c program9-8.c program9-9.c program9-10.c program9-11.c TRU-COMP2130 C Programming

31 Files Example: Let’s make a file copy program. TRU-COMP2130
#include <stdio.h> FILE *in, *out; in = fopen(“in_filename”, “r”); // mode: r, w, a, r+, w+, a+ if (in == NULL) ... out = fopen(“out_filename”, “w”); fclose(in); fprintf(out, “format ...”, variables...); fscanf(...); fgets(...); int fseek(FILE*, long, [SEEK_SET|SEEK_CURRENT|SEEK_END); move file position pointer int fwrite(char*, int size, int nmemb, FILE*); int fread(char*, int size, int nmemb, FILE*); fputc(...); fgetc(...); Example: Let’s make a file copy program. TRU-COMP2130 C Programming

32 Examples program10-1.c <stdlib.h>, exit(), FILE*, fopen(), NULL
program10-2.c fopen() program10-3a.c program10-3b.c program10-4.c fprintf(), fclose() program10-5.c fscanf(), EOF program10-6.c fgets() // get a line from a FILE program10-7.c fseek() program10-8.c passing FILE* program10-9.c returning FILE* program10-10.c program10-11.c program10-12.c fwrite() program10-13.c fread() program10-14.c fputc(), fgetc() TRU-COMP2130 C Programming

33 Structures User-defined data structure
struct struct_name { // class without methods in Java data_type member1; int num; ... }; struct struct_name test; // how to declare a struct variable test.num = 10; // how to access a member Examples program12-1.c struct {...} program12-2.c struct Date {...} program12-3.c array of structs program12-4.c passing a struct program12-5.c struct pointer program12-6.c malloc(), free() Let’s make a dynamic queue. TRU-COMP2130 C Programming

34 Bitwise Operations Examples | bitwise OR & bitwise AND ^ XOR ~ NOT
<< shift left >> shift right Examples program14-1.c & program14-2.c |, ^, ~, <<, >> program14-3.c char* argv[], char** program14-4.c ipv4addr.c IPv4 address conversion Assignments Check if a target bit in an integer is 1. Get the first three bytes in an integer. TRU-COMP2130 C Programming


Download ppt "COMP 2130 Intro Computer Systems Thompson Rivers University"

Similar presentations


Ads by Google