Week 4 Wednesday 25/06/2025 12PM-2PM - Bongo/Tabla Lab (K17 G07/G08)
Sign up at: https://buytickets.at/comp1511unsw/1290763 (Access Code: "COMP1511", also linked on the forum)int ice_cream_per_day[7];

int ice_cream_per_day[7] = {3, 2, 1, 2, 1, 3, 5};
^ Note you can only do this when you declare, not later!
int ice_cream_per_day[7] = {};
^ Will initialise all elements to 0
int my_data[] = {3, 2, 1, 2, 1, 3, 5};
^ Will create a 7-element array
int my_data[14] = {3, 2, 1, 2, 1, 3, 5};
^ Will create a 14-element array, with the first 7 elements then 7 0'd out
int first_day_ice_creams = ice_cream_per_day[0];

ice_cream_per_day[0] = 5;

"Jake Renzella" -> is a string with 13 characters!C doesn't have a string data type :(
C has arrays! :)
int numbers[7] = {3, 2, 1, 2, 1, 3, 4}


We can build our own string type by using an array of chars!
char[]s#include <stdio.h>
int main(void) {
char name[3] = {'G', 'a', 'b'};
// change name to Jake
// :( can't, won't fit
return 0;
}
#include <stdio.h>
#define MAX_STR 50
int main(void) {
char name[MAX_STR] = {'J', 'a', 'k', 'e'};
return 0;
}
char name[MAX_STR] = {'J', 'a', 'k', 'e'};
char[]...\0char[]
Notice the \0 at the end! This means that C will know when it reaches the end of the array
char[]char word[] = {'h', 'e', 'l', 'l', 'o', '\0'};
So there are easier ways to use them:
char word[] = "hello";
"Jake!"" to wrap the string literalUsed to assign strings to char[] easily:
char name[] = "Jake Renzella";printf or fputsfgets<stdio.h>fgetsfgets(array[], length, stream)
stdin)fgets usage// Declare the array which will contain the string. Note, we don't know how big the string will be, so let's come up with a maximum.
char my_string[MAX_LENGTH]
// read the string in
fgets(my_string, MAX_LENGTH, stdin);
CTRL+D is entered in the terminal by calling fgets in a loopfgets() stops reading when either length-1 characters are read, newline character is read or an end of file is reached, whichever comes first#include <stdio.h>
// I know my string will never need to be more than 15 chars
#define MAX_LENGTH 15
int main(void) {
char name[MAX_LENGTH];
printf("Enter your name: ");
// fgets reads the entire string, including the newline character
while (fgets(name, MAX_LENGTH, stdin) != NULL) {
// every time this runs, we update `name`!
}
}
fputs(array[], stream)stdout in COMP1511char name[] = "Jake";
fputs(name, stdout);
^ Why doesn't fputs need the LENGTH, like fget?
strlen() -> gives us the length of the string (excluding the \0).strcpy() -> copy the contents of one string to anotherstrcat() -> join one string to the end of another (concatenate)strcmp() -> compare two stringsstrchr() -> find the first occurrence of a character
#include <string.h>