Download Computing Science - Thompson Rivers University

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Java performance wikipedia , lookup

Join-pattern wikipedia , lookup

String literal wikipedia , lookup

C Sharp syntax wikipedia , lookup

Scala (programming language) wikipedia , lookup

Stream processing wikipedia , lookup

Logic programming wikipedia , lookup

ALGOL 68 wikipedia , lookup

Programming language wikipedia , lookup

Control flow wikipedia , lookup

Abstraction (computer science) wikipedia , lookup

Subroutine wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Standard ML wikipedia , lookup

Object-oriented programming wikipedia , lookup

Functional programming wikipedia , lookup

Corecursion wikipedia , lookup

Go (programming language) wikipedia , lookup

Reactive programming wikipedia , lookup

C++ wikipedia , lookup

Structured programming wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

C syntax wikipedia , lookup

Transcript
Intro to C Programming
Winter 2013
COMP 2130 Intro Computer Systems
Computing Science
Thompson Rivers University
Course Objectives

The better knowledge of computer systems, the better programing.
Computer System
C Programming Language
Computer architecture
CPU (Central Processing Unit)
IA32 assembly language
Introduction to C language
Physical main memory
MMU (Memory Management Unit)
Virtual memory space
Memory hierarchy
Cache
Dynamic memory management
Compiling, linking, loading,
executing
Better coding – locality
Reliable and efficient programming for power programmers
(to avoid strange errors, to optimize codes, to avoid security holes, …)
TRU-COMP2130
C Programming
2
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.1 – 9.9.2, 9.9.4 –
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.1 –
6.4.2, 6.5, 6.6.2 – 6.6.3, 6.7
Virtual memory (if time permits): B&O 9.4 – 9.5
TRU-COMP2130
C Programming
3
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, … .
…
TRU-COMP2130
C Programming
4
Unit Contents




Introduction
Types, Operators, and Expressions
Control Flow
Functions and Program Structure
TRU-COMP2130
C Programming
5
1. Introduction

Only the topics that are different from Java will be discussed.
TRU-COMP2130
C Programming
6
Getting Started

C program files should end with “.c”.

hello.c
#include <stdio.h>
// 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
TRU-COMP2130
C Programming
7
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)

TRU-COMP2130
C Programming
8
#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;
}
}
TRU-COMP2130
C Programming
9
printf("%d\t%d\n", fahr, celsius);


%d specifies an integer argument (d: decimal).
Output
1 -17
20-6
404
...

Can we print better? Like
1 -17
20 -6
40
4
...

printf("%3d\t%6d\n", fahr, celsius);
TRU-COMP2130
C Programming
10
1 -17
20 -6
40
4
...

Is there any problem in the above output?
The Celsius temperatures in the previous out are not accurate. For example
0o F is -17.8o 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.
TRU-COMP2130
C Programming
11

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);
TRU-COMP2130
C Programming
???
???
???
12

Summary of printf() format specifiers







%d
print as decimal integer
%6d
print as decimal integer, at least 6 characters wide
%f
print as floating point
%6f
print as floating point, at least 6 characters wide
%.2f
print as floating point, 2 characters after decimal point
%6.2f
print as floating point, at least 6 characters wide and 2 after
decimal point
Other format specifiers





%o
%x
%c
%s
%%
TRU-COMP2130
for octal
for hexadecimal
for character
for character string
% itself
C Programming
13
Symbolic Constants
#define name replacement_list
#include <...>
#define LOWER 0
#define UPPER 300
#define STEP 20
// similar to final variable in Java
main()
{
float fahr, celsius;
for (fahr = ... ; fahr <= ... ; fahr = fahr +
celsius = 5 * (fahr-32) / 9;
printf("%3.0f\t%6.1f\n", fahr, celsius);
}
...) {
}
TRU-COMP2130
C Programming
14
Symbolic Constants
#define name replacement_list
#include <stdio.h>
#define LOWER 0
#define UPPER 300
#define STEP 20
// similar to final variable in Java
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);
}
}
TRU-COMP2130
C Programming
15
Character Input and Output
#include <stdio.h>
/* copy input to output; 1st version */
main()
{
int c;
c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
//
//
//
//
in order to read a character
EOF is defined in stdio.h.
EOF means End of File, ^D.
in order to print a character
}
}


Where did getchar(), EOF, and putchar() come from?
Let’s run this program to understand I/O better.
TRU-COMP2130
C Programming
16
#include <stdio.h>
/* copy input to output; 2nd version */
main()
{
int c;
while ((c = getchar()) != EOF)
putchar(c);
// okay?
}

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 =.
TRU-COMP2130
C Programming
17

