COMP 2130 Intro Computer Systems Thompson Rivers University

Slides:



Advertisements
Similar presentations
C: Advanced Topics-II Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
Advertisements

Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Kernighan/Ritchie: Kelley/Pohl:
Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Computer Science 210 Computer Organization Introduction to C.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Characters and Strings File Processing Exercise C Programming:Part 3.
Lecture 1 cis208 January 14 rd, Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
Introduction to C For the participants of Practical Course „Scientific Computing and Visualization“
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Computer Organization and Design Pointers, Arrays and Strings in C
‘C’ Programming Structures and Commands
The Machine Model Memory
Computer Science 210 Computer Organization
C Primer.
A bit of C programming Lecture 3 Uli Raich.
C Characters and Strings
Tokens in C Keywords Identifiers Constants
Functions, Part 2 of 2 Topics Functions That Return a Value
C Programming Tutorial – Part I
Programming Languages and Paradigms
INC 161 , CPE 100 Computer Programming
Quiz 11/15/16 – C functions, arrays and strings
An Introduction to C Programming
C Short Overview Lembit Jürimägi.
C programming language
Programming Paradigms
C Basics.
C Programming:Part 3 Characters and Strings File Processing Exercise.
Arrays in C.
Introduction to C Programming
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Programming in C Input / Output.
11/10/2018.
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Lexical Elements, Operators, and the C Cystem
C Stuff CS 2308.
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
Lexical Elements, Operators, and the C Cystem
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Computing Science Thompson Rivers University
Functions, Part 2 of 3 Topics Functions That Return a Value
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Lectures on Numerical Methods
File Input and Output.
Chapter: 7-12 Final exam review.
Chapter 2 - Introduction to C Programming
Programming in C Input / Output.
Chapter 2 - Introduction to C Programming
Chapter 11 Programming in C
Strings #include <stdio.h>
Programming Languages and Paradigms
C Language B. DHIVYA 17PCA140 II MCA.
Introduction to C Programming
C Characters and Strings
Functions, Part 2 of 3 Topics Functions That Return a Value
15213 C Primer 17 September 2002.
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

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

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

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

Unit Contents References Basic C http://www.java2s.com/Code/C/CatalogC.htm Basic C Primitive Data Types, Operators, and printf() ... TRU-COMP2130 C Programming

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

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

Basic C # include <stdio.h> // import statements in Java # include <stdlib.h> # define PI 3.141953 // 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

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 * 3.1416 * 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

#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

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

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

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

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