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;
    }
    苟利国家生死以, 岂因祸福避趋之
  • 相关阅读:
    strong,weak, retain, assign的区别@property的参数
    iOS 声明属性关键字讲解
    iOS中ARC和非ARC混用
    存储过程修改产品描述页图片alt描述信息
    mysql字符串函数(转载)
    读者证
    存储过程
    复制档案或目录 linux cp命令详解
    linux shell获取时间
    linux 备份日志文件
  • 原文地址:https://www.cnblogs.com/chintsai/p/11829260.html
Copyright © 2011-2022 走看看