C Flashcards
History of c
-Ritchie and Thompson 1973
Rewrite UNIX kernel with C
-Richie and Kernaghan
Created the “C programming language”
C
- general purpose language
- equally usable for applications programming and systems programming (network protocol, database management system, write a compiler)
- ubiquitous (where find C you find UNIX)
Features of C
-most toolchains have relatively small footprint
Popular choice for developing embedded systems
Operating systems research and development
Good choice for systems programs that one expects to port
Compile time features
- ANSI-compliant compilers provide extensive compile-time diagnostics
- also provide a continuum of optimizations
Run time features
-easy to adapt a C compilers output to the execution environment on a platform
Missing features
- no native array bounds checking
- no null pointer checkers
- no uninitialized vars check
Aggregate data types
- struct: one mechanism to declare user-defined types
- arrays: can be set scaler or aggregate
- union: similar to structs, but members are overlaid
Static and extern
- internal linkage
- external linkage
Addresses and pointers
- All bars refer to data
- All data resides in memory
- Every memory location has an address
- C exposes these details for us to use
Program scope
Var exists for programs lifetime and can be accessed from other files (extern)
File scope
Visible from its declaration to end of source file
Static
Function scope
Visible from begging to end of function
Block scope
- visible from form declaration to end of the block
- block is curly brackets
C preprocessor
- separate program that runs before the compiler
- allows macro processing, inclusion of additional C source files, conditional compilation
Macro processing
- a name that has an associated text string
- macros introduced from #define
Standard headers
Stdio.h : standard I/O library funcs Math.h : math funcs Stdlib.h : memory allocation funcs and general utility funcs string.h : string funcs ctype.h : char testing and mapping funcs
Abstract data type
-an abstract data type is a set of operations which access a collection of stored data
FILE *fopen(char *filename, char *mode)
- open file corresponding to filename
- can be r rw or rw+
- if errors occur returns null
char *fgets(char *buf, int n, FILE *stream)
- read at most n-1 chars from stream and copy to location buf. Terminates when newline or n-1 chars
- returns null if EOF or error
- set stream to stdin to accept input from standard input
int scanf(char *format, […])
- read formatted data from standard input
- returns EOF Shen encountered else returns number of fields successfully converted
- format specifiers encoded in format
printf(char *format, […])
- print formatted output to standard output
- returns the number of chars printed
- the format specifiers are encoded in the string format
- takes a variable # of arguments
int fprintf(FILE *stream, char *format, […])
-like printf but output goes to already opened stream
int fputc(int c, FILE *stream)
- outputs a single char to already opened stream
- character is stored in an int
- num from 0 to 255
- can also pass a char as the first parameter
int fclose(FILE *stream)
- closes the stream (flushes all OS buffers such that output file is completed)
- dissociates the actual file from the stream variable
- returns 0 if file closed properly