Skip to content

02: C Basics

#include <stdio.h>

int main() {
    printf("%lu\n", sizeof(int));

    // integer overflow
    int a = 98765;
    int b = 43210;
    int c = a * b;
    printf("c = %d\n", c);

    // floating-point imprecision
    float x = 0.1;
    c = ((x + x) == 0.2);
    printf("c = %d\n", c);

    printf("x = %.5f\n", x);
    printf("0.2 = %.5f\n", 0.2);
    printf("x = %.10f\n", x);
    printf("0.2 = %.10f\n", 0.2);

    // type conversion
    a = 5; b = 2;
    float c_f = (float)a / (float)b;

    // factor
    unsigned p = 0, q = 0;
    printf("Please enter your numbers: \n");
    scanf("%u%u", &p, &q); // not safe to handle arbitrary input
    if (q && !(p % q))
    printf("%u is a factor of %u\n", q, p);
    else
    printf("%u is NOT a factor of %u\n", q, p);
}