Week 1 Code Examples
// Lecture Example
// hello_comp.c
//
// Written by: Angela Finlayson (z9801583)
// Date: 16/09/2025
//
// A program showing how to print output in C
// The first of many C programs you will C
/*
This is another way to do comments
in C!!!!!!!
*/
#include <stdio.h>
int main(void) {
printf("Hello COMP1511 and COMP1911\n");
printf("Goodbbye!!!!!!!!!!!!!\n");
return 0;
}
// Lecture 1 Example
// hello_world.c
//
// This program was written by Angela Finlayson (z9801583)
// on 15/9/2025
//
// Our first C program
#include <stdio.h>
int main(void) {
printf("\"Hello \\ world!\"\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 25T3
// Written by: z9801583
// Date: 16/09/2025
#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 25T3
// Written by: z9801583
// Date: 16/09/2025
#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 25T3
// Written by: z9801583
// Date:15/9/2025
#include <stdio.h>
int main(void) {
// Declare and initialise an int variable
int years = 3;
// Declare and initialise a double
double lab_mark = 1.7;
// Print the int and the double variable
printf("Years: %d lab_mark %lf\n", years, lab_mark);
// Declare and initialise a char variable
char grade = 'A';
// Print the char variable
printf("Grade %c\n", grade);
// Modify the double variable
lab_mark = 1.719312323;
// Print the double variable to 2 decimal places
printf("%lf %.2lf\n", lab_mark, lab_mark);
printf("%%\n");
return 0;
}
// An example declaring and printing variables
// of types int, double and char
// for COMP1511/1911 25T3
// Written by: z9801583
// Date: 16/9/2025
#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);
// Complete
double x;
double y;
printf("Please enter x and y: ");
scanf("%lf %lf", &x, &y);
printf("(%lf, %lf)\n",x, y);
// What happens if the user does not type in the right thing ?
return 0;
}
#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;
}
#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;
}
// 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;
}