Week 1 Code Examples
// Lecture 1 Example
// hello_world.c
//
// This program was written by Angela Finlayson (z9801583)
// Date: 13/9/2026
//
// Our first C program
#include <stdio.h>
int main(void) {
printf("Hello \tCOMP1511 and COMP1911!!!\n\n\n");
printf("\"Goodbye\"...\n");
printf("\\\n");
return 0;
}
// A program showing how to print output in C
// The first of many C programs you will C
#include <stdio.h>
int main(void) {
printf("Hello COMP1511 and COMP1911\n");
return 0;
}
// A tricky example with escape characters
// Warning: this may hurt your brain
#include <stdio.h>
int main(void) {
printf("\\\"\\\n");
return 0;
}
// An example declaring and printing variables
// of types int, double and char
// and the kinds of errors you might make
// for COMP1511/1911 26T3
// Written by: z9801583
// Date: 15/09/2026
#include <stdio.h>
int main(void) {
int sides = 4;
int result = -99;
double value = 1.5;
printf("A square has %d sides\n", sides);
printf("The result is %d and the value is %lf\n", result, value);
return 0;
}
// An example declaring and printing variables
// of types int, double and char
// and the kinds of errors you might make
// for COMP1511/1911 26T3
// Written by: z9801583
// Date: 15/09/2026
#include <stdio.h>
int main(void) {
int sides;
int result = -99;
int value = 1.5;
printf("A square has %d sides\n", sides);
printf("The result is %d and the value is %d\n", result);
return 0;
}
// An example declaring and printing variables
// of types int, double and char
// for COMP1511/1911 26T3
// Written by: z9801583
// Date:15/9/2026
#include <stdio.h>
int main(void) {
// Declare a variable to store a player's initial and intialise it
char initial = 'W';
// Declare and initialise a variable to store an amount of gold
int gold = 0;
// Declare and initialise a variable to store the health of the player
double health = 12.5;
// Add code to print out the player's initial
printf("Welcome player %c\n", initial);
// Add code to print the gold and the health
printf("Gold: %d Health: %.2lf\n", gold, health);
return 0;
}
// An example declaring and printing variables
// of types int, double and char
// for COMP1511/1911 26T3
// Written by: z9801583
// Date: 15/9/2026
#include <stdio.h>
int main(void) {
char initial;
printf("Please enter your first initial: ");
scanf("%c", &initial);
int age;
printf("Please enter your age: ");
scanf("%d", &age);
double height;
printf("Please enter your height in cm: ");
scanf("%lf", &height);
printf("Initial: %c\nAge: %d\nHeight: %lf\n", initial, age, height);
return 0;
}
// An example declaring and printing variables
// of types int, double and char
// and the confusion of when scanf("%c", &initial)
// reads in the newline character
// for COMP1511/1911 26T3
// Written by: z9801583
// Date: 15/9/2026
#include <stdio.h>
int main(void) {
int age;
printf("Please enter your age: ");
scanf("%d", &age);
double height;
printf("Please enter your height in cm: ");
scanf("%lf", &height);
char initial;
printf("Please enter your first initial: ");
scanf("%c", &initial);
printf("Age: %d Height: %lf Initial: %c \n", age, height, initial);
return 0;
}
// An example declaring and printing variables
// of types int
// for COMP1511/1911 26T3
// Written by: z9801583
// Date: 15/9/2026
#include <stdio.h>
int main(void) {
int gold;
int level;
scanf("%d", &gold);
scanf("%d", &level);
printf("Gold: %d Level: %d\n", gold, level);
// What happens if you do not type in ints?
return 0;
}
// An example declaring and printing variables
// of types int, double and char
// and how to use scanf(" %c", &initial)
// to skip over whitespace including the newline characters
// for COMP1511/1911 26T3
// Written by: z9801583
// Date: 15/9/2026
#include <stdio.h>
int main(void) {
int age;
printf("Please enter your age: ");
scanf("%d", &age);
double height;
printf("Please enter your height in cm: ");
scanf("%lf", &height);
char initial;
printf("Please enter your first initial: ");
scanf(" %c", &initial);
printf("Age: %d Height: %lf Initial: %c \n", age, height, initial);
return 0;
}
// A simple program demonstrating expressions
// Try to work out what will be printed without running the program
#include <stdio.h>
int main(void){
int x = 4;
int y = 3;
int z = (x + y) * 10 - x;
printf("%d\n", z);
char c1 = 'a';
char c2 = c1 + 1;
printf("%c\n", c2);
x = 3;
y = 2;
z = x / y;
printf("%d\n", z);
x = 3;
y = 2;
double w = x / y;
printf("%lf\n", w);
return 0;
}
// Some more expressions to try
// with division and mod
// Press Ctrl / to comment and uncomment sections of code
// to see what the output would be
#include <stdio.h>
int main(void){
int x = 5;
double y = 2;
double z = x / y;
printf("%.1lf\n", z);
printf("%d\n", x % 2);
// int zero = 0;
// printf("%d\n", x / zero);
// printf("%d\n", x % zero);
return 0;
}
// Some more expressions to try
#include <stdio.h>
int main(void){
// double is not accurate
double x = 0.3;
printf("%lf %lf\n", x, 0.1);
printf("%.20lf %.20lf\n", x, 0.1);
// int overflow example
int y = 2147483647;
printf("%d\n", y);
y = y + 1;
printf("%d\n", y);
return 0;
}
// Write a program convert.c that
// prompts the user enter the number of hours
// calculates how many minutes that is equivalent to
// prints out the number of minutes
// See sample output below:
// $ ./convert
// Please enter the number of hours: 2.5
// That is 150.00 minutes
#include <stdio.h>
int main(void){
double hours;
double minutes;
printf("Please enter the number of hours: ");
scanf("%lf", &hours);
minutes = hours * 60;
printf("That is %.2lf minutes\n", minutes);
return 0;
}