L1: C Warmup Flashcards

1
Q

Parts of the C Standard Library, Categorized (15)

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

C Standard Library:

Input/Output Library

A

stdio.h

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

C Standard Library:

General Function Library

A

stdlib.h

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

C Standard Libary:

Testing and Debug Function Libraries

A

assert. h
errno. h
ctype. h

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

C Standard Libary:

System Libraries

A

stddef. h
stdarg. h
signal. h
setjmp. h
limits. h
locale. h
time. h

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

C Standard Library:

Variable Libraries

A

float. h
string. h

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Fundamental Principles of Strings

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Array Declarations

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Array Declarations:

Undefined Declaration

A

Declared as a pointer:

char *B

or

char B[]

This allocates no storage, B is undefined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Primitive Types:

Integers

  • types
  • declaration
  • values
  • constants
A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Useful Functions:

Characters

(5 functions)

A

Library: ctype.h

Functions for testing a character against ranges:

isalpha(‘a’)

isdigit(‘0’)

iswhite(‘\t’)

isupper(‘A’)

islower(‘a’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Useful Names and Functions:

Strings

A

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Fundamentals of Arrays

(How Array Pointers Work)

A
  • 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]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Primitive Operations:

Bitwise Operations

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

C Primitive Types

A
  • Integers
    • int, long
      • 32, 64 bit respectively(outdated)
  • Characters:
    • char
      • 8-bit
  • Arrays
  • Pointers
    • address of integer: int *p
    • address of character: char *q
  • Functions
    • Names of subroutines
    • form:
      • returntype name(arg1, arg2,…){ //code}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

C Primitive Types:

Arrays

  • Declarations
  • Values
A
  • 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[]
17
Q

Primitive Types:

Characters

  • declaration
  • value
  • Special Character examples
A
  • Declaration:
    • char c;
  • Value:
    • 8-bit characters
  • Special Characters:
    • ‘a’ normal char
    • ‘\n’ newline char
    • ‘\t’ tab
    • ‘\0’ null terminator
    • ’\’ escaped backslash
18
Q

Useful Names and Functions:

Utility Functions

A

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
19
Q

How to use a routine from a different file

without referencing a header

A
  • 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
20
Q

Steps of Using

“while” logic

A
  1. Create Initial State
    • int s[4];
    • int i, n;
    • n = 4; i=0;
  2. Declare While loop with condition
    • while( i<n …>
    </n>
  3. Do work within the while loop
    • s[i] = i * i;
  4. Advance the state
    • ++i;
21
Q

Primitive Operations:

  • Incrementing
  • Decrementing
  • Post/Pre
  • Variable Types
A
  • 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
22
Q

Defining a Constant in C

A

define NAME (value)

example:

*convention is to use upper case for the name

23
Q

Compiler:

Compile a program to a specified name

A

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”

24
Q

Useful Names and Functions:

Input/Output (I/O)

A

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
25
Q

C Pointer Variations

A
  • Normal:
    • p
    • Returns pointer to itself
  • Pointer:
    • *p
    • Returns thing pointed at
  • Pointer to a field within a structure:
    • q->f
    • Returns field f of structure q
  • Address Pointer:
    • &x
    • Returns address of x