Skip to content

Instantly share code, notes, and snippets.

@chrisbodhi
Created February 2, 2021 01:48
Show Gist options
  • Save chrisbodhi/c1bce1d1971d8215d575022bce227d01 to your computer and use it in GitHub Desktop.
Save chrisbodhi/c1bce1d1971d8215d575022bce227d01 to your computer and use it in GitHub Desktop.

Ex 1.1: Experiment with “hello world” program

int main(void) {
    printf("hello, world\n");
    return 0;
}

Ex 1.2: Experiment with other escape sequences

int main(void) {
    printf("how are\t\t\t\nyou\n\a\b\e");
    return 0;
}

Ex 1.3: Modify the temp conversion program to print a heading above the table

int main(void) {
    float fahr, celsius;
    int lower, upper, step;

    lower = -40;
    upper = 300;
    step = 20;

    fahr = lower;
    printf("Fahr\tCelsius\n");
    while (fahr <= upper) {
        celsius = (5.0/9.0) * (fahr-32.0);
        printf("%3.0f %6.1f\n", fahr, celsius);
        fahr = fahr + step;
    }
    return 0;
}

Ex 1.4: Write a program to do Celsius -> Fahr

int main(void) {
    /* 0°C × 9.0/5.0) + 32 */
    float celsius, fahr;
    int upper, lower, step;

    lower = -40;
    upper = 300;
    step = 20;

    celsius = lower;
    printf("Celsius\tFahr\n");
    while (celsius <= upper) {
        fahr = celsius * (9.0/5.0) + 32;
        printf("%6.0f %3.0f\n", celsius, fahr);
        celsius += step;
    }

    return 0;
}

Ex 1.5: Print the above temp table in reverse order

int main(void) {
    /* 0°C × 9.0/5.0) + 32 */
    float celsius, fahr;
    int upper, lower, step;

    upper = 300;
    lower = -40;
    step = 20;

    celsius = upper;
    printf("Celsius\tFahr\n");
    while (celsius >= lower) {
        fahr = celsius * (9.0/5.0) + 32;
        printf("%6.0f %3.0f\n", celsius, fahr);
        celsius -= step;
    }

    return 0;
}

(Aside: use #defines)

int main(void) {
    printf("lower is %d", LOWER);
    return 0;
}

Ex 1.6: Verify that the expression getchar() != EOF is 0 or 1 Ex 1.7: Write a program to print the value of EOF

int main(void) {
    int answer;

    answer = (getchar() != EOF);
    printf("answer is %d, EOF is %d", answer, EOF);
    return 0;
}

p. 20 Ex 1.8: Write a program to count the number of blanks, tabs, and newlines. Ex 1.9: Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment