COMP1511 18s1 (webcms)
Code Examples from Lectures on C_basics
COMP1511 18s1 (flask)

A simple program demonstrating the use of scanf to sum 2 numbers
    #include <stdio.h>

int main(void) {
    int x, y, sum;
    printf("Enter x: ");
    scanf("%d", &x);
    printf("Enter y: ");
    scanf("%d", &y);
    sum = x + y;
    // These 6 printfs can be better replaced by a single printf
    printf("%d", x);
    printf(" + ");
    printf("%d", y);
    printf(" = ");
    printf("%d", sum);
    printf("\n");
    return 0;
}


A simple program demonstrating the use of scanf to sum 2 numbers
    #include <stdio.h>

int main(void) {
    int x, y, sum;
    printf("Enter x: ");
    scanf("%d", &x);
    printf("Enter y: ");
    scanf("%d", &y);
    sum = x + y;
    printf("%d + %d = %d\n", x, y, sum);
    return 0;
}


A simple program demonstrating the use of scanf to sum 2 numbers
    #include <stdio.h>

int main(void) {
    int x, y;
    printf("Enter x: ");
    scanf("%d", &x);
    printf("Enter y: ");
    scanf("%d", &y);
    printf("%d + %d = %d\n", x, y, x +y);
    return 0;
}


A simple program demonstrating the use of scanf
    #include <stdio.h>

int main(void) {
    int x, y;
    int answer;

    printf("Enter x: ");
    scanf("%d", &x);
    printf("Enter y: ");
    scanf("%d", &y);
    answer = x * x + y * y;
    printf("x squared + y squared = %d\n", answer);
    return 0;
}



Convert a temperature in fahrenheit to celsius
    #include <stdio.h>

int main(void) {
    double fahrenheit, celsius;

    printf("Enter Fahrenheit temperature: ");
    scanf("%lf", &fahrenheit);
    celsius = 5.0 / 9.0 * (fahrenheit - 32);
    printf("%lf Fahrenheit = %lf Celsius\n", fahrenheit, celsius);
    return 0;
}



Demonstrate approximate representation of reals producing error. sometimes if we subtract two approximations which are very close together we can can get a large relative error
correct answer if x == 0.000000011 (1 - cos(x)) / (x * x) is very close to 0.5 code prints 0.917540 which is wrong by a factor of almost two
    #include <stdio.h>
#include <math.h>

int main(void) {
    double x, y;
    x = 0.000000011;
    y = (1 - cos(x))  / (x * x);
    printf("correct answer = ~0.5 but y = %lf\n", y);
    return 0;
}



Demonstrate approximate representation of reals
The value 0.1 can not be precisely represented as a real
As a result a non-zero value is printed
    #include <stdio.h>

int main(void) {
    double a, b;
    a = 0.1;
    b = 1 - (a + a + a + a + a + a + a + a + a + a);
    printf("%g\n", b);
    return 0;
}



Overflow a 32-bit int compile with gcc or clang to see overflow
    #include <stdio.h>

int main(void) {
    int a, b;
    a = 2000000000;
    b = a + a;
    printf("%d", b);
    return 0;
}



Printing binary representation of ints

This is not a programming example - it uses features not taught in this course (or nor not yet taught)
    #include <stdio.h>

int main(void) {
    int a, j;

    while (1) {
        printf("Enter an int: ");
        scanf("%d", &a);
        j = 8 * (sizeof a);
        while (j > 0) {
            j = j - 1;
            printf("%d", (a >> j) & 1);
        }
        printf("\n");
    }
    return 0;
}



Printing bytes of a file as binary

This is not a programming example - it uses features not taught in this course (or nor not yet taught)
    #include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    FILE *stream;
    int c, i, j, byteCount;

    for (i = 1; i < argc; i = i + 1) {
        stream = fopen(argv[i], "r");
        if (stream == NULL) {
            perror(argv[i]);  // prints why the open failed
            exit(1);
        }
        c = fgetc(stream);
        byteCount = 0;
        while (c != EOF) {
            printf("%s: byte %4d = ", argv[i], byteCount);
            j = 7;
            while (j >= 0) {
                printf("%d", (c >> j) & 1);
                j = j - 1;
            }
            printf("\n");
            c = fgetc(stream);
            byteCount = byteCount + 1;
        }
    }
    return 0;
}



Convert a measurement in feet to metres

A simple program demonstrating the use of scanf and #define for constants
    #include <stdio.h>

#define INCHES_IN_FOOT  12
#define CM_IN_INCH      2.54
#define CM_IN_METRE     100

int main(void) {
    double feet;
    double metres;

    printf("Enter number of feet: ");
    scanf("%lf", &feet);

    metres = feet * INCHES_IN_FOOT * CM_IN_INCH / CM_IN_METRE;

    printf("%.2lf", feet);
    printf(" feet is ");
    printf("%.2lf", metres);
    printf(" metres\n");

    return 0;
}