CS 449 – Executables and Linking

nutmegactΛογισμικό & κατασκευή λογ/κού

11 Νοε 2012 (πριν από 4 χρόνια και 11 μήνες)

292 εμφανίσεις

Welcome to CS 449

Hyunjin

Lee

abraham@cs.pitt.edu

http://www.cs.pitt.edu/~abraham/CS0449/


Course Goals


Introduction to the C programming language


Focus on compare/contrast with Java


Explore how a program is created, stored, and
loaded from disk, then executed in memory


Interact with abstraction layers (interfaces)


Libraries


Operating system


Implement own abstraction layer

Prerequisites


CS401


Java


CS445


Data Structure


Co
-
requisite: CS447


Comp. org. and
assem
.


No CS447 this term, so hopefully everyone’s
already taken it


I will assume expertise in Java


Knowledge of data structures such as linked
lists and stacks

Textbooks


Required textbook:

Oualline
, Steve. Practical C Programming.
O’Reilly, Sebastopol, CA, 1997.

ISBN: 1
-
56592
-
306
-
5


Electronic textbook, PDF available (written by
Jon
Misurda
, a lecturer in the department) on
the course website:

http://www.cs.pitt.edu/~abraham/CS0449/


Class Policies


Two mid
-
term exams, and one non
-
cumulative final exam


All exams closed notes/book


Four projects


two to four weeks between
each


Labs and Quizzes


in recitation; no makeups

Projects


For the projects, you will be developing on
Unix
-
like systems (SunOS and Linux)
maintained by the university, using a
command line


DO NOT develop on Windows


Do not try to use an IDE like NetBeans or
Eclipse


you will just make things harder for
yourself

Grades

First Midterm

15%

Second Midterm

15%

Final Exam

15%

4 Projects

40% (10% each)

Labs and Quizzes

15%

Total

100%


Lowest quiz/lab score will be dropped

Introduction to C

Hello World

#include <stdio.h>


