2. C programming

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/77

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 11:45 AM on 5/17/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

78 Terms

1
New cards

What does every C program have

Header file — function declarations and at least a main function (the entry point)

2
New cards

what line is at the top of every C file

The preprocessor directive, #include <stdio.h> , to include necessary header files.

3
New cards

What type does main have to return

the main function in C must return an int value (0 typically means success)

4
New cards
5
New cards

How do you write C comments

// single line
/* block comment */

6
New cards

Provide code to display hello world in C (create main too)

#include <stdio.h>

int main(){
	printf("Hello world\n");
	return 0;
}

7
New cards

what is the ‘short’ data type in C

a smaller integer (2 bits instead of 4)

8
New cards

what is unsigned and signed in C

unsigned means the value cannot be negative

variables are set to signed by default

9
New cards

what is the stack in C

memory used automatically for function calls and local variables

10
New cards

what is the heap in C

memory managed manually by the programmer for dynamically-allocated data

11
New cards

what is the symbol table

a compiler structure tracking global variables and function names

12
New cards

what is shadowing / masking in C

when a variable in an inner scope hides one with the same name in an outer scope

13
New cards

How is the heap used

allocated with malloc()

freed with free()

14
New cards

give an example of when malloc and free is used

int main() {
	int *num = malloc(sizeof(int));
	*num = 5;
	printf("%d\n", *num);
	free(num);
	return 0;
}

15
New cards

when is malloc needed

when you need memory while the program is running

free gives back the memory when you are done with it

16
New cards

what is an operand

the value or address an opcode / operator acts on

17
New cards

what is a unary operator

an operator that takes one operand e.g !x, ++i

18
New cards

what is a binary operator

one that takes two operands e.g. a + b

19
New cards

what is the modulo operator

%

20
New cards

what is the remainder operator


/

21
New cards

What does the operator & do

bitwise AND

22
New cards

what does the operator | do

bitwise OR

23
New cards

what does the operator ^ do

bitwise XOR

24
New cards

what does the operator ~ do?

bitwise NOT

25
New cards

what does the operator « do

bitwise left shift

5 « 1

moves the binary representation of 5 left by 1 place

26
New cards

what does the operator » do

bitwise right shift

5 » 1

moves the binary representation of 5 right by 1 place

27
New cards

what does putting & before variables do in c

gets the memory address of the variable

int x = 10;
printf("%p", &x);
// returns the address of the variable

28
New cards

what does putting * before variables do in c

it defines that the variable is a pointer.

it will store the address of a variable

int x = 10; 
int *p = &x;
printf("%d", *p);

29
New cards

what is a loop invariant

a condition that remains true throughout a loop’s execution

30
New cards

how does switch case work in C

switch () {
	case 1:
		//do something
		break;
	case 2:
		//do something
		break;
	default:
		//does something if no case match
}

31
New cards

how do you loop through a 2d array in c

int array[2][3];

for(i = 0; i< 2; i++) {
	for(j = 0; j < 3; j++) {
		printf("%d\n", array[i][j]);
	}
}

32
New cards

what does void mean in a function

that it returns no value

33
New cards

what is a library

a collection of pre-written compiled functions

34
New cards

How does C pass in its variables

pass by value

function parameters are copies; modifying them does not affect caller variables.

35
New cards

how do you modify a callers variable in C?

pass a pointer to it

void change(int *x){
	*x = 10;
}

int main() {
	int a = 5;
	change(&a);
}

36
New cards

what is a prototype in C

a function prototype is a declaration of a function before it is defined, so the compiler knows the name, return type and parameter types.

int add(int a, int b);

int main(){
	//...
}

37
New cards

how do you define a function in C

int add(int a, int b){
	return a + b;
}

replace int with void for no return type

38
New cards

In what order do 2d arrays use columns and rows

[row][column]

39
New cards

How is array data stored in memory

Contiguously

40
New cards

how do you initialise an array in C

int arr[] = {1, 2, 3, 4, 5};
// or 
int arr[5];

41
New cards

how do you read from an array in C

int x = arr[2];

42
New cards

how do you write to an array in c

arr[0] = 7;

43
New cards

how do you check if you are within the bounds of an array in c

if(index >= 0 && index < 5){
	arr[index] = value
}

44
New cards

What is a string made up of in C

A char array terminated by a null character (\0)

45
New cards

What is the string “hello” made up of in C

(h, e, l, l, o, \0)

46
New cards

how do you create a pointer to a string

char *s = "hello"

47
New cards

how do you create a modifiable copy of a string

char s[] = “hello”

48
New cards

how do you compare strings in C

strcmp(s1, s2);
/*
0 if equal
negative if s1 < s2
positive if s2 > s1
*/ 

49
New cards

how do you import strings in C

#include <string.h>

50
New cards

how do you get the length of a string in C

strlen(s)

51
New cards

how do you copy a string into a new char array in c

strcpy(dest, src)

52
New cards

how do you add strings together in c?

strcat(dest, src)

53
New cards

how do you search a string for a character

strchr(str, chr)
//returns pointer of first occurrence of search item 
//or returns null

54
New cards

how do you copy a certain amount of characters in a string in C

strncpy(dest, src, n)
// copies up to n characters

55
New cards

how do you print a string in C

char str[] = "hello";
printf("%s\n", str);

56
New cards

what is the format for printing an int in C

%d

57
New cards

what is the format for printing a char in C

%c

58
New cards

what is the format for printing a float in C

%f

59
New cards

what is a pointer

a variable that stores the memory address of another variables

60
New cards

what is indirection in pointers

accessing data through a reference to its location, rather than directly

61
New cards

what does dereferencing mean in C

accessing the value at the address a pointer holds

62
New cards

how do pointers work with different data types

pointers have different data types

int * points to ints

char * points to chars

63
New cards

how do you return an address of a variable

&variablename

64
New cards

what does a pointer need before being used

it needs to be initialised (normally to null)

65
New cards

how do you use &variable to let a function to use pass-by-reference

when defining the function, have pointers as parameters

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x = 5;
    int y = 10;

    swap(&x, &y);
    return 0;
}

66
New cards

how do you read an input from the user

scanf("%d", &input)

remember the & for address of variable

67
New cards

how do you create a variable and create a pointer to the variable

int x = 7;
int *p = &x;

68
New cards

how do you make a swap function in C

void swap(int *a, int *b){
	int temp = *a;
	*a = *b;
	*b = temp;
}

69
New cards

when do you need to allocate memory in C

when you need data of a size only known at runtime

or data that outlives the function that created it

70
New cards

what does malloc(n) return

a pointer to n bytes of heap memory, or null if fails

71
New cards

what does sizeof() do in C and when do you use it

sizeof returns the size of the data type you need.

you use it when you are using malloc() to allocate the right amount of memory

int *p = malloc(sizeof(int));

72
New cards

what is a struct in C

a struct groups related variables of possibly different types under one name

structs let you build composite data types matching the structure of your problem (a person, point etc.)

73
New cards

what is a field / member in a struct

a variable inside a struct

74
New cards

what does . do in structs

gets a field through a struct variable

example:

p1.age = 20;

75
New cards

what does → do in structs

gets a field through a pointer to the struct

example:

struct Person *p = malloc(sizeof(struct Person));

p->age = 25;

76
New cards

what does → mean in structs

go to the struct that points to the pointer, and get the variable

77
New cards

what is a self-referential struct

a struct containing a pointer to its own type (for linked structures)

78
New cards