zoukankan      html  css  js  c++  java
  • 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;
    }
    苟利国家生死以, 岂因祸福避趋之
  • 相关阅读:
    Linux let 命令
    perl hash array 嵌套 push
    Perl CGI编程
    Perl关联数组用法集锦
    关于反射和动态代理
    SpringBoot与web开发
    Springboot与日志
    Spring Boot
    SpringBoot的自动配置原理
    Spring MVC执行流程
  • 原文地址:https://www.cnblogs.com/chintsai/p/11829260.html
Copyright © 2011-2022 走看看