How to use libraries?
E.g., I want to use
READ the documentation, but typically it goes like this:
(1) #include "/home/students/alice/libs/libcool.h"
May need to tell the compiler where this is
(2) compile as usual
(3) need to add the libr
Variables in C
- naming rules are the same as Java
- no reserved words
- don't start with a digit
- can contain letters, digits, _
- are case sensitive
chose and be consistent
Pointers in C
* In C we can declare pointer variables
* Declare as:
(type to define as) * p;
* I.e., declare just like you would any other variable, except
add the "death star"
* This means:
p stores an address (in memory) of an
How to read and write files in C
You need this yesterday but don't know enough to learn it properly
so we will gloss over lots.
All of these functions (and wait there's more) live in <stdio.h>
(1) declare a variable of type FILE*
What's the star? Just y
Basic types in C
int "integer types"
Fun with integer types:
(1) You can preface any integer type with "signed" or "unsigned"
signed char y;
unsigned char z;
signed int i;
unsigned int u;
(2) The type "int" m
How does fprintf work?
fprintf(FILE*, format string, stuff);
There are 3 standard files in C
meaning standard "FILE* things"
(0) stdin - standard input
(1) stdout - standard output
(2) stderr - standard error
fprintf(stdout, "foo", bar, cruft, land);
* There is a GUI for UNIX!
* We won't cover the GUI in class (darn)
* This primer: how to use a shell
What is a shell?
- program to parse and execute commands
(written in C)
- AKA "command line"
- usually run interactively
get the promp
Writing your own functions in C
public int foo(args)
Basically the same
<return type> funcname(zero or more parameters)
/* statements */
<return type> : what the function returns
- can be some type
- can say "return 4; "
Operators in C
Short story - same as Java ?
> < >= <= = !=
what is the type of "x < 4" in Java? boolean.
what is the type of "x < 4" in C?
- will be 1 if true, 0 if false
+ - * / %
What is the type of int / int
October 3 - Preprocessor and Linked lists in C
Thursday, October 3, 2013
More fun with C preprocessor
#define FOO 3.141
Other fun things we can do:
Helpful way to debug code:
/ Normal C code h
What we know so far about pointers in C:
(0) Declaring them:
TYPE * var;
declares a variable that is a pointer to something of type TYPE.
A pointer is an "address in RAM" of something.
(1) How to get the address of something in C:
October 8 - Curses
Tuesday, October 8, 2013
For part 3, use a library called 'ncurses' to help implement a user interface
For more information:
For more information on lecture notes and tutorials
October 1 - Pointer Casting
Tuesday, October 1, 2013
1. Project 1 test posted : ~cs229/public/tests.1.1/
2. Next 3 weeks:
a. Part 2 due next week
b. Mid term the week after
c. Part 3 due
Int I = 0x000
How does the C compiler work?
What are the "stages" of the compiler?
(stage 1): Preprocessor
Lines that start with "#" are preprocessor directives
(i) #include "file" or
"Insert the contents of that file, here"
Introduction to C
(2) Legacy code
(3) Speed (less true)
(4) Fun with memory
(5) Today - some applications are still written in C
Linux kernel - C and assembly
Windows - C, C+, assembly
Most Unix stuff (ls, cp, bash)