int main(void) {


printf("Hello, world!
\
n");


return 0;

}

Comments


Like in Java, you can insert comments


You should only use the
/*


*/

version;
//

is not always available (it is in C++)


For example:




See chapter 3 in the book for programming
style suggestions; you will be expected to use
reasonable style for your projects and labs



int i = 1; /* set variable i to one */

Basic Program Structure


Our program begins with header includes,
such as
#include <stdio.h>


You may need to include other headers, such as
stdlib.h
, in the future


Every program needs a
main

function, which
begins with:


and ends with:

int main(void) {


return 0;

}

Statements


Just like Java, C executes code as sequences of
statements


Each statement ends in a semicolon


For example,


is a statement composed of a function call,
with a string argument


printf(“Hello World
\
n”);

Expressions


As in Java, expressions are building blocks of
computation


The basic operators (
+
,
-
,
*
,
/
,
%
) and their
order of operations are the same as Java


For example:


is an expression, while


is a statement



(1+2)*3


(1+2)*3;

Variables


As in Java:


variables store values


names start with either a letter or underscore


continue with any number of letters, digits, or
underscores


names are case sensitive


Names are traditionally lowercase (unlike Java)


e.g.,
entry_total

rather than
entryTotal

Variable Declarations


Unlike in Java, all variables must be declared
at the start of a block (for now, the beginning
of the
main

function)






int main(void) {


int i;


int j;


i=2;


return 0;

}

int main(void) {


int i;


i=2;


int j;


return 0;

}

Is legal

Is not legal

Assignment


As in Java, use a single equal sign = to store
the right side in the left side


For example,



As in Java, you can assign a value at time of
declaration:



And there are the same short cuts, e.g.:




total = (1+2)*3; /* store 9 in total */


int total = (1+2)*3; /* store 9 in total */

+=

-
=

*=

/=

++

--

Types


Each variable declaration specifies a type


C includes the following built
-
in types:


Type

Description

Typical size (32
-
bit x86)

char

A single character, or byte

1 byte (8

bits)

int

An integer

4 bytes

long

An integer size >=
int

4 bytes

long
long

An integer

size >
int
, >=
long

8 bytes

short

An integer size <=
int

2 bytes

float

Single
-
precision

floating point value

4

bytes

double

Double
-
precision

floating point value

8 bytes

Integer Representation


Integer values are represented by a specific
number of binary digits


For example,


would be represented by 16
-
bits:



Negative numbers are 2’s compliment:

-
1 =

-
2 =


short total = 27;

0

0

0

0

0

0

0

0

0

0

0

1

1

0

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

0

Unsigned Types


Each integer type has an unsigned variant


e.g.,
unsigned int


The values in these variables are always >= 0


This enables larger numbers to be stored

Characters


A variable of
char

type can store either a
small number, or a single letter/symbol


Letters and symbols are referenced by their
ASCII value (see Appendix A for the values)


To get the value of a letter or symbol, use
single quotes; for example:



char letter_a = 'a' /* store 97 */

Escape Characters


Certain characters are useful, yet cannot be
directly typed in, such as a line break


In C, as in Java, we use a backslash followed
by a letter (or number) to represent those


Char Seq.

Description

\
n

New line
(only on Unix
-
like systems)

\
0

Null character (useful for strings)

\
t

Tab character

\
'

Single
-
quote/apostrophe

\
"

Double
-
quote

\
\

Backslash

printf


printf

is a function defined in the stdlib.h
header file


It can take any number of parameters


The first parameter is always the “format”


In general, the format string is printed as
-
is
(like in the hello world program)


The format can contain fields, which start with
the
%

symbol

printf

fields


Each field acts as a placeholder


The character after the
%

determines the type
of value to expect; c for
char
, and d for digit
(i.e.,
int
)


Each value comes from remaining arguments


order in which they appear in the format


Result from above?


printf("%c: %d
\
n%c: %d", 'A', 3, 'B', 4);

A: 3

B: 4

Comparison Operators


We have the same comparison operators as
Java, e.g.,
==
,
!=
,
<=
,
>=
,
<
,
>


Unlike in Java, there is no
boolean

type


In C, zero (for any integer type) is treated as
false, and any non
-
zero value as true


The comparison operators are guaranteed to
return values consistent with that scheme,
but not necessarily one for true

Boolean Operators


We have the same logical operators as Java,
e.g., !, &&, ||


These work as expected with the same
true/false scheme as the comparison
operators


These operators “short
-
circuit”, as in Java


If the result of the left
-
side determines the
ultimate result, the right
-
side is not executed


1 == 2 && printf("wont't get printed
\
n");

Bit
-
wise Operators


We can operate on the bit level of integers
using bit
-
wise operators


These same operators are also in Java, but less
often used

Op

Description

&

Bit
-
wise AND

|

Bit
-
wise OR

^

Bit
-
wise XOR

<<

Shift left

>>

Shift right

~

Invert bits

Bit
-
wise AND


The bits of each side are ANDed together:


18 & 17

0

0

0

0

0

0

0

0

0

0

0

1

0

0

1

0

0

0

0

0

0

0

0

0

0

0

0

1

0

0

0

1

0

0

0

0

0

0

0

0

0

0

0

1

0

0

0

0

18

& 17

16

Bit
-
wise OR


The bits of each side are ORed together:


18 & 17

0

0

0

0

0

0

0

0

0

0

0

1

0

0

1

0

0

0

0

0

0

0

0

0

0

0

0

1

0

0

0

1

0

0

0

0

0

0

0

0

0

0

0

1

0

0

1

1

18

& 17

19

Bit
-
wise XOR


The bits of each side are XORed together:


18 & 17

0

0

0

0

0

0

0

0

0

0

0

1

0

0

1

0

0

0

0

0

0

0

0

0

0

0

0

1

0

0

0

1

0

0

0

0

0

0

0

0

0

0

0

0

0

0

1

1

18

& 17

3

Left shift


The bits of the left side are shifted to the left
by the amount on the right side:






Note that zeros are added on the right


18 << 3

0

0

0

0

0

0

0

0

0

0

0

1

0

0

1

0

0

0

0

0

0

0

0

0

1

0

0

1

0

0

0

0

18

<< 3

Right shift


The bits of the left side are shifted to the right
by the amount on the right side:






Note that zeros are added on the left


18 >> 1

0

0

0

0

0

0

0

0

0

0

0

1

0

0

1

0

0

0

0

0

0

0

0

0

0

0

0

0

1

0

0

1

18

>> 1

0

0

0

0

0

0

0

0

0

0

0

0

0

1

0

0

>> 2

Bit inversion


All of the bits are inverted:


~18

0

0

0

0

0

0

0

0

0

0

0

1

0

0

1

0

1

1

1

1

1

1

1

1

1

1

1

0

1

1

0

1

18

~18