L1: C Warmup Flashcards
Parts of the C Standard Library, Categorized (15)
General:
- stdio.h - input/output
- stdlib.h - General Functions
- math.h - Math Functions
System:
- stddef.h
- stdarg.h
- signal.h
- setjmp.h
- limits.h
- locale.h
- time.h
Variables:
- float.h
- string.h
Testing/Debug:
- assert.h
- errno.h
- ctype.h
C Standard Library:
Input/Output Library
stdio.h
C Standard Library:
General Function Library
stdlib.h
C Standard Libary:
Testing and Debug Function Libraries
assert. h
errno. h
ctype. h
C Standard Libary:
System Libraries
stddef. h
stdarg. h
signal. h
setjmp. h
limits. h
locale. h
time. h
C Standard Library:
Variable Libraries
float. h
string. h
Fundamental Principles of Strings
- Strings are arrays of char’s where the last char is ‘\0’
- Declaration: char s[], s is an array variable that points to the array
- Assignment:
- s = “abc”
- Assigment with an empty string:
- s = “”
- s actually points to the null terminator “\0”
- Assign 0:
- s = 0
- s is a null pointer, points nowhere
Array Declarations
- Format:
- type name[size]
- example: char A[4]
- Once declared, A is a constant that can’t be modified
- The name ‘A’ is a pointer to the first element in the array
Array Declarations:
Undefined Declaration
Declared as a pointer:
char *B
or
char B[]
This allocates no storage, B is undefined
Primitive Types:
Integers
- types
- declaration
- values
- constants
- int
- declaration: int i;
- 16 or 32 bit (or 64)
- Constants:
- 0, 1, 10, 0xF, 010
- long,
- end in ‘L’
- 32, 64, or 128 bit (2x as long as int)
- Constants:
- 0L, 1L, 10L, 0xFL, 010L
Useful Functions:
Characters
(5 functions)
Library: ctype.h
Functions for testing a character against ranges:
isalpha(‘a’)
isdigit(‘0’)
iswhite(‘\t’)
isupper(‘A’)
islower(‘a’)
Useful Names and Functions:
Strings
include ‘<‘string.h’>’
- Concatenate:
- strcat(s, “more”)
- Copy String:
- strcpy(s, “more”)
- Compare:
- strcmp(s, t)
- returns 0 if equal, -1 if s<t>t</t>
- Length of String
- strlen(s)
- Find character in string:
- strchr(s,c)
- Returns pointer to first char in s that equals character c (or returns NULL)
Fundamentals of Arrays
(How Array Pointers Work)
- int A[4]
- Allocates room on stack, makes A point to first element
- *A = 21
- Stores the value 21 where A points to the first element
- *(A+1) = 6
- Stores the value 6 at the next int beyond where A points
- Arrays are pointers to the first element
- *(A+n) is equivalent to A[n]
Primitive Operations:
Bitwise Operations
- Bitwise AND:
- i & j
- Bitwise OR:
- i | j
- Bitwise NOT:
- ~i
- Bitwise XOR:
- i ^ j
- Bitwise Left Shift, Right Shift:
- a << b shift bits of a by b bits
- a >> b
C Primitive Types
- Integers
- int, long
- 32, 64 bit respectively(outdated)
- int, long
- Characters:
- char
- 8-bit
- char
- Arrays
- Pointers
- address of integer: int *p
- address of character: char *q
- Functions
- Names of subroutines
- form:
- returntype name(arg1, arg2,…){ //code}
C Primitive Types:
Arrays
- Declarations
- Values
- Declaration:
- type name[size]
- Name with new space allocated
- This is a constant
- ex: char A[10] , int I[8]
- Empty Declaration:
- type name[]
- Pointer to an array, name to share old space
- This is a variable, can be redirected
- ex: char B[], int J[]
Primitive Types:
Characters
- declaration
- value
- Special Character examples
- Declaration:
- char c;
- Value:
- 8-bit characters
- Special Characters:
- ‘a’ normal char
- ‘\n’ newline char
- ‘\t’ tab
- ‘\0’ null terminator
- ’\’ escaped backslash
Useful Names and Functions:
Utility Functions
from the “stdlib.h” library
- atoi(s)
- convert s to an integer (0 if not a number)
- malloc(n)
- allocates n bytes of new memory and returns a pointer to it
- free(p)
- frees the block of memory that p points to
- exit(status)
- end the program, with the specified status
- getenv(“PWD”)
- get the specified environment(shell) variable
How to use a routine from a different file
without referencing a header
- Declare a function prototype that matches the function declaration in the other file:
- int myMethod(int n);
- When compiling, link the other file
- example:
- cc -o sum sum.c main.c
- This allows the program in “main” to use a function from sum.c, as long as it is declared
- It’s easier to just include other files
Steps of Using
“while” logic
- Create Initial State
- int s[4];
- int i, n;
- n = 4; i=0;
- Declare While loop with condition
- while( i<n …>
- Do work within the while loop
- s[i] = i * i;
- Advance the state
- ++i;
Primitive Operations:
- Incrementing
- Decrementing
- Post/Pre
- Variable Types
- Increment:
- Pre: ++i, returns new value
- Post: i++, returns old value
- Decrement
- Pre: –j, returns new value
- Post: j–, returns old value
- Applies to types:
- int
- long
- char
- pointers
Defining a Constant in C
define NAME (value)
example:
*convention is to use upper case for the name
Compiler:
Compile a program to a specified name
Use the ‘-o’ flag when compiling, followed by a name
example:
cc -o myProgram sub.c main.c
will compile both sub.c and main.c into an executable
program called “myProgram”
Useful Names and Functions:
Input/Output (I/O)
Library: stdio.h
- Named Values:
- NULL = 0
- EOF = -1
- Standard File Descriptors:
- stdin
- stdout
- stderr
- Functions:
- getchar() - gets next char from file or EOF
- putchar(c) - write one char to file
- printf(…) - high level output formatting to file
- fgets() - read one line from file