#include <stdio.h>
#include <stdlib.h>

void formatted_output_example();
void fahrenheit_example();
void formatting_other_types_example();
void formatting_strings_example();
void clear_screen();

int main(int argc, char** argv) {
    formatted_output_example();

    clear_screen();
    fahrenheit_example();

    clear_screen();
    formatting_other_types_example();

    clear_screen();
    formatting_strings_example();

    return (EXIT_SUCCESS);
}

void formatted_output_example(){
    printf("Formatted Output Example:\n\n");

    int a,b;
    float c,d;

    a = 15;
    b = a / 2;

    printf("%d\n",b);
    printf("%3d\n",b);
    printf("%03d\n",b);

    c = 15.3;
    d = c / 3;

    printf("%3.2f\n\n",d);
}

void fahrenheit_example(){
    printf("Fahrenheit Example:\n\n");

    int Fahrenheit;

    for (Fahrenheit = 0; Fahrenheit <= 300; Fahrenheit = Fahrenheit + 20) {
        printf("%3d %06.3f\n", Fahrenheit, (5.0/9.0)*(Fahrenheit-32));
    }
}

void formatting_other_types_example(){
    printf("Formatting Other Types Example:\n\n");

    printf("The color: %s\n", "blue");
    printf("First number: %d\n", 12345);
    printf("Second number: %04d\n", 25);
    printf("Third number: %i\n", 1234);
    printf("Fourth number: %10d\n", 12345);
    printf("Float number: %3.2f\n", 3.14159);
    printf("Decimals: %d, %ld\n", 1977, 650000L);
    printf("Hexadecimal: %x\n", 255);
    printf("Octal: %o\n", 255);
    printf("Unsigned value: %u\n", 150);
    printf ("Some different radixes: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100);
    printf("Characters: %c, %c\n", 'A', 'a');
    printf("ASCII code of char '%c': %d\n", 'a', 'a');
    printf("Just the percentage sign: %%\n");
    printf("Percentage value: %2d%%\n", 10);
    printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);
    printf ("Width trick: %*d \n", 5, 10);
    printf ("%s \n\n", "A string");
}

void formatting_strings_example(){
    printf("Formatting Strings Example:\n\n");

    int n = 1;

    printf("%d.\t:%s:\n", n++, "Hello, world!");
    printf("%d.\t:%15s:\n", n++, "Hello, world!");
    printf("%d.\t:%.10s:\n", n++, "Hello, world!");
    printf("%d.\t:%-10s:\n", n++, "Hello, world!");
    printf("%d.\t:%-15s:\n", n++, "Hello, world!");
    printf("%d.\t:%.15s:\n", n++, "Hello, world!");
    printf("%d.\t:%15.10s:\n", n++, "Hello, world!");
    printf("%d.\t:%-15.10s:\n", n++, "Hello, world!\n\n");
}

void clear_screen(){
    getchar();

    #ifdef __linux__
    system("clear");
    #else
    system("cmd /c cls");
    #endif
}
