1/77
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
What does every C program have
Header file — function declarations and at least a main function (the entry point)
what line is at the top of every C file
The preprocessor directive, #include <stdio.h> , to include necessary header files.
What type does main have to return
the main function in C must return an int value (0 typically means success)
How do you write C comments
// single line
/* block comment */Provide code to display hello world in C (create main too)
#include <stdio.h>
int main(){
printf("Hello world\n");
return 0;
}what is the ‘short’ data type in C
a smaller integer (2 bits instead of 4)
what is unsigned and signed in C
unsigned means the value cannot be negative
variables are set to signed by default
what is the stack in C
memory used automatically for function calls and local variables
what is the heap in C
memory managed manually by the programmer for dynamically-allocated data
what is the symbol table
a compiler structure tracking global variables and function names
what is shadowing / masking in C
when a variable in an inner scope hides one with the same name in an outer scope
How is the heap used
allocated with malloc()
freed with free()
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;
}when is malloc needed
when you need memory while the program is running
free gives back the memory when you are done with it
what is an operand
the value or address an opcode / operator acts on
what is a unary operator
an operator that takes one operand e.g !x, ++i
what is a binary operator
one that takes two operands e.g. a + b
what is the modulo operator
%
what is the remainder operator
/
What does the operator & do
bitwise AND
what does the operator | do
bitwise OR
what does the operator ^ do
bitwise XOR
what does the operator ~ do?
bitwise NOT
what does the operator « do
bitwise left shift
5 « 1
moves the binary representation of 5 left by 1 place
what does the operator » do
bitwise right shift
5 » 1
moves the binary representation of 5 right by 1 place
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 variablewhat 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);what is a loop invariant
a condition that remains true throughout a loop’s execution
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
}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]);
}
}what does void mean in a function
that it returns no value
what is a library
a collection of pre-written compiled functions
How does C pass in its variables
pass by value
function parameters are copies; modifying them does not affect caller variables.
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);
}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(){
//...
}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
In what order do 2d arrays use columns and rows
[row][column]
How is array data stored in memory
Contiguously
how do you initialise an array in C
int arr[] = {1, 2, 3, 4, 5};
// or
int arr[5];how do you read from an array in C
int x = arr[2];how do you write to an array in c
arr[0] = 7;how do you check if you are within the bounds of an array in c
if(index >= 0 && index < 5){
arr[index] = value
}What is a string made up of in C
A char array terminated by a null character (\0)
What is the string “hello” made up of in C
(h, e, l, l, o, \0)
how do you create a pointer to a string
char *s = "hello"how do you create a modifiable copy of a string
char s[] = “hello”
how do you compare strings in C
strcmp(s1, s2);
/*
0 if equal
negative if s1 < s2
positive if s2 > s1
*/ how do you import strings in C
#include <string.h>how do you get the length of a string in C
strlen(s)how do you copy a string into a new char array in c
strcpy(dest, src)how do you add strings together in c?
strcat(dest, src)how do you search a string for a character
strchr(str, chr)
//returns pointer of first occurrence of search item
//or returns nullhow do you copy a certain amount of characters in a string in C
strncpy(dest, src, n)
// copies up to n charactershow do you print a string in C
char str[] = "hello";
printf("%s\n", str);what is the format for printing an int in C
%d
what is the format for printing a char in C
%c
what is the format for printing a float in C
%f
what is a pointer
a variable that stores the memory address of another variables
what is indirection in pointers
accessing data through a reference to its location, rather than directly
what does dereferencing mean in C
accessing the value at the address a pointer holds
how do pointers work with different data types
pointers have different data types
int * points to ints
char * points to chars
how do you return an address of a variable
&variablenamewhat does a pointer need before being used
it needs to be initialised (normally to null)
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;
}how do you read an input from the user
scanf("%d", &input)remember the & for address of variable
how do you create a variable and create a pointer to the variable
int x = 7;
int *p = &x;how do you make a swap function in C
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}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
what does malloc(n) return
a pointer to n bytes of heap memory, or null if fails
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));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.)
what is a field / member in a struct
a variable inside a struct
what does . do in structs
gets a field through a struct variable
example:
p1.age = 20;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;what does → mean in structs
go to the struct that points to the pointer, and get the variable
what is a self-referential struct
a struct containing a pointer to its own type (for linked structures)