C: week 1

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/32

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

33 Terms

1
New cards

testing equality of floating point numbers

instead of x == y you should write fabs(x-y), and you #define EPSILON to a suitable value

2
New cards

explicit type casting

3
New cards

switch statements can only act on…

…ordinal values

4
New cards

general idiomatic form for switch case statements:

if you deliberately omit a break statement between cases, make it obvious by putting a comment

5
New cards

do while loop syntax

6
New cards

idiomatic form of writing an infinite loop

7
New cards

execution of code inside a loop (definitely a for loop, and perhaps do or while) can be manipulated by which statements

break: break out of the current loop

continue: jump to the end of the current loop (effectively ignoring everything below the continue statement)

8
New cards

enum syntax

enum day {MON, TUE, WED, THU, FRI, SAT, SUN};

when you initialise a value of this type, you must declare it as:

enum day d;

9
New cards

enum typedef example

typedef enum {MON, TUE, WED, THU, FRI, SAT, SUN} day;

we can now declare our variable as:

day d;

10
New cards

array declaration syntax

double data[MAXDATA];

to initialise an array at declaration:

int a[3] = {1, 2, 3}; // all elements specified

int b[10] = {1, 2, 3, 4}; // some elements unspecified

you may omit the array size when it can be inferred from the initializer:

int arr[] = {10,20,30,40,50,60};

11
New cards

how to find the number of elements in an array

#define NELEMENTS(arr) (sizeof(arr) / sizeof(arr[0]))

12
New cards

iteration over an array

13
New cards

iteration over arrays only works when…

…the full declaration of the array is available. it doesn’t work when the array is passed as a parameter to a function - inside any function sizeof(arr) delivers the size of a pointer, not the array.

so, when passing an array to a function, you usually pass a second parameter - the number of elements in the array.

14
New cards

strcpy()

char *

strncpy(char dst, const char src, size_t len);

copy the string src to dst (including the terminating ‘\0’ character

it’s your responsibility to ensure that there’s enough space for any string you strcpy() to fit, otherwise buffer overrun occurs

15
New cards

strcat()

 char *

     strcat(char restrict s1, const char restrict s2);

 append a copy of the null-terminated string s2 to the end of the null-terminated string s1, then add a terminating ‘\0’

16
New cards

sprintf()

does formatted output into a string buffer, overwriting the existing contents

17
New cards

declare a pointer to an int variable e.g.

int *ip;

to declare and initialise:

int *ip = &x;

18
New cards

dereference operator

*

int *ip = &x; int y = *ip;

y is the dereferenced value of ip

19
New cards

how do you increment a pointer?

(*ip)++;

NOT

*ip++ this parses as *(ip++) which means something entirely different

20
New cards

can you declare multiple pointers at once?

NO

use separate lines

21
New cards

all pointer arithmetic is…

scaled

22
New cards

pointer arithmetic: advancing a pointer within an array

as pointe arithmetic is scaled, ip++ is scaled by sizeof(*ip), here it adds sizeof(int)==4 to ip

23
New cards

idiomatic pointer-based strlen(s)

24
New cards

is pointer arithmetic allowed when you have a Pto1?

no, it is a classic error to do any pointer arithmetic when we’re dealing with a Pto1. pointer arithmetic (p++ etc) is only allowed when we’re dealing with a PtoM

25
New cards

pointer arithmetic: incrementing a pointer by N elements

ip += N;

26
New cards

can you add two pointers together?

no, thats meaningless

27
New cards

making a PtoM point to &arr[N] where the array has N elements…

this is a pointer one element beyond the end of the array, it’s allowed because many loops naturally lead to this final value.

you’re not allowed to dereference this pointer.

28
New cards

pointer arithmetic: for both comparison and subtraction, it is Undefined Behaviour if…

…the pointers are not PtoMs that both point within the same array

29
New cards

meaning of *p++

*p++ parses as *(p++) and is a combination of *p and p++. Called post-increment, this delivers the current value of *p and then increments the value of p as a side-effect

30
New cards

compact and idiomatic implementation of strcpy(dst, src)

31
New cards

which 3 other combined dereference-and-inc/dec operators does C provide?

32
New cards

what are C’s 3 file-handles pre-opened from which data can be read or written?

33
New cards

printf() usage

“print-formatted, “a versatile way of printing text to stdout

e.g.

printf("An integer: %d, and a character %c.\n", 42, c);