Basic language features in c
- arithmetic
+ - * / % ++ --
- relational
== != < > <= >=
- bitwise
~ & | ^ >> <<
- logical
&& || !
- conditional
?:
- assignment
= += -+ *= /= %=
- arity (unary,binary, ternary)
- precedence
- associativity: order in which orperators of the same predecence execute
- expression: sequence of operators that resolves to a single value
- statement: expression terminated by a semi-colon
- block: sequence of statements between a pair of matches braces { }
- scope: part of the program where a variable can be used:
- local: inside a block
- global: outside blocks
- conditional - if/else
- selective - switch statement
- iterative - for, while, do-while (segfaults often)
- jump - break, continue
- you need to document sideeffects of a function: unintended/unexpected
- do not document every line of code or i will CHRISTINE you
- 2.1. primitives DTs
- 2.2. compound DTs
- 2.3. pointers
-
bit: (0|1)
-
byte: 8 consecutive bits
-
nibble: 4 consecutive bits; maps to one hexadecimal digit
-
base conversion:
- binary base 2
- octal base 8
- hexadecimal base 16
- unsigned non-negative whole numbers
- each bit represents non-negative integer power of 2
- 8 bits → 2^8 ⇒ 256 max val
- least significant bits → lowest powers of 2 ("lower order bits")
- most significant bits → highest powers of 2 ("higher order bits")
1. unsigned char is 1 byte
2. unsigned int is 4 bytes (on 32bit arch)
3. unsigned short int is 2 bytess
- in c {int, char} is always signed int
- positive integers and zero - use magnitude-only bit model
- negative integers: write bits for positive number; invert; add 1
-
fixed point: NOT USED ANYMORE
- number of digits in fraction are fixed
- inefficient use of avialable bits
- fine for fraction powers of 2 but imprecise for powers of 10
-
floating point:
- format: +/- 1.f x 2^e
- two's complement: only applies when you put in a negative number
- to reverse, run the procedure again
float
- 4 bytes
double
- 8 bytes
- why? store data that is only one bit long; modify
- smallest c data type is 8 bits, so if we have single-bit data, we waste 7 bits
- bitwise ops allow using those 7 bits
- can use bitwise ops on literals and vars
- bit-shift operators are not 'circular'
<< 3
left shift by 3 (adding zeros to end)>> 2
right shift by 2- (if twos complement, adding sign bit to beginning)
- (if otherwise, adding zeros to begninng )
THESE ARE THE ONLY MEMORY ALLOCATION FUNCTIONS YOU MAY USE FOR THIS COURSE. OR ELSE I WILL CRY. IAMPROF.
void \*malloc(size_t size)
where size is a number of bytes (duh)- returns the start memory address reserved
- if you don't store this pointer, how in the hell ya gonna free the memory, honey?
- in stdlib.h
- clears memory first, allocs a number of elements
- mallocs
- in stdlib.h
- both malloc and calloc return the pointer to void
- void means: no datatype || any data type
- outside scope of course
- usually used on pointers
- can be used on non-pointer vars
- never contains function implementations
- never includes .c files
-
preprocessor
-
compile
-
link
-
gcc -c : we will use -c from now on
-
gcc *.o -o bin
- different programming languages
- compilation may be slow
source → compiler → object files → linking → binary executable
- include the header file
- link in the object file
- remember: libc.a is always linked in by default
- dynamic - default - runtime as needed - smaller exe but slower execution time
- static - library code is copied into exe - larger exe but faster exe time
- a program with more than one control flow
- a large system can be:
- multithreaded
- multiprocess
- distributed
- !== processes
- share resources with process tree
- execution over multiple physical host
- each host has different resources (fs, processor...)
- hosts msust be networked togeter in order to commnicate
- either by intranet or internet
- why?
- users are in different physical locations
- server hosts are in different physical locatoins
- single host has insufficient resources/processor power
- can be DIFFERENT exe OR MULTIPLE instance of same exe
- why?
- system has different tasks to perform
- tasks are independent
- tasks use different resources
- process with multiple control flows
- share the process (virtual memeory, address space, resources)
- different threads may need to synchronize
- why?
- different things to do within a process
- tasks ~dependent upon each other
- example: one thread blocks and waits for user input
- launching, killing, signals
- process id (PID)
- parent process id - PID of process that started this process
- usually managed by OS; can be: shell, program
ps -ef
- clone of itself
- new process is the child
- child gets an instantaneous copy of the parent's address space
- morphing itself
- char *args = {processname,arg1,arg2};
- execvp(buffer, args);
- OS keeps process table
- all tables have finite capacity
- wait until any of the children terminator
- or wait until a specific process terminates
- fork-exec the stupid way
- just an integer
- USR1, USR2
- typically used for errors
- procesess
- auto(matic): stored on stack, incl. params, local vars
- static: retains value until program termination, only visible in block
- extern: global and declared in another file
- register: stored in CPU register rather than in memory; may be ignored
- good compiler will perform this optimization anyway
- mutable: allows parent's const status to change (i.e. struct/c++ class)
- see http://www.csegeek.com/csegeek/view/tutorials/cpp_lang/cpp_storage_class.php
- automatic variable disappear at the closing brace
- != global memory
- a paramter name specificed within a function prototype is useable only in that prototype
- the name is not binding wihtin the implementation of hte function
- this is really → labels!
- do not use these unless you're forced to and you want Christine to Christine you!
- (these notes are not comprehensive for this course.)
- libraries are in
/usr/include
- duh. honestly this is first-year/highschool material. what is this curriculum? - Hot tip from the prof. Don't try to grep through
*.o
in/usr/lib
it's object code. yawn. why am i here?
- include the header files
- specify additonal location with
gcc -I
- specify additonal location with
- linking
-lpthread -lm
- specify additional location with
gcc -L
- specify additional location with
- create object files, package them
- supply header
*.h
files and archive*.a
files to - where is my
libc.a
? → this is coolgcc --print-file-name=libc.a
- include a library →
#include <yomammagotsalib.h>
- ( really? can i just get a 90% on the exam for writing every answer as a portable sh script without bashisms? )
- ( cause he's got a big ego. he talks like dis cuz he can back it up. )
- ( who needs twitter when you can read my course notes. )
- ( My dream is that we would cover POSIX-defined constructs instead of backticks. )
- not on the exam -_-
- Data Segment / Global Segment
- static vars, global vars
- Text Segment / Code Segment:
- program instructions
- Function Call Stack:
- automatic vars, parameters
- Heap
- dynamically allocated with malloc, calloc, c++ new