Operator precedence rules
()
!
*
+
<<
<
==
&
^
|
&&
||
=
[]
->
.
~
- (unary)
/
%
>>
<=
>
>=
!=
+=
TRU-COMP2130
-=
*=
+ (unary)
/=
%=
*
&=
C Programming
&
|=
sizeof
^=
++
<<=
--
>>=
18
Boolean values?

Syntax error?
while (c = getchar() != EOF)







Let’s 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.
TRU-COMP2130
C Programming
19
Arrays
#include <stdio.h>
/* count digits, white space, others */
main()
{
int c, i, nwhite, nother, ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; ++i)
ndigit[i] = 0;
// very similar to Java
// really necessary?
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);
}
TRU-COMP2130
C Programming
20


A character, e.g., ‘3’, is an integer value. int c = ‘3’; is valid.
ASCII table
TRU-COMP2130
C Programming
21
Functions
#include <stdio.h>
int power(int m, int n);
// function prototype
// 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?
TRU-COMP2130
C Programming
22
...
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;
}
TRU-COMP2130
C Programming
23
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.

TRU-COMP2130
C Programming
24
Character Arrays
In the same file
#include <stdio.h>
#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);
}
int getline(char s[], int lim)
{
int c, i;
for (i=0; i<lim-1 && (c=getchar())!=EOF &&
c!='\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0'; // “...’\0’”
return i;
}
void copy(char to[], char from[])
{
int i;
i = 0;
while ((to[i] = from[i]) != '\0')
++i;
if (max > 0) printf("%s", longest);
return 0;
}
...
}

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?

Main memory

addr
value
var
…
…
…


def
abc
line

…
…
…

abc
‘C’
line[0]
abc+1
‘O’
line[1]
…
…
…
TRU-COMP2130
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.
C Programming
26

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.
TRU-COMP2130
C Programming
27

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’.

TRU-COMP2130
C Programming
28
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?
TRU-COMP2130
C Programming
29
External Variables and Scope
#include <stdio.h>
In the same file
#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;
}
#include …
…
#define …
…
// declaration of global variables
…
// declaration (i.e., definitions) of functions
…
// implementation of functions
{
…
}

Visible to every
function in the file
Any identifier declared early is visible in the same block or sub-blocks.
TRU-COMP2130
C Programming
31
2. Types, Operators, Expressions
TRU-COMP2130
C Programming
32
Data Types and Sizes

Primitive data types
C
char, unsigned char
Java
Size
byte
1B
char in Java uses 2Bs.
short, unsigned short
short
2Bs
int, unsigned int
int
4Bs
long, unsigned long
long
8Bs
// there is no unsigned in Java
float
float
4Bs
double
double
8Bs
// 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.
TRU-COMP2130
C Programming
33
Constants





int
1234,
long
12345678L
double
123.4, 1e-3
float
123.4F
printf() format specifiers




%u
%l
%lu
%e
TRU-COMP2130
unsigned integers
long integers
unsigned long integers
float, double
C Programming
34
Operators

Arithmetic operators
=
+
++
-*
/
%

assignment
addition
increment by one; sometimes confusing
subtraction
decrement by one; sometimes confusing
multiplication
division; integer division
modulo
Relational operators
>
>=
<
<=
TRU-COMP2130
C Programming
35

Boolean operators
==
!=
&&
||

AND
OR
not bitwise AND
not bitwise OR
Reference operators– will be discussed later.
TRU-COMP2130
C Programming
36
Bitwise Operators
|
&
^
~
<<
>>

bitwise OR
bitwise AND
XOR
1’s complement
shift left
arithmetic shift right (the left most bit will be copied.)
Examples







TRU-COMP2130
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.
…
C Programming
37
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.
TRU-COMP2130
C Programming
38
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?
TRU-COMP2130
C Programming
39
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.

TRU-COMP2130
C Programming
40
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.
TRU-COMP2130
C Programming
41
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()?
TRU-COMP2130
C Programming
42

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];
TRU-COMP2130
C Programming
43
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.
TRU-COMP2130
C Programming
44
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
TRU-COMP2130
C Programming
45
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.
TRU-COMP2130
C Programming
46
The C Preprocessor

File inclusion
#include “filename”
// from the working dir
#include <filename>
// from the standard dir
or

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
TRU-COMP2130
C Programming
47
#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
TRU-COMP2130
C Programming
48