Chapter 4 : Character Strings and Formatted Input/Output

1. Write a program that asks for your first name, your last name, and then prints the names in the format last, first.

#include <stdio.h>

int main(void) {
    char firstName[40];
    char lastName[40];

    printf("Please enter your first name:
");
    scanf("%s", firstName);
    printf("Please enter your last name:
");
    scanf("%s", lastName);
    printf("Your name is %s %s.
", firstName, lastName);

    return 0;
}

4. Write a program that requests your height in inches and your name, and then displays the information in the following form:

Dabney, you are 6.208 feet tall

Use type float, and use / for division. If you prefer, request the height in centimeters and display it in meters.

#include <stdio.h>

int main(void) {
    float height;
    char name[30];

    printf("Enter your height in inches and your name please: 
");
    scanf("%f %s", &height, name);
    printf("%s, you are %.3f feet tall", name, height);
    return 0;
}

7. Write a program that sets a type double variable to 1.0/3.0 and a type float variable to 1.0/3.0. Display each result three times—once showing four digits to the right of the decimal, once showing 12 digits to the right of the decimal, and once showing 16 digits to the right of the decimal. Also have the program include float.h and display the values of FLT_DIG and DBL_DIG. Are the displayed values of 1.0/3.0 consistent with these values?

#include <stdio.h>
#include <float.h>

int main(void) {
    double a = 1.0 / 3.0;
    float b = 1.0 / 3.0;

    printf("a = %.4f, b = %.4f
", a, b);
    printf("a = %.12f, b = %.12f
", a, b);
    printf("a = %.16f, b = %.16f
", a, b);

    printf("FLT_DIG: %d, DBL_DIG: %d
", FLT_DIG, DBL_DIG);

    return 0;
}
苟利国家生死以, 岂因祸福避趋之
原文地址:https://www.cnblogs.com/chintsai/p/11829260